由于 Butterknife 依赖错误,我无法构建 Android 项目。
错误
任务“:app:javaPreCompileDebug”执行失败。
无法解析配置“:app:debugAnnotationProcessorClasspath”的所有文件。 无法转换 butterknife-compiler-8.6.0.jar (com.jakewharton:butterknife-compiler:8.6.0) 以匹配属性 {artifactType=processed-jar, org.gradle.category=library, org.gradle.libraryelements=jar, org.gradle.status=release, org.gradle.usage=java-runtime}。 > JetifyTransform 执行失败:/Users/jordan/.gradle/caches/modules-2/files-2.1/com.jakewharton/butterknife-compiler/8.6.0/d3defb48a63aa0591117d0cec09f47a13fffda19/butterknife-compiler-8.6.0.jar。 > 无法转换“/Users/jordan/.gradle/caches/modules-2/files-2.1/com.jakewharton/butterknife-compiler/8.6.0/d3defb48a63aa0591117d0cec09f47a13fffda19/butterknife-compiler-8.6.0.jar” 使用 Jetifier。原因:AmbigeousStringJetifierException,消息: 给定的工件包含带有包引用的字符串文字 无法安全重写的“android.support.v4.content”。 使用反射的库(例如注释处理器)需要 手动更新以添加对 androidx 的支持..(使用 --stacktrace 运行 更多细节。) 这是一个已知的异常,Jetifier 将无法jetify 这个库。 建议: - 如果您认为这个库不需要jetified(例如,如果它已经支持AndroidX,或者如果它不使用支持) 完全库/AndroidX),添加 android.jetifier.blacklist = {以逗号分隔的正则表达式(或简单名称)列表 您不想将其喷射到 gradle.properties 的库 文件。 - 如果您认为这个库需要被jetified(例如,如果它使用旧的支持库并且如果不这样做就会破坏您的应用程序) jetified),请联系该库的作者将此库更新为 支持AndroidX并在发布后使用支持的版本。 如果您需要进一步帮助,请留言
应用程序级别的 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,请按照官方文档中的库项目
部分中提供的步骤进行操作In Android studio Flamingo and above version below code working for butterknife library.
1. add below code in build.gradle(:app) file
tasks.withType(JavaCompile).configureEach {
options.fork = true
options.forkOptions.jvmArgs += [
'--add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED',
'--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED',
'--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED',
'--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED',
'--add-opens=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED',
'--add-opens=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED',
'--add-opens=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED',
'--add-opens=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED',
'--add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED',
'--add-opens=jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED',
]
}
2.
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'
}
3. To use Butter Knife in a library, add the plugin to your buildscript:
buildscript {
repositories {
mavenCentral()
google()
}
dependencies {
classpath 'com.jakewharton:butterknife-gradle-plugin:10.2.3'
}
}
4. and then apply it in your module:
apply plugin: 'com.android.library'
apply plugin: 'com.jakewharton.butterknife'
5. Now make sure you use R2 instead of R inside all Butter Knife annotations.
class ExampleActivity extends Activity {
@BindView(R2.id.user) EditText username;
@BindView(R2.id.pass) EditText password;
...
}