Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱
MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina.com

目录

目录
AndroidX
AndroidX 的变化
常用依赖库对比
常用支持库类对比
官方文档介绍
迁移到 AndroidX
后续问题处理
手动修改错误包名
修复DataBinding中的重名id错误
去除 attr.xml 中重复的属性名称
Glide中的注解不兼容androidX
规范包名
修改未自动迁移的三方库
莫名问题的解决

AndroidX

官方文档
所有 AndroidX 引用库
API文档
androidx-class-mapping.csv
androidx-artifact-mapping.csv

Google 2018 IO 大会推出了扩展库 AndroidX,AndroidX 是对 android.support.xxx 包的整理后产物。由于之前的support包过于混乱,所以,google推出了 androidX 用于替换support包。

其只是将原来的android.*替换成androidx.*,只有包名和Maven工件名受到影响,原来的类名、方法名和字段名不会更改。

由于在后续版本中,会逐步放弃对 support 的升级和维护,所以,我们必须尽快迁移到 androidX。

Jetpack components are part of the AndroidX library. Learn more about components on the Jetpack home page.

AndroidX 的变化

常用依赖库对比

详细变化内容,可以下载CSV格式映射文件:androidx-class-mapping.csv

Old build artifact AndroidX build artifact
com.android.support:appcompat-v7:28.0.2 androidx.appcompat:appcompat:1.0.0
com.android.support:design:28.0.2 com.google.android.material:material:1.0.0
com.android.support:support-v4:28.0.2 androidx.legacy:legacy-support-v4:1.0.0
com.android.support:recyclerview-v7:28.0.2 androidx.recyclerview:recyclerview:1.0.0
com.android.support.constraint:constraint-layout:1.1.2 androidx.constraintlayout:constraintlayout:1.1.2

常用支持库类对比

详细变化内容,可以下载CSV格式映射文件:androidx-artifact-mapping.csv

Support Library class AndroidX class
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

官方文档介绍

AndroidX is the open-source project that the Android team uses to develop, test, package, version and release libraries within Jetpack.

AndroidX是Android团队用于在Jetpack中开发,测试,打包,发布和发布库的开源项目。

AndroidX is a major improvement to the original Android Support Library. Like the Support Library, AndroidX ships separately from the Android OS and provides backwards-compatibility across Android releases. AndroidX fully replaces the Support Library by providing feature parity and new libraries. In addition AndroidX includes the following features:

AndroidX是对原始Android支持库的重大改进。与支持库一样,AndroidX与Android操作系统分开提供,并提供跨Android版本的向后兼容性。 AndroidX通过提供功能奇偶校验和新库完全取代了支持库。此外,AndroidX还包括以下功能:

  • All packages in AndroidX live in a consistent namespace starting with the string androidx. The Support Library packages have been mapped into corresponding androidx.* packages. For a full mapping of all the old classes and build artifacts to the new ones, see the Package Refactoring page.

    AndroidX中的所有软件包都以字符串androidx开头,位于一致的命名空间中。支持库包已映射到相应的androidx。*包。有关所有旧类和构建工件的完整映射到新构件,请参阅“包重构”页面。

  • Unlike the Support Library, AndroidX packages are separately maintained and updated. The androidx packages use strict Semantic Versioning starting with version 1.0.0. You can update AndroidX libraries in your project independently.

    与支持库不同,AndroidX软件包是单独维护和更新的。 androidx软件包使用严格的语义版本控制,从版本1.0.0开始。您可以单独更新项目中的AndroidX库。

  • All new Support Library development will occur in the AndroidX library. This includes maintenance of the original Support Library artifacts and introduction of new Jetpack components.

    所有新的支持库开发都将在AndroidX库中进行。这包括维护原始支持库工件和引入新的Jetpack组件。


If you want to use AndroidX in a new project, you need to set the compile SDK to Android 9.0 (API level 28) or higher and set both of the following Android Gradle plugin flags to true in your gradle.properties file.

  • android.useAndroidX: When set to true, the Android plugin uses the appropriate AndroidX library instead of a Support Library. The flag is false by default if it is not specified.
  • android.enableJetifier: When set to true, the Android plugin automatically migrates existing third-party libraries to use AndroidX by rewriting their binaries. The flag is false by default if it is not specified.

迁移到 AndroidX

Migrating to AndroidX

1、修改 gradle.properties

android.useAndroidX=true
android.enableJetifier=true

其中:

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

2、将AS更新至 AS 3.2 及以上
将 Gradle 插件版本改为 4.6 及以上
将 compileSdkVersion、buildToolsVersion 版本改为 28.0.2及以上

3、在AndroidStudio 3.2 或更高版本中执行如下操作:

