Migrate to AndroidX 遇到的坑

Androidx 遷移方法:

首先把 gradle 版本改爲3.2.0以上,以及 compileSdkVersion 爲28以上

然後 Android Studio 菜單欄 Refactor -> Migrate to AndroidX

如果是新項目,使用AndroidX相關依賴,可以在gradle.properties文件裏添加配置:

android.useAndroidX=true
android.enableJetifier=true

如果你只是想使用AndroidX,但是之前的不遷移,可以這樣配置:

android.useAndroidX=true
android.enableJetifier=false

遷移完成後運行報錯。。。

Conflict with dependency 'androidx.lifecycle:lifecycle-runtime' in project ':app'. Resolved versions for runtime classpath (2.0.0-rc01) and compile classpath (2.0.0) differ. This can lead to runtime crashes. To resolve this issue follow advice at https://developer.android.com/studio/build/gradle-tips#configure-project-wide-properties. Alternatively, you can try to fix the problem by adding this snippet to ...\...\..\..\build.gradle:
​

根據提示解決:

subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
​
    project.configurations.all {
        resolutionStrategy.eachDependency { details ->
            if (details.requested.group == 'com.android.support'
                    && !details.requested.name.contains('multidex') ) {
                details.useVersion "27.1.1"
            }else if(details.requested.group == 'androidx.lifecycle')
            {
                details.useVersion "2.0.0-rc01"
            }else if(details.requested.group == 'androidx.versionedparcelable')
            {
                details.useVersion "1.0.0-rc01"
            }else if(details.requested.group == 'androidx.core')
            {
                details.useVersion "1.0.0-rc01"
            }
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章