迁移到 AndroidX

Migration to AndroidX

好吧,我开始从一年前的项目(从那时起就没有动过)迁移到 AndroidX,但我在资源和 gradle 构建方面遇到了问题。我完全迷失了新的命名空间,我改变了其中的一些,我升级了 AndroidStudio 告诉我的所有东西,但仍然无法识别我项目中的东西。我将在此处粘贴 gradle,并将错误放在下方。

// 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
}

模块

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'

错误

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

您的 activity_principal.xml 有误。尝试改变:

android:style

style

您还需要添加

android.useAndroidX=true

如果您想使用 androidX 库而不是支持库,请添加到您的 gradle.properties 文件。

  1. 实施androidx.appcompat:appcompat:1.0.0-alpha1
  2. 实施androidx.constraintlayout:constraintlayout:1.1.2

很好,我更改了两个实现并将 androix=true 添加到 属性 文件中。 构建 gradle 不再抛出错误(我认为)但是我的布局在设计视图中不可见

问题的图像

还有这个:

问题 2 的图像

在 AndroidStudio 3.2+ 中你可以使用 Refactor |迁移到 AndroidX 以升级项目。 如需更多信息,请前往 here.

迁移到 androidX 后,我花了一整夜试图解决这个问题。以下是我为它工作所做的几件事:

在gradle.property:

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

在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'
   ...

说完这些,再说几件平常的事情:

  • 确保将 gradle 更新到最新版本(目前为 3.4.2)
  • 之前的操作需要 android Studio 3.4 及以上版本
  • 清除您的 Gradle 缓存(如果在 macOs rm -rf $HOME/.gradle/caches/ 上使用此命令)
  • 使缓存无效并重新启动,...
  • 剩下的只是更改一些未正确重构的实现。

认为之后应该都找到了。希望它能为您节省一些时间。

打开 gralde.properties 文件在行下方添加

android.useAndroidX = true

同步项目

完美运行。

尽管接受的答案是正确的。但是为了帮助其他人,在复杂的项目中,我们发现了很多需要手动修复的错误。我已经写了一篇关于该主题的完整文章,尤其是手动修复。 Ultimate Guide to Migrate Android Project to AndroidX.