Refactor > Migrate to AndroidX

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

You need to have at least have compileSdk 28 set in your module build.gradle to refactor to androidx

在执行该操作时会提醒我们是否将当前项目打包备份。如果你提前已经做好了备份,可以忽略;如果没有备份,则先备份。

后续问题处理

参考文章
Android Support 库和 AndroidX 冲突问题

手动修改错误包名

由于 Migrate to AndroidX 执行之后,部分控件的包名/路径名转换的有问题,所以还需要我们手动调整(包括修改xml布局文件和.java/.kt文件)。

如:ViewPager、RecyclerView 等,这些内容在迁移完成之后,包名是 androidx.core.weight.xxxx,这是一个错误的包名,我们必须手动修改,否则,无法正常编译。点击绿色 Run(运行) 按钮时会详细报出哪里有错误。

注意,在 AndroidStudio 的 build 选项卡中一次最多只会报 50条错误!所以,可能在你修完第一批之后,后面还有N个50。此处要保持一个平静的 ?。

修复DataBinding中的重名id错误

在 AndroidStudio3.2 + androidx 环境下,对错误的检查和处理更为严格。如果同一个xml布局文件中存在同名id,在之前的版本中,我们可以正常编译和运行,但是,在新的环境下, 必然会报错,错误信息如下:

在上图的错误信息中,我们以 DecibelBinding 为例,简述修复过程。

  • 如上图,无法将xxxBinding 构造器中的xxxBinding应用到指定类型指明了出错的 Binding类 为 DecibelBinding

  • 按照DataBinding类名的生成规则,我们可以知道,DecibelBinding 对应的xml文件名应该是 decibel.xml (如果你在xml中通过 class="xxxBinding" 指定了DataBinding的生成类名,那么就全局搜索吧)

  • 在确定了xml之后,我们还需要知道到底哪里出了错误,那么,就继续看图中的 错误:找不到符号 符号:变量 xxx.这个变量就是控件的id名称。

  • DataBinding转换控件id名的规则是:去除下划线连接符,然后将原下划线后面的第一个字母大写。所以,图中的 fragmentDiscoverGridItemRelativeLayout1 对应的控件id应该是:@+id/fragment_discover_grid_item_relative_layout,后面之所以有一个1 ,是因为重复了。然后,我们在对应的xml文件中搜索这个控件名,然后删除重复即可。

去除 attr.xml 中重复的属性名称

在迁移到 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>

关于重名属性,在 androidX 中不知道哪个控件中包含了一个 mode 属性,所以,如果之前你的自定义控件中有 attr/mode,需要手动改成其他。

Glide中的注解不兼容androidX

迁移到 androidX 之后,Glide中使用的 android.support.annotation.CheckResultandroid.support.annotation.NonNull这两个注解无法迁移。之前有用户在Glide中提过issue: issues/3185

在上述issue中有用户表示,将Glide升级到 4.8.0 之后,可以正常迁移。但是,我这边并不行。然后,我先升级了Glide ,又在 gralde文件中增加了support.annotation ,这样才能正常编译通过。貌似在后续Glide 5.x 版本中会完成对 androidx的完全兼容。

我的临时解决方案:

//图片加载——Glide
implementation "com.github.bumptech.glide:glide:4.8.0
annotationProcessor "com.github.bumptech.glide:compiler:4.8.0//这两行是为了解决 https://github.com/bumptech/glide/issues/3185 ——Glide 中的注解还没有完全兼容androidx
implementation "com.android.support:support-annotations:28.0.0-alpha3"
annotationProcessor "com.android.support:support-annotations:28.0.0-alpha3"

规范包名

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

对于包名的命名规范,好像要求并非十分严格。因为我发现,部分包含大写字母的报名在编译时会报错,部分不报错。但是,遵守规范总会更好一点!

修改未自动迁移的三方库

虽然我们从gradle中配置了迁移三方库的参数,但是,由于三方库的版本更新问题,也可能会迁移失败。在三方库迁移失败时,如果使用了数据绑定,通常会报如下错误:

碰到上述错误之后,我们可以按下列步骤处理:

  • 在gradle文件中,将可升级的三方库升级(通常情况下,可升级的三方库会有黄色提示)
  • 如果gradle中可升级的库都升级之后依旧报上述错误,那么,可以新建一个项目,然后将gradle中的依赖库逐个拷贝到新项目中,没拷贝一个编译一次,这样可以确认是哪个三方库有问题。(实际操作时可以使用二分法的方式进行,每次拷贝一半的依赖库,然后编译)。然后就可以有针对性的处理了

莫名问题的解决

迁移过程中如果爆出一些 android 包本身或者其他莫名其妙的问题时,先去 xml 布局文件 或 .java 文件中找一下,是否有继续引用 xxx.support.xxx 的情况,如果有,记得替换成 androidx.xxx.xxx 包下的对应控件。( xxx 泛指任意内容)

2019-4-15

转载于:https://www.cnblogs.com/baiqiantao/p/10711147.html

Jetpack 迁移到 androidX support MD相关推荐

  1. Android源码中com.android.support依赖迁移到AndroidX库

    <<返回总目录 文章目录 一.AndroidX 简介 二.迁移步骤 三.Gradle 四.常用依赖库对比 五.温馨提示: 一.AndroidX 简介 1.Google(谷歌) 2018 I ...

  2. android studio迁移,AndroidStudio 一键迁移至 AndroidX

    在上篇文章 <关于 MVPArms 兼容 AndroidX 的问题> 中,我犯了一个致命的错误. 我没升级 AndroidStudio 到最新版, 是的, 我的 AndroidStudio ...

  3. 迁移到AndroidX报错Program type already present: androidx.annotation xxx

    最近将项目中使用的support库迁移到AndroidX了,其中遇到了几处报错,其中大部分错误在网上都能找到,现在我只把网上搜索不到解决方法的错误分享一下.迁移成功后,运行项目时报错:Program ...

  4. android项目迁移到androidX:类映射(android.support.v4*)

    支持库类 AndroidX 类 android.support.v4.accessibilityservice.AccessibilityServiceInfoCompat androidx.core ...

  5. android androidx版本,Android AndroidX 简介与迁移

    AndroidX 简介 AndroidX 是 Android 团队用于在 Jetpack 中开发,测试,打包,版本和发布库的开源项目 . AndroidX 是对原始 Android Support L ...

  6. AndroidX和Support库使用记录

    项目导入了support库但是没有办法使用 导入support-v4库的方法有很多,自己可以上网搜一下,都能找到.~ 使用网上的方法发现自己导入了包还是没有办法导入ViewPager ,Fragmen ...

  7. Android:你好,androidX!再见,android.support

    190325 补充:莫名问题的解决 181106 补充:修改未迁移成功的三方库 1.AndroidX简介 点击查看Android文档中对androidx的简介 按照官方文档说明 androidx 是对 ...

  8. Flutter中解决AndroidX包与Support包冲突问题

    本文转载来至https://www.jianshu.com/p/034254874ca9 最近在项目开发中遇到AndroidX兼容性问题,如下图 BUILD FAILED in 15s ******* ...

  9. android.support已死,androidx当立——弃用support库指南

    前言 androidx 是对 android.support.xxx 包的整理后产物.由于之前的support包过于混乱,所以,google推出了 androidX. 自support v7:28.+ ...

最新文章

  1. java awt 监视器_java – 停止所有Awt / Swing线程和监视器和东...
  2. Android内部自带的SQLite数据库操作dos命令
  3. Flash安全沙箱和跨域文件
  4. 数据库:Redis数据库优点介绍
  5. SQL中CONVERT转化函数的用法[个人推荐]
  6. 前端学习(7):web的三大技术
  7. 点击某些按钮不要触发验证控件
  8. 面向对象程序设计概念
  9. Xshell使用教程——问答方式
  10. 史海峰:我的架构师修炼之道
  11. 《刻意练习——如何从新手到大师》——创造天才
  12. 1.1 数组——二分查找(Leetcode 0704)
  13. pic32mx是什么cpu_DM320105 | Microchip 入门套件, XLP, PIC处理器系列 (MIPS32内核) MCU, PIC32 | RS Components...
  14. 谷歌浏览器弹出窗口html代码,谷歌浏览器显示通知消息JS代码
  15. 博士骗领210万元、硕士骗领3万元人才补贴,全被判刑了!
  16. 网络——介质访问控制
  17. GAF安装部署-微服务架构下的云原生部署
  18. vue字符云-------把老婆放到云端
  19. Matlab求解给定系统稳态误差
  20. 谷粒学院之阿里云oss存储模块

热门文章

  1. JavaScript学习(八十二)—JavaScript的组成
  2. hp android 平板,惠普三款Android平板电脑新产品曝光
  3. 京东月薪8万招聘HR,岗位要求只有这3个字
  4. Flash写操作流程
  5. 退休的姐妹们,你们还打工吗?
  6. 芯片5nm和7nm有什么差别?CPU已经很小了,可以做大点吗?
  7. Chialisp是什么
  8. 东北老家有人在大连娶了媳妇
  9. 中年人需要写个遗嘱吗?
  10. 职场中显情商低的三种行为