美文网首页
AndroidX 迁移

AndroidX 迁移

作者: JustDo23 | 来源:发表于2019-11-18 18:06 被阅读0次
AndroidX.png

引言:Android Support Library Exit History.

作者:JustDo23

时间:2019年09月18日

官网:https://developer.android.google.cn/jetpack/androidx

01. 简单概览

  • AndroidXJetPack 中与操作系统 解除捆绑 并且 向后兼容 的开源项目。
  • AndroidX 完全取代 Support 并提供新的功能及特性。
  • 所有 Support 有关旧类 完整映射AndroidX 中。
  • AndroidX 使用严格的 语义版本控制 并可以进行 单独更新
  • 语义版本控制

02. 初步使用

  • 需要设置 compileSdkVersion28 及以上
  • gradle.properties 文件中进行配置
# 是否指定使用 AndroidX
android.useAndroidX=true
# 是否将第三方依赖转换为 AndroidX
android.enableJetifier=true
  • useAndroidXtrue 则 Android 插件 会自动使用相应的 AndroidX 而非 Support
  • enableJetifiertrue 则 Android 插件 会重写第三方库的 二进制 文件,自动迁移 现有的第三方库 以使用 AndroidX

03. 项目迁移

  • 使用 Android Studio 3.2 及更高版本
  • 菜单栏依次选择 Refactor > Migrate to AndroidX
  • 弹窗提示是否进行 项目备份
  • 指定备份路径或者跳过备份
  • 自动进行 项目扫描 并在 Find 中提示 所有引用
  • 点击 Do Refactor 进行迁移
  • 注意:这里并不是结束,有可能还有很多包名替换是错误的,需要手动调整

04. Glide

dependencies {
    implementation 'com.github.bumptech.glide:glide:4.9.0'// Glide
    annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'// Glide
}

如上配置,在 编译时 仍旧会报错,自动生成的文件总是引用 android.support.annotation.CheckResultSupport 注解包内的类。

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.50"// Kotlin

dependencies {
    implementation 'com.github.bumptech.glide:glide:4.9.0'// Glide
    kapt 'com.github.bumptech.glide:compiler:4.9.0'// Glide
}

如上解决,项目原本没有引入 Kotlin 在引入之后使用 kapt 替换注解编译器,问题解决。

05. FileProvider

<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}.fileProvider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>

如上结果,迁移 AndroidX 只需要修改 provider 节点下的 android:name 其余配置不变。

06. 迁移检查

  • 执行指令来检查依赖关系
# 只查看 release 的依赖关系
$ ./gradlew app:dependencies --configuration releaseCompileClasspath
  • 快捷键 Command + Shift + F 全局搜索 android.support
  • 运行程序隐藏的编译错误

07. 个人经验

  • 第一个项目本身引入了 Kotlin 在自动迁移之后需要手动修改很多错误的包名
  • 第二个项目没有引入过 Kotlin 在自动迁移之后包名基本全部正确替换
  • 可以尝试多次进行自动迁移操作以达到正确替换包名的目的

08. 包名替换

  • 快捷键 Command + Shift + R 进行全局搜索替换
问题包名 新版包名
android.support.annotation.NonNull androidx.annotation.NonNull
android.support.annotation.Nullable androidx.annotation.Nullable
android.support.constraint.ConstraintLayout androidx.constraintlayout.widget.ConstraintLayout
android.support.v4.widget.NestedScrollView androidx.core.widget.NestedScrollView
android.support.v7.widget.RecyclerView androidx.recyclerview.widget.RecyclerView
android.support.v7.widget.LinearLayoutManager androidx.recyclerview.widget.LinearLayoutManager
android.support.constraint.Guideline androidx.constraintlayout.widget.Guideline
android.support.v7.widget.CardView androidx.cardview.widget.CardView
androidx.core.view.ViewPager androidx.viewpager.widget.ViewPager
android.support.v4.view.PagerAdapter androidx.viewpager.widget.PagerAdapter
android.support.v4.app.FragmentManager androidx.fragment.app.FragmentManager
android.support.v4.app.FragmentTransaction androidx.fragment.app.FragmentTransaction
android.support.v7.app.AppCompatDialog androidx.appcompat.app.AppCompatDialog

09. 拓展阅读

相关文章

  • 是时候迁移至androidX--Google官方视频笔记

    视频连接:迁移AndroidX 1.为什么需要迁移到AndroidX Support Library 28.0是最...

  • AndroidX迁移小记

    AndroidX迁移小记 为什么要迁移AndroidX?因为Android 现有的软件包,如Android支持库,...

  • 笔记

    Android Studio升级到3.4后的两个小坑 AndroidX迁移 1.AndroidX迁移 2....

  • AndroidX - Program type already

    最近在将项目迁移到Androidx的时候,通过Refactor - > Migrate to AndroidX,然...

  • AndroidX 迁移

    title: androidx迁移date: 2019-10-15 11:49:20tags: Androidx ...

  • AndroidX迁移和方法

    迁移到 AndroidX AndroidX 将原始支持库 API 替换为 androidx 命名空间中的软件包。只...

  • 迁移AndroidX

    迁移AndroidX https://developer.android.google.cn/jetpack/an...

  • 迁移AndroidX!!!

    参考链接本次迁移主要参考上面谷歌官方公众号文章,迁移背景建议点进文章进行查看,本文主要提及一些迁移过程和其中遇到的...

  • 迁移AndroidX

    1. 前言 AndroidX replaces the original support library APIs...

  • AndroidX迁移

    什么是 AndroidX? Google 2018 IO 大会推出了 Android新的扩展库 AndroidX,...

网友评论

      本文标题:AndroidX 迁移

      本文链接:https://www.haomeiwen.com/subject/bbbbictx.html