天天看点

butterKnife10.1.0使用说明及报错解决

一、引入步骤

1.在app的build.gradle中添加依赖

dependencies {
    implementation 'com.jakewharton:butterknife:10.1.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'
}
           

2.在app的build.gradle中添加java编译版本说明

android {
    // Butterknife requires Java 8.
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
           

二、注意事项

问题现象

引入后编译报错

Manifest merger failed : Attribute [email protected] value=(android.support.v4.app.CoreComponentFactory) from [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91
    is 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 <application> element at AndroidManifest.xml:36:5-364:19 to override.
           

按照以下提示修改了manifest文件后继续报错

org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:processDebugManifest'.
           

其实问题原因在于:Androidx和Android support库不能共存

解决方法:转换成androidX

1. 将build tools更新到3.2.0,gradle更新到4.6,依赖库统一更新到28.0.0

2. 在gradle.properties文件中添加

android.useAndroidX=true
android.enableJetifier=true
           

3. 工具栏选择→Refactor→Migrate to Androidx...

butterKnife10.1.0使用说明及报错解决

4. 选中所有需要重命名的目录,执行Do Refactor

5.修改所有类中的引用