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

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

AndroidX是什么?

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

AndroidX的变化

1.常见依赖库映射android

旧编译工件 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 格式文件。git

2.常见类映射github

支持库类 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 格式文件。bash

为何要迁移AndroidX?

下面是Google官方描述app

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版本ide

  • 将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变化中的依赖库映射。gradle

修改前 修改后
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 环境后,必须严格遵照命名规范,不然,可能报错,从而致使不能正常编译和运行。

参考:

相关文章
相关标签/搜索