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