You do not intend to try Android X do?

Disclaimer: This article is a blogger original article, follow the CC 4.0 by-sa copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/androidokk/article/details/96432559

AndroidX is Google 2018 IO Assembly's new extensions, mainly for Android support library made significant improvements. Like with the support library, AndroidX with the Android operating system separately, and backward compatibility with the various versions of Android, it can be said AndroidX is to replace Android support library designed.
Here Insert Picture Description

What AndroidX that?

AndroidX is the Android team for the development of the Jetpack, the test, package, and publish its version control libraries and open source projects. [Excerpt from the official]
AndroidX completely replaced the support library not only provides the same functionality, but also provides a new library.
AndroidX original will support library API packages mapped to androidx namespace. Only the work packages and Maven name has changed; class, method, and field names are not changed.
And support different libraries, AndroidX package will separately maintain and update. androidx packages use strict semantic version control, starting with version 1.0.0, you can update the project AndroidX library alone.
All new development will support libraries in AndroidX library, which includes maintenance of the original workpiece support libraries and the introduction of new Jetpack components.

The change AndroidX

1. Common rely library mapping
Here Insert Picture Description
2. Common class mapping
Here Insert Picture Description

Why move AndroidX?

Here is the official description of Google

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 android.enableJetifier=true
说明 表示当前项目启用 androidx 表示将依赖包也迁移到androidx
备注:enableJetifier如果取值为false,表示不迁移依赖包到androidx,但在使用依赖包中的内容时可能会出现问题,当然了,如果你的项目中没有使用任何三方依赖,那么,此项可以设置为false。

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

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

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

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 in the notes is not compatible androidX?
After migration to androidX, android.support.annotation.CheckResult and android.support.annotation.NonNull Glide used in these two notes can not be migrated. Before users issue mentioned in Glide in: github.com/bumptech/gl...
A user that, after upgrading to 4.8.0 Glide, normal migration in the above issue. However, my side is not OK. Then, I will upgrade Glide, added support.annotation in gralde file, so as to properly compile. Seemingly in subsequent Glide 5.x version will be fully compatible with the completion of the androidx.

Specification package name (ie folder name)?
Here is the package name, referring to the project folder name. In previous versions, we name may appear in capital letters when the package name, although it does not conform to Java naming conventions, but at least be able to compile and run properly. However, after upgrading to AndroidStudio3.2 + androidX environment, we must strictly abide by the naming convention, otherwise, it may be an error, resulting in a failure to compile and run properly.

Guess you like

Origin blog.csdn.net/androidokk/article/details/96432559