android|快速 AndroidX 迁移

前言
旧版支持库版本繁琐的配置,想必折磨着不少开发者,Google 2018 IO 大会推出了 Android 新的扩展库 AndroidX,用于替换原来的 Android扩展库,将原来的android.替换成androidx.;只有包名和Maven工件名受到影响,原来的类名,方法名和字段名不会更改。接下来我们来看看使用 AndroidX的扩展库需要哪些配置。
1.升级Android Studio 升级Android Studio IDE 到 3.2 及以上(看到官方文档已经更新到 android studio 3.3了,这里用的3.2版本)
2.升级Gradle 插件 升级Gradle 插件版本改为 4.6及以上
【android|快速 AndroidX 迁移】android|快速 AndroidX 迁移
文章图片

可以直接复制黏贴到 gradle-wrapper 文件里面distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists

3. 修改build_tools版本,compileSdkVersion、target_sdk compileSdkVersion 版本升级到 28及以上;
buildToolsVersion 版本改为 28.0.2及以上。
4.参照 官方的依赖库新旧映射关系,一对一的进行依赖替换 官方依赖映射
下面是我自己项目中的依赖替换,如果你的项目也是这样定义依赖的,可以省去一些功夫
ext.deps = [:]def versions = [:] //Libraries versions.gsonVersion = '2.7' versions.glideVersion = '3.7.0' versions.okHttpVersion = '3.8.1' versions.eventbusAnnotationProcessorVersion = '3.0.1' versions.imagepickerVersion = '0.3.2' versions.volleyVersion = '1.0.0' versions.rxjavaVersion = '1.3.0'versions.packerVersion = "2.0.0" versions.greenDaoVersion = "3.2.0" versions.debugdbVersion = "1.0.0"//Testing versions.robolectricVersion = '3.0' versions.jUnitVersion = '4.12' versions.mockitoCoreVersion = '2.8.47' versions.mockitoAndroidVersion = '2.8.47'//Development versions.leakcanaryAndroidVersion = '1.5.1'//android x versions.annotationsVersion = '1.0.0' versions.recyclerViewV7Version = '1.0.0' versions.supportv4Version = '1.0.0' versions.supportv7Version = '1.0.0-alpha1' versions.supportcoreuiVersion = "1.0.0" versions.espressoVersion = '3.1.0' versions.cardViewVersion = '1.0.0' versions.constraintVersion = "1.1.2" versions.multidexVersion = "2.0.0"def deps = [:]def nplDependencies = [:] nplDependencies.gson = "com.google.code.gson:gson:$versions.gsonVersion" nplDependencies.eventbusannotation = "org.greenrobot:eventbus-annotation-processor:$versions.eventbusAnnotationProcessorVersion" nplDependencies.supportv7 = "androidx.appcompat:appcompat:$versions.supportv7Version" nplDependencies.annotations = "androidx.annotation:annotation:$versions.annotationsVersion" nplDependencies.cardview = "androidx.cardview:cardview:$versions.cardViewVersion" nplDependencies.supportV4 = "androidx.legacy:legacy-support-v4:$versions.supportv4Version" nplDependencies.recyclerview = "androidx.recyclerview:recyclerview:$versions.recyclerViewV7Version" nplDependencies.glide = "com.github.bumptech.glide:glide:$versions.glideVersion" nplDependencies.imagepicker = "com.lzy.widget:imagepicker:$versions.imagepickerVersion" nplDependencies.volley = "com.android.volley:volley:$versions.volleyVersion" nplDependencies.rxjava = "io.reactivex:rxjava:$versions.rxjavaVersion" nplDependencies.pkng = "com.mcxiaoke.packer-ng:helper:$versions.packerVersion" nplDependencies.greendao = "org.greenrobot:greendao:$versions.greenDaoVersion" nplDependencies.debugdb = "com.amitshekhar.android:debug-db:$versions.debugdbVersion" nplDependencies.constraint = "androidx.constraintlayout:constraintlayout:$versions.constraintVersion" nplDependencies.multidex = "androidx.multidex:multidex:$versions.multidexVersion" deps.nplDependencies = nplDependenciesdef nplTestDependencies = [:] nplTestDependencies.mockitoandroid = "org.mockito:mockito-android:$versions.mockitoAndroidVersion" nplTestDependencies.mockitocore = "org.mockito:mockito-core:$versions.mockitoCoreVersion" nplTestDependencies.junit4 = "junit:junit:$versions.jUnitVersion" nplTestDependencies.espresso = "androidx.test.espresso:espresso-core:$versions.espressoVersion" deps.nplTestDependencies = nplTestDependenciesdef developmentDependencies = [:] developmentDependencies.leakCanary = "com.squareup.leakcanary:leakcanary-android:$versions.leakcanaryAndroidVersion" deps.developmentDependencies = developmentDependenciesdef commDependencies = [:] commDependencies.supportcoreui = "androidx.legacy:legacy-support-core-ui:$versions.supportcoreuiVersion" deps.commDependencies = commDependenciesdef okgoDependencies = [:] okgoDependencies.okhttp = "com.squareup.okhttp3:okhttp:$versions.okHttpVersion" deps.okgoDependencies = okgoDependenciesdef recyclerviewDependencies = [:] recyclerviewDependencies.supportV7 = "androidx.appcompat:appcompat:$versions.supportv7Version" recyclerviewDependencies.recyclerview = "androidx.recyclerview:recyclerview:$versions.recyclerViewV7Version" deps.recyclerviewDependencies = recyclerviewDependenciesdef build_versions = [:] build_versions.min_sdk = 14 build_versions.target_sdk = 28 build_versions.build_tools = "28.0.3" ext.build_versions = build_versions ext.deps = depsdef addRepos(RepositoryHandler handler) { handler.google() handler.jcenter() handler.maven { url 'https://oss.sonatype.org/content/repositories/snapshots' } }ext.addRepos = this.&addRepos

5.替换类中依赖
批量进行依赖替换
二 一键迁移AndroidX库
AS 3.2 及以上版本提供了更加方便快捷的方法一键迁移到AndroidX。选择菜单上的ReFactor —— Migrate to AndroidX…即可。(如果迁移失败,就需要重复上面1,2,3,4步手动去修改迁移)
android|快速 AndroidX 迁移
文章图片

我自己测试的没有按照预期迁移。
注意:
如果您使用的是Android KTX模块,请务必更新您的依赖项以避免迁移问题。有关每个模块的最新版本,请参阅Android KTX
总结
android x 替换旧的根据版本号依赖,更大限度方便到开发者。减少了维护成本,将精力用到coding上。按照文档一步步的调整依赖库,clean project,rebuild project。xml里面的依赖同样需要替换。

引用 https://www.jianshu.com/p/7dc111353328 Android AndroidX的迁移
https://developer.android.google.cn/jetpack/androidx/migrate 官方文档

    推荐阅读