Markdown版本笔记 | 个人GitHub首页 | 个人博客 | 个人微信 | 个人邮箱 |
---|---|---|---|---|
MyAndroidBlogs | baiqiantao | baiqiantao | bqt20094 | baiqiantao@sina.com |
官方文档
全部 AndroidX 引用库
API文档
androidx-class-mapping.csv
androidx-artifact-mapping.csvhtml
Google 2018 IO 大会推出了扩展库 AndroidX,AndroidX 是对 android.support.xxx
包的整理后产物。因为以前的support包过于混乱,因此,google推出了 androidX 用于替换support包。java
其只是将原来的android.*
替换成androidx.*
,只有包名和Maven工件名受到影响,原来的类名、方法名和字段名不会更改。android
因为在后续版本中,会逐步放弃对 support 的升级和维护,因此,咱们必须尽快迁移到 androidX。git
Jetpack components are part of the AndroidX library. Learn more about components on the Jetpack home page.github
详细变化内容,能够下载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.csvapp
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.ide
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.一、修改 gradle.properties
android.useAndroidX=true android.enableJetifier=true
其中:
android.useAndroidX=true
表示当前项目启用 androidxandroid.enableJetifier=true
表示将依赖包
也迁移到androidx。若是取值为false,表示不迁移依赖包到androidx,但在使用依赖包中的内容时可能会出现问题,固然了,若是你的项目中没有使用任何三方依赖,那么,此项能够设置为false二、将AS更新至 AS 3.2 及以上
将 Gradle 插件版本改成 4.6 及以上
将 compileSdkVersion、buildToolsVersion 版本改成 28.0.2及以上
三、在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。此处要保持一个平静的 💗。
在 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文件中搜索这个控件名,而后删除重复便可。
在迁移到 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,须要手动改为其余。
迁移到 androidX 以后,Glide中使用的 android.support.annotation.CheckResult
和 android.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中配置了迁移三方库的参数,可是,因为三方库的版本更新问题,也可能会迁移失败。在三方库迁移失败时,若是使用了数据绑定,一般会报以下错误:
碰到上述错误以后,咱们能够按下列步骤处理:
迁移过程当中若是爆出一些 android 包自己或者其余莫名其妙的问题时,先去 xml 布局文件 或 .java
文件中找一下,是否有继续引用 xxx.support.xxx
的状况,若是有,记得替换成 androidx.xxx.xxx
包下的对应控件。( xxx 泛指任意内容)
2019-4-15