还在用android.support?该考虑迁移AndroidX了!

9,638 阅读5分钟

AndroidX是Google 2018 IO 大会推出的新扩展库,主要是对Android 支持库做了重大改进。与支持库一样,AndroidX 与 Android 操作系统分开提供,并与各个 Android 版本向后兼容,可以说AndroidX就是为了替换Android支持库而设计的。

AndroidX是什么?

  • AndroidX 是 Android 团队用于在 Jetpack 中开发、测试、打包和发布库以及对其进行版本控制的开源项目。[摘自官方]
  • AndroidX完全取代了支持库,不仅提供同等的功能,而且提供了新的库。
  • AndroidX 会将原始支持库 API 软件包映射到 androidx 命名空间。只有软件包和 Maven 工件名称发生了变化;类、方法和字段名称没有改变。
  • 与支持库不同,AndroidX 软件包会单独维护和更新。androidx 软件包使用严格的语义版本控制,从版本 1.0.0 开始,可以单独更新项目中的 AndroidX 库。
  • 所有新支持库的开发工作都将在 AndroidX 库中进行,这包括维护原始支持库工件和引入新的 Jetpack 组件。

AndroidX的变化

1.常见依赖库映射

旧编译工件 AndroidX 编译工件
com.android.support.constraint:constraint-layout androidx.constraintlayout:constraintlayout:1.1.2
com.android.support:appcompat-v7 androidx.appcompat:appcompat:1.0.0
com.android.support:cardview-v7 androidx.cardview:cardview:1.0.0
com.android.support:coordinatorlayout androidx.coordinatorlayout:coordinatorlayout:1.0.0
com.android.support:design com.google.android.material:material:1.0.0-rc01
com.android.support:drawerlayout androidx.drawerlayout:drawerlayout:1.0.0
com.android.support:gridlayout-v7 androidx.gridlayout:gridlayout:1.0.0
com.android.support:media2 androidx.media2:media2:1.0.0-alpha03
com.android.support:multidex androidx.multidex:multidex:2.0.0
com.android.support:percent androidx.percentlayout:percentlayout:1.0.0
com.android.support:recyclerview-v7 androidx.recyclerview:recyclerview:1.0.0
com.android.support:support-annotations androidx.annotation:annotation:1.0.0
com.android.support:support-compat androidx.core:core:1.0.0
com.android.support:support-fragment androidx.fragment:fragment:1.0.0
com.android.support:support-v4 androidx.legacy:legacy-support-v4:1.0.0
com.android.support:viewpager androidx.viewpager:viewpager:1.0.0
com.android.support:swiperefreshlayout androidx.swiperefreshlayout:swiperefreshlayout:1.0.0

更多详细依赖库变化,可查阅官方文档或下载这些映射的 CSV 格式文件。

2.常见类映射

支持库类 AndroidX 类
android.arch.lifecycle.Lifecycle androidx.lifecycle.Lifecycle
android.support.v4.app.Fragment androidx.fragment.app.Fragment
android.support.v4.app.FragmentActivity androidx.fragment.app.FragmentActivity
android.support.v7.app.AppCompatActivity androidx.appcompat.app.AppCompatActivity
android.support.v7.app.ActionBar androidx.appcompat.app.ActionBar
android.support.v7.widget.RecyclerView androidx.recyclerview.widget.RecyclerView
android.support.design.card.MaterialCardView com.google.android.material.card.MaterialCardView
android.support.design.ripple.RippleUtils com.google.android.material.ripple.RippleUtils
android.support.design.widget.CoordinatorLayout androidx.coordinatorlayout.widget.CoordinatorLayout
android.support.design.widget.NavigationView com.google.android.material.navigation.NavigationView
android.support.percent.PercentFrameLayout androidx.percentlayout.widget.PercentFrameLayout

更多详细支持类映射变化,可查阅官方文档或下载这些映射的 CSV 格式文件。

为什么要迁移AndroidX?

下面是Google官方描述

Existing packages, such as the Android Support Library, are being refactored into AndroidX.
Although Support Library versions 27 and lower are still available on Google Maven,
all new development will be included in only AndroidX versions 1.0.0 and higher.
  • 大致意思是:现有的软件包,如Android支持库,正在被重构为Androidx。尽管在Google Maven上仍然提供支持库版本27及更低版本,但所有新开发将只包含在Androidx 1.0.0及更高版本中。

AndroidX迁移步骤?

1.更新Android Studio与Gradle版本

  • 将Android studio升级到 3.2及以上;
  • Gradle 插件版本改为4.6及以上;
  • compileSdkVersion 版本升级到 28及以上;
  • buildToolsVersion 版本改为 28.0.2及以上。

2.迁移AndroidX配置

  • 在项目的gradle.properties文件里添加如下配置:
android.useAndroidX=true
android.enableJetifier=true
配置 说明
android.useAndroidX=true 表示当前项目启用 androidx
android.enableJetifier=true 表示将依赖包也迁移到androidx

备注:enableJetifier如果取值为false,表示不迁移依赖包到androidx,但在使用依赖包中的内容时可能会出现问题,当然了,如果你的项目中没有使用任何三方依赖,那么,此项可以设置为false。

3.修改依赖库
修改项目app目录下的build.gradle依赖库,具体可以参照AndroidX变化中的依赖库映射。

修改前 修改后
implementation 'com.android.support:appcompat-v7:28.0.2' implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'com.android.support:design:28.0.2' implementation 'com.google.android.material:material:1.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.2' implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
... ...

4.依赖类重新导包
将原来import的android.**包删除,重新import新的androidx.**包

import android.support.v7.app.AppCompatActivity; → import androidx.appcompat.app.AppCompatActivity;

5.一键迁移AndroidX库
AS 3.2 及以上版本提供了更加方便快捷的方法一键迁移到 AndroidX。选择菜单上的ReFactor —— Migrate to AndroidX... 即可。(如果迁移失败,就需要重复上面1,2,3,4步手动去修改迁移)

备注:如果你的项目compileSdkVersion 低于28,点击Refactor to AndroidX...会提示:

Q&A

  • 同一个项目中Android Support和AndroidX可以共存吗?
不可以共存。需要将依赖修改为Android Suppor或AndroidX中任一种。
  • 执行Migrate to AndroidX之后就完成AndroidX迁移了?
不一定。部分控件的包名/路径名转换的有问题,所以还需要我们手动调整(包括修改xml布局文件和.java/.kt 文件)。
  • DataBinding中的错误(重名id错误)?
在 AndroidStudio3.2 + androidx 环境下,对错误的检查和处理更为严格。如果同一个xml布局文件中存在同名id,
在之前的版本中,我们可以正常编译和运行,但是,在新的环境下, 必然会报错,错误信息如下:

  • attr.xml 中重复的属性名称会报错?
在迁移到 androidX 之前,我们为自定义控件编写自定义属性时,可以与android已有的属性重名,
但是,在AndroidX环境下不行了,如果存在重名的情况,必然会报错——会提示你重复定义(详细错
误信息没截图,但翻译过来就是重复定义了attr/xxx)。
  • 错误示例
<declare-styleable name="RoundImageView">
    ...
    <!-在迁移到androidx之前,这样写虽然不规范,但是能用,不报错->
    <attr name="textSize" format="Integer" />
    ...
</declare-styleable>
  • 正确示例
<declare-styleable name="RoundImageView">
    ...
    <!-迁移到androidX之后,必须使用android:xxx 属性,不能定义android已有的属性->
    <attr name="android:textSize" />
    ...    
</declare-styleable>
  • Glide中的注解不兼容androidX?
    迁移到 androidX 之后,Glide中使用的 android.support.annotation.CheckResult 和 android.support.annotation.NonNull这两个注解无法迁移。之前有用户在Glide中提过issue: github.com/bumptech/gl…
    在上述issue 中有用户表示,将Glide升级到 4.8.0 之后,可以正常迁移。但是,我这边并不行。然后,我先升级了Glide ,又在 gralde文件中增加了support.annotation ,这样才能正常编译通过。貌似在后续Glide 5.x 版本中会完成对 androidx的完全兼容。

  • 规范包名(即文件夹名)?
    这里所说的包名,指的是项目中的文件夹名称。在之前版本中,我们命名包名时可能会出现大写字母,虽然这并不符合Java命名规范,但起码能正常编译和运行。然而,升级到 AndroidStudio3.2 + androidX 环境后,必须严格遵守命名规范,否则,可能报错,从而导致不能正常编译和运行。

参考: