从支持库到依赖关系的AndroidX重构

时间:2018-08-06 10:23:18

标签: android androidx

我想将我的应用程序从使用Android支持库迁移到AndroidX组件。

这是我的build.gradle

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.test.myapplication"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    /*
    implementation "com.android.support:appcompat-v7:28.0.0-beta01"
    implementation "com.android.support:design:28.0.0-beta01"
    implementation "com.android.support:preference-v7:28.0.0-beta01"
    implementation "com.android.support:cardview-v7:28.0.0-beta01"
    implementation "com.android.support.constraint:constraint-layout:1.1.2"
    */
    implementation "androidx.appcompat:appcompat:1.0.0-beta01"
    implementation "com.google.android.material:material:1.0.0-beta01"
    implementation "androidx.preference:preference:1.0.0-beta01"
    implementation "androidx.cardview:cardview:1.0.0-beta01"
    implementation "androidx.constraintlayout:constraintlayout:1.1.2"
    implementation "androidx.legacy:legacy-support-v4:1.0.0-beta01"

    implementation 'com.dmitrymalkovich.android:progress-fab:1.6'
    implementation 'cat.ereza:customactivityoncrash:2.2.0'

}

在“依赖项”部分中,您可以看到旧的依赖项作为注释。 使用AndroidX依赖项,在构建应用程序时出现以下错误:

AGPBI: {"kind":"error","text":"error: duplicate value for resource \u0027attr/behavior_peekHeight\u0027 with config \u0027\u0027.","sources":[{"file":"/Users/plinzen/.gradle/caches/transforms-1/files-1.1/appcompat-1.0.0-beta01.aar/5f1c73d5e36b65b3011573345597f31d/res/values/values.xml","position":{"startLine":1303,"startColumn":4,"startOffset":70911,"endColumn":68,"endOffset":70975}}],"original":"","tool":"AAPT"}
AGPBI: {"kind":"error","text":"error: resource previously defined here.","sources":[{"file":"/Users/plinzen/.gradle/caches/transforms-1/files-1.1/appcompat-1.0.0-beta01.aar/5f1c73d5e36b65b3011573345597f31d/res/values/values.xml","position":{"startLine":1303,"startColumn":4,"startOffset":70911,"endColumn":68,"endOffset":70975}}],"original":"","tool":"AAPT"}

我认为这可能与我的其他两个依赖项有某种冲突,因为它们都仍在使用Android支持库,例如./gradlew app:dependencies状态:

implementation 'com.dmitrymalkovich.android:progress-fab:1.6'
implementation 'cat.ereza:customactivityoncrash:2.2.0'



+--- com.dmitrymalkovich.android:progress-fab:1.6
|    \--- com.android.support:design:23.3.0
|         +--- com.android.support:appcompat-v7:23.3.0 -> 27.0.1
|         |    +--- com.android.support:support-annotations:27.0.1
|         |    +--- com.android.support:support-core-utils:27.0.1
|         |    |    +--- com.android.support:support-annotations:27.0.1
|         |    |    \--- com.android.support:support-compat:27.0.1
|         |    |         +--- com.android.support:support-annotations:27.0.1
|         |    |         \--- android.arch.lifecycle:runtime:1.0.0
|         |    |              +--- android.arch.lifecycle:common:1.0.0
|         |    |              \--- android.arch.core:common:1.0.0
|         |    +--- com.android.support:support-fragment:27.0.1
|         |    |    +--- com.android.support:support-compat:27.0.1 (*)
|         |    |    +--- com.android.support:support-core-ui:27.0.1
|         |    |    |    +--- com.android.support:support-annotations:27.0.1
|         |    |    |    \--- com.android.support:support-compat:27.0.1 (*)
|         |    |    +--- com.android.support:support-core-utils:27.0.1 (*)
|         |    |    \--- com.android.support:support-annotations:27.0.1
|         |    +--- com.android.support:support-vector-drawable:27.0.1
|         |    |    +--- com.android.support:support-annotations:27.0.1
|         |    |    \--- com.android.support:support-compat:27.0.1 (*)
|         |    \--- com.android.support:animated-vector-drawable:27.0.1
|         |         +--- com.android.support:support-vector-drawable:27.0.1 (*)
|         |         \--- com.android.support:support-core-ui:27.0.1 (*)
|         +--- com.android.support:support-v4:23.3.0
|         |    \--- com.android.support:support-annotations:23.3.0 -> 27.0.1
|         \--- com.android.support:recyclerview-v7:23.3.0
|              +--- com.android.support:support-v4:23.3.0 (*)
|              \--- com.android.support:support-annotations:23.3.0 -> 27.0.1
\--- cat.ereza:customactivityoncrash:2.2.0
     \--- com.android.support:appcompat-v7:27.0.1 (*)

有没有办法解决我的问题?

我可以简单地在我的build.gradle中排除(子)依赖项吗?我有些疑问,因为AndroidX和Android支持之间的程序包名称不同。

编辑:我按照Commonsware的建议在项目gradle.properties中启用了Jetifier,但仍然遇到相同的错误:

android.useAndroidX=true
android.enableJetifier=true

1 个答案:

答案 0 :(得分:2)

我不知道AndroidX要求最低版本为Android Studio 3.2。我仍在Android Studio 3.1上工作,这就是Jetifier无法正常工作的原因。之后,我的代码就会编译。