在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版本都改了

AndroidX迁移_第1张图片

2. Could not find com.android.tools.build:gradle:3.4.1.
AndroidX迁移_第2张图片

原因 :缺少Google库

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

AndroidX迁移_第3张图片

 

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 } }
    }
    ...

更多相关文章

  1. 推荐10个Android开源项目
  2. Android Studio运行项目闪退(页面闪退以及跳转闪退)
  3. 关于Android Studio构建失败,导入新项目打包apk遇到的一些问题
  4. Android studio创建java项目
  5. cocos2d-x 2.x版本之win32 window移植android 环境搭配 只用NDK
  6. Android 项目开发填坑记 - 使用 MultiDex 解决 64K 限制
  7. 低版本android project在高版本ADK中运行方法
  8. Android项目--团购类客户端
  9. Android百度地图——定位SDK(版本v3.1)(二)

随机推荐

  1. android在service中使用AsyncHttpClient
  2. Android 之事件的分发和消费机制
  3. android:configChanges名词解析(文档翻译)
  4. Android ndk 入门5 - opencv实现
  5. Layout inflation is the term used with
  6. Android关于获取用户是否允许某项权限
  7. Android compress Image
  8. android aosp 源码版本
  9. android 代码片段整理
  10. Android FAILURE: Build failed with an