Migration to AndroidX

痞子三分冷 提交于 2020-01-23 06:07:26

问题


Well, I started a migration to AndroidX from a year old project (untouched since then) and I have problems withs the resources and the gradle build. Im completely lost with the new namespaces, I changed some of them, I upgraded all the things AndroidStudio told me, but still not recognizing things on my project. I will paste both gradle here and Ill put the error below.

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {

    repositories {
        google()
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Module

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    buildToolsVersion "28.0.3"
    defaultConfig {
        applicationId "com.example.usuario.tm"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test:runner:1.1.0-alpha3"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0-alpha3', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })
        implementation 'androidx.appcompat:appcompat:1.0.0'
        implementation 'com.android.support.constraint:constraint-layout:1.1.3'
        implementation 'androidx.legacy:legacy-support-v4:1.0.0'
        implementation 'com.google.android.material:material:1.0.0'
        implementation 'com.google.gms:google-services:4.1.0'
        implementation 'com.google.android.gms:play-services-auth:16.0.1'
        testImplementation 'junit:junit:4.12'
    }
}

apply plugin: 'com.android.application'
apply plugin: 'maven'

Error

Android resource linking failed
Output:  C:\Users\Xrless\Desktop\Programing\TN\TM\app\src\main\res\layout\activity_principal.xml:34: error: attribute android:style not found.
error: failed linking file resources.

Command: C:\Users\Xrless\.gradle\caches\transforms-1\files-1.1\aapt2-3.2.1-4818971-windows.jar\cf456f090f0725907522fb6d2bec3322\aapt2-3.2.1-4818971-windows\aapt2.exe link -I\
        C:\Users\Xrless\AppData\Local\Android\Sdk\platforms\android-28\android.jar\
        --manifest\
        C:\Users\Xrless\Desktop\Programing\TN\TM\app\build\intermediates\merged_manifests\debug\processDebugManifest\merged\AndroidManifest.xml\
        -o\
        C:\Users\Xrless\Desktop\Programing\TN\TM\app\build\intermediates\processed_res\debug\processDebugResources\out\resources-debug.ap_\
        -R\
        @C:\Users\Xrless\Desktop\Programing\TN\TM\app\build\intermediates\incremental\processDebugResources\resources-list-for-resources-debug.ap_.txt\
        --auto-add-overlay\
        --java\
        C:\Users\Xrless\Desktop\Programing\TN\TM\app\build\generated\not_namespaced_r_class_sources\debug\processDebugResources\r\
        --custom-package\
        com.example.usuario.tm\
        -0\
        apk\
        --output-text-symbols\
        C:\Users\Xrless\Desktop\Programing\TN\TM\app\build\intermediates\symbols\debug\R.txt\
        --no-version-vectors
Daemon:  AAPT2 aapt2-3.2.1-4818971-windows Daemon #0

回答1:


There is an error in your activity_principal.xml. Try to change:

android:style

to

style

Also you need to add

android.useAndroidX=true

to your gradle.properties file if you want to use androidX library instead of Support Library.




回答2:


In AndroidStudio 3.2+ you can use Refactor | Migrate to AndroidX to upgrade a project. For more go here.




回答3:


I spent an entire night trying to solve this issue after the migration to androidX. Below are the few things I did for it to work:

In gradle.property:

dex.force.jumbo=true
android.useAndroidX=true
android.enableJetifier=true
android.enableD8=true
android.enableR8.libraries = false
android.enableR8 = false

In build.gradle:

defaultConfig {
    ...
    compileSdkVersion 28
    …
}
//Use JDK1.8 and make sure the same is well specified in your gradle file.
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
//Exclude Guava libraries
 ...
configurations {
    ...
    implementation.exclude group: 'com.google.guava'
}

//Make sure your firebase and google libraries are all updated
   ...
   implementation 'com.google.guava:guava:27.0.1-android'
   ...

After all these, few usual things:

  • Make sure you update your gradle to the latest version (3.4.2 as of now)
  • the previous action will require android Studio version 3.4 and above
  • Clear your Gradle caches(use this command if on macOs rm -rf $HOME/.gradle/caches/)
  • Invalidate caches and restart, ...
  • The rest will be just to change few implementations that were not refactored properly.

Think all should be find after that. Hope it saves you some time.




回答4:


  1. implementation androidx.appcompat:appcompat:1.0.0-alpha1
  2. implementation androidx.constraintlayout:constraintlayout:1.1.2



回答5:


Nice, I changed both implementations and added the androix=true to the property file. The build gradle thing is not throwing errors anymore (I think) But my layouts are not visible on the design view

IMAGE OF THE PROBLEM

And this:

IMAGE OF THE PROBLEM 2




回答6:


open gralde.properties file add below line

android.useAndroidX = true

Sync project

It works perfectly.




回答7:


Although the accepted answer is correct. But to help other guys, In complex projects, we found such kind of many errors which we need to fix manually. I have written a full article on the topic, especially the manual fixes. Ultimate Guide to Migrate Android Project to AndroidX.



来源:https://stackoverflow.com/questions/52960640/migration-to-androidx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!