无法使用 butterknife 依赖项构建 android 项目

not able to build android project with butterknife dependency

由于 butterknife 依赖错误,我无法构建 android 项目。

错误

Execution failed for task ':app:javaPreCompileDebug'.

Could not resolve all files for configuration ':app:debugAnnotationProcessorClasspath'. Failed to transform butterknife-compiler-8.6.0.jar (com.jakewharton:butterknife-compiler:8.6.0) to match attributes {artifactType=processed-jar, org.gradle.category=library, org.gradle.libraryelements=jar, org.gradle.status=release, org.gradle.usage=java-runtime}. > Execution failed for JetifyTransform: /Users/jordan/.gradle/caches/modules-2/files-2.1/com.jakewharton/butterknife-compiler/8.6.0/d3defb48a63aa0591117d0cec09f47a13fffda19/butterknife-compiler-8.6.0.jar. > Failed to transform '/Users/jordan/.gradle/caches/modules-2/files-2.1/com.jakewharton/butterknife-compiler/8.6.0/d3defb48a63aa0591117d0cec09f47a13fffda19/butterknife-compiler-8.6.0.jar' using Jetifier. Reason: AmbiguousStringJetifierException, message: The given artifact contains a string literal with a package reference 'android.support.v4.content' that cannot be safely rewritten. Libraries using reflection such as annotation processors need to be updated manually to add support for androidx.. (Run with --stacktrace for more details.) This is a known exception, and Jetifier won't be able to jetify this library. Suggestions: - If you believe this library doesn't need to be jetified (e.g., if it already supports AndroidX, or if it doesn't use support libraries/AndroidX at all), add android.jetifier.blacklist = {comma-separated list of regular expressions (or simply names) of the libraries that you don't want to be jetified} to the gradle.properties file. - If you believe this library needs to be jetified (e.g., if it uses old support libraries and breaks your app if it isn't jetified), contact the library's authors to update this library to support AndroidX and use the supported version once it is released. If you need further help, please leave a comment at

Gradle 应用级构建文件中的依赖项

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.2.0-beta02'
    implementation 'com.melnykov:floatingactionbutton:1.3.0'
  //  implementation 'com.android.support:design:28.0.0'
 //   implementation 'com.android.support:support-v13:28.0.0'
    implementation 'com.jpardogo.materialtabstrip:library:1.0.6'
    implementation 'com.jakewharton:butterknife:8.6.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0'



}

我该如何解决?

请注意,根据官方文档,Butter Knife 工具现已弃用。
他们建议切换到 View Binding.

因此,我请求您重新考虑使用黄油刀的决定。

并且在您的 gradle 文件中,请确保正确添加以下行。
添加这些更改,然后在 Android Studio 中单击文件 > 使缓存无效并重新启动。

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

依赖关系

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

如果您使用的是 Kotlin,请将 annotationProcessor 替换为 kapt。

要在图书馆中使用 Butter Knife,请按照 official documentation

中的 图书馆项目 部分中提供的步骤进行操作