迁移到AndroidX

10

好的,我从一个一年前就没有更改的项目开始迁移到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

https://dev59.com/f7Xna4cB1Zd3GeqPIk-m#57198973 - Sumit Shukla
7个回答

3

您的activity_principal.xml中存在一个错误。请尝试更改:

android:style

style

此外,如果您想使用AndroidX库而非Support Library,请在gradle.properties文件中添加

android.useAndroidX=true

即可。


1
我已经写了一篇关于这个主题的完整文章,特别是手动修复方面,希望能进一步阐述。https://handyopinion.com/ultimate-guide-to-migrate-android-project-to-androidx/ - Asad Ali Choudhry

1
在AndroidStudio 3.2+中,您可以使用“重构|迁移至AndroidX”来升级项目。更多信息请参见此处

1
我花了整整一晚上的时间来解决迁移到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/
  • 使缓存失效并重启...
  • 剩下的就是更改一些未正确重构的实现。

我想这些应该足够了。希望能为您节省一些时间。


0
  1. 引入 androidx.appcompat:appcompat:1.0.0-alpha1 库
  2. 引入 androidx.constraintlayout:constraintlayout:1.1.2 库

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

enter image description here

还有这个:

问题2的图片

enter image description here


你需要手动更新布局引用,并且小部件将引用旧的支持库。你需要在每个布局中更新所有的引用。 - portfoliobuilder

0

打开 gradle.properties 文件,在下面添加以下内容

android.useAndroidX = true

同步项目

它完美地工作。


0

虽然被接受的答案是正确的。但为了帮助其他人,在复杂的项目中,我们发现有许多这样的错误需要手动修复。我已经写了一篇关于这个主题的完整文章,特别是手动修复方面。Android项目迁移到AndroidX的终极指南


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接