Android开发Android开发经验谈Android技术知识

还在用 android.support?谷歌强推 Android

2020-06-08  本文已影响0人  jett老师

前言

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

1. AndroidX 是什么?

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

更多详细依赖库变化,可查阅官方文档(https://developer.android.com/jetpack/androidx/migrate#artifact_mappings)或下载这些映射的 CSV 格式(https://developer.android.com* /topic/libraries/support-library/downloads/androidx-artifact-mapping.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

更多详细支持类映射变化,可查阅官方文档(https://developer.android.com/jetpack/androidx/migrate#artifact_mappings)或下载这些映射的CSV 格式(https://developer.android.com* /topic/libraries/support-library/downloads/androidx-class-mapping.csv*)文件。

2. 为什么要迁移 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.

3. AndroidX 迁移步骤?

1. 更新 Android Studio 与 Gradle 版本

2. 迁移 AndroidX 配置

android.useAndroidX=trueandroid.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 Suppor或AndroidX中任一种。
不一定。部分控件的包名/路径名转换的有问题,所以还需要我们手动调整(包括修改xml布局文件和.java/.kt 文件)。
在 AndroidStudio3.2 + androidx 环境下,对错误的检查和处理更为严格。如果同一个xml布局文件中存在同名id,
在之前的版本中,我们可以正常编译和运行,但是,在新的环境下, 必然会报错,错误信息如下:
在迁移到 androidX 之前,我们为自定义控件编写自定义属性时,可以与android已有的属性重名,
但是,在AndroidX环境下不行了,如果存在重名的情况,必然会报错——会提示你重复定义。
<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>

学习分享

最近有朋友在我面前炫耀,自己在阿里内部得到了一份多牛批的学习资料,对于他这种只炫耀不分享的人,我表示不就是一份资料吗?我自己整理的比你更加系统,全面谁不会一样!在这里我不小气,免费分享给大家。学无止尽,祝大家都能找到自己满意的工作。

点击【Android高级工程师进阶学习】加入我们的圈子领取资料和我们一起吧学习交流吧!

Android进阶学习全套手册

Android高级架构师进阶知识体系图

Android对标阿里P7学习视频

BATJ大厂Android高频面试题

上一篇 下一篇

猜你喜欢

热点阅读