在Google I/O 2018上,Android团队宣布了AndroidX。它是Android团队用于在Jetpack中开发,测试,打包,版本和发布库的开源项目。与support库一样,每个AndroidX库都与Android操作系统分开提供,并提供跨Android版本的向后兼容性。

androidx 是对 android.support.xxx 包和其他google android支持库整理后的产物。由于之前的support包过于混乱,所以,google推出了 AndroidX。自support v7:28开始,大部分support包都会迁移到androidx下,所以建议开发者们尽早将项目转移到androidx下。

迁移步骤
1.先备份项目!
2.使用Android Studio自动迁移
在菜单栏中依次进入Refactor > Migrate to AndroidX,之后as会提示备份项目,如果已备份就不用勾选备份选项。

迁移完成之后,gradle.properties里会多出这两行

android.useAndroidX=true
android.enableJetifier=true
1
2
如果你的项目中没有使用任何三方依赖,那么android.enableJetifier可以设置为false

3. 旧依赖包名与新包名对照表(建议使用Ctrl + F搜索)
执行Migrate to AndroidX完成之后,虽然常用的support包都会被转成对应的androidx里的包名,gradle或者maven里的部分依赖但还是有可能会未能转换成功,这时候需要我们手动修改。

请参考 https://developer.android.google.cn/topic/libraries/support-library/refactor

 

更多的步骤参考  可以参考 https://www.jianshu.com/p/41de8689615d

遇到的问题,

1、gradle 版本太低

注意需要把这两个地方的gradle版本都改了

2. Could not find com.android.tools.build:gradle:3.4.1.

原因 :缺少Google库

解决方法:在项目的build.gradle  > buildscript >repositories中添加 google()

 

3、使用了注解的引用找不到了,如butterknife

Error:Execution failed for task ':app:javaPreCompileDebug'.
> Annotation processors must be explicitly declared now.  The following dependencies on the compile classpath are found to contain annotation processor.  Please add them to the annotationProcessor configuration.
    - butterknife-7.0.1.jar
  Alternatively, set android.defaultConfig.javaCompileOptions.annotationProcessorOptions.includeCompileClasspath = true to continue with previous behavior.  Note that this option is deprecated and will be removed in the future.
  See https://developer.android.com/r/tools/annotation-processor-error-message.html for more details.
 在app的build中
android {
    ...
    defaultConfig {
        ...
        //添加如下配置就OK了
        javaCompileOptions { annotationProcessorOptions { includeCompileClasspath = true } }
    }
    ...

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