前言

今天在 Android Studio 上新建了个项目,引入 butterknife:10.0.0,运行后居然抛出了异常:

Manifest merger failed : Attribute application@appComponentFactory value=(android.support.v4.app.CoreComponentFactory) from [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91is also present at [androidx.core:core:1.0.0] AndroidManifest.xml:22:18-86 value=(androidx.core.app.CoreComponentFactory).Suggestion: add 'tools:replace="android:appComponentFactory"' to  element at AndroidManifest.xml:5:5-19:19 to override.

按照日志提示
在 AndroidManifest.xml 中添加
tools:replace=“android:appComponentFactory”

<?xml version="1.0" encoding="utf-8"?>                                                                                

运行后,仍抛出异常:

Manifest merger failed with multiple errors, see logs

显示 AndroidManifest.xml 文件里出错
查看 AndroidManifest.xml 文件里的 Merged Manifest:

折腾几番后,仍找不到解决方法,就直接上 butterknife 的 issue 上寻找答案。

发现 butterknife:10.0.0 已经不支持 support 老版库,如果需要使用,只能使用9.0.0这版。 butterknife:10.0.0 只支持新的 AndroidX 库。

ButterKnife的github地址
Github: https://github.com/JakeWharton/butterknife

1. AndroidX库

Google 2018 IO 大会推出了 Android 新的扩展库 AndroidX,用于替换原来的 Android support 扩展库,将原来的 android.替换成 androidx.;只有包名和 Maven 工件名受到影响,原来的类名,方法名和字段名不会更改。

1.1 AndroidX变化

1、常用依赖库对比
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
2、常用支持库类对比
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了解一下

2. 解决第三方库只支持 AndroidX 库问题

2.1 方法一:将项目的 support 库统一转换成 AndroidX 库

1、AS 更新升级配置
  • 将 AS 更新至 AS 3.2 及以上
  • Gradle 插件版本改为 4.6 及以上,gradle 版本至少为3.2.0以上

  • compileSdkVersion 版本升级到 28及以上
  • buildToolsVersion 版本改为 28.0.2及以上
2、开始迁移AndroidX
方法一:一键迁移AndroidX库

在 AS 3.2 Canary 中添加了一键迁移的功能,Refactor -> Migrate to AndroidX 。

选择Migrate

选择要转换的项目

选择需要转换为 AndroidX 包的库,点击 Do Refactor

方法二:配置 gradle.properties 迁移AndroidX库

除了 Do Refactor 方法外,还可以通过配置 gradle.properties 引入 AndroidX 库。

android.useAndroidX=trueandroid.enableJetifier=true

其中:

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

在使用 android.enableJetifier=false 时,需要注意:
引入某些第三方的最新库时,如 butterknife:10.0.0,是只支持新的 AndroidX 库,所以要将整个项目的依赖都转换为 AndroidX,否则编译就会报错。

3、对比 Android support 库和 AndroidX 库

转换前和转换后类库对比
Android support 库

AndroidX 库

4、androidx.constraintlayout.widget.ConstraintLayou 异常

当将所有库的依赖都转换为 AndroidX 后,运行项目,居然抛出 androidx.constraintlayout.widget.ConstraintLayout 的错误:

附上 build.gradle 文件和布局 xml 文件
build.gradle

dependencies {    implementation fileTree(include: ['*.jar'], dir: 'libs')    implementation 'androidx.appcompat:appcompat:1.0.0-beta01'    implementation 'androidx.constraintlayout:constraintlayout:1.1.2'    testImplementation 'junit:junit:4.12'    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'}

布局 xml 文件

<?xml version="1.0" encoding="utf-8"?>    

所有的 support 库都已改成 AndroidX 了,而且布局中 ConstraintLayout 控件的引入也改成了 androidx,为什么还会抛出 Error inflating class androidx.constraintlayout.widget.ConstraintLayout 异常??

解法:
原来是 androidx.constraintlayout:constraintlayout:1.1.2 中的一个bug,将

implementation 'androidx.constraintlayout:constraintlayout:1.1.2'

改成

implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

编译后,即可成功运行出来。

2.2 方法二:降低第三方库的版本,使其兼容 Android support 库

使用第三方依赖库时,如果最新版本适配的是 AndroidX 库,可降低第三方的库的版本。
如 butterknife,从 butterknife 的 issue 上可知,butterknife:9.0.0 是适配 Android support 库的,如果你的项目仍想使用 Android support 老库,引入 butterknife:9.0.0 即可。

PS:

依赖库的时候,尽量不要使用 latest.release,而使用具体的版本。
原因:

  1. 不会因为依赖库更新,引起兼容问题
  2. 减少每次去检查最新版本,可以节省编译时间

3. 引入 butterknife:10.0.0

在整个项目转换为 AndroidX 库后,引入 butterknife:10.0.0
在 build.gradle 里添加 butterknife:10.0.0

android {    compileSdkVersion 28    defaultConfig {        applicationId "com.example.testdemo1"        minSdkVersion 16        targetSdkVersion 28        versionCode 1        versionName "1.0"        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"        //AndroidStudio 3.0 canary 8 Annotation processors must be exp        //添加部分        javaCompileOptions {            annotationProcessorOptions {                includeCompileClasspath true            }        }    }   .......}dependencies {   ......    implementation 'com.jakewharton:butterknife:10.0.0'    implementation 'com.jakewharton:butterknife-compiler:10.0.0'}

添加后,运行,抛出异常:

Error: Static interface methods are only supported starting with Android N (--min-api 24): void butterknife.Unbinder.lambda$static$0()

原因分析: java8才支持静态接口方法,Android N 要求 jdk 版本为1.8
解决方案:定义和调用静态接口方法的 module 编译时都使用 jdk1.8 即可

原来 butterknife:10.0.0 需要指定 java的 jdk 版本
在 app 的 build.gradle 中添加如下:

android {     ......    // 指定jdk版本    compileOptions {        sourceCompatibility 1.8        targetCompatibility 1.8    }}

编译后,即可成功运行出来。

4. 总结

AndroidX 虽然目前对我们没有多大的影响,我们可以不使用它,仍然使用旧版本支持库,毕竟没有强制,但从长远来看还是有好处的。AndroidX 重新设计了包结构,旨在鼓励库的小型化,支持库和架构组件包的名字也都简化了,而且也是减轻Android生态系统碎片化的有效方式。之后Android 的一些新的特性,也都会被写在 AndroidX 库中。

5. 附

如果在老版本转换到 AndroidX 的过程中遇到 Program type already present 报错,可参考这个连接,使用androidx时Program type already present报错的一种解决尝试

更多相关文章

  1. 箭头函数的基础使用
  2. NPM 和webpack 的基础使用
  3. Python list sort方法的具体使用
  4. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  5. android 使用html5作布局文件: webview跟javascript交互
  6. android“设置”里的版本号
  7. Android(安卓)Resource介绍和使用
  8. "Failed to fetch URL https://dl-ssl.google.com/android/repos
  9. 使用NetBeans搭建Android开发环境

随机推荐

  1. Android(安卓)DVM
  2. android UI 优化系列之 创建RGB565的缓存
  3. Android(安卓)Notes(06) - Touch事件分发
  4. android studio logcat 打印不出信息
  5. [Android] - 官方轉換dp, sp 至 pixel的
  6. 如何打log 检查 Android(安卓)CTS failur
  7. PendingIntent实现原理和代码
  8. Android崩溃
  9. Android中Http网络请求库Asnyc-http的使
  10. Android(安卓)内存溢出(Out Of Memory)