122

我升级了我的android studio to 3.4 canary,现在由于以下错误而无法成功构建:

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.

更多细节:

Caused by: java.lang.RuntimeException: Failed to transform '.gradle/caches/modules-2/files-2.1/com.jakewharton/butterknife-compiler/9.0.0-SNAPSHOT/732f93940c74cf32a7c5ddcc5ef66e53be052352/butterknife-compiler-9.0.0-SNAPSHOT.jar' using Jetifier. Reason: 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.)

显然,它与Butterknife, androidx and Jetifier

有人知道如何解决这个问题吗?

4

9 回答 9

194

新的正确答案:

Butterknife 10.0.0 添加了对 AndroidX 的支持。

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

Butterknife < 10.0.0 的旧答案:

尝试将 jetifier 中的黄油刀列入黑名单:

gradle.properties file:

android.jetifier.blacklist = butterknife.*\\.jar

您需要在 AGP 的 3.3.0-rc1 和 Kotlin Gradle 插件的 1.3.0 版本上:

buildscript {
    repositories {
        ...
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.0-rc01'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.0"
        classpath 'com.jakewharton:butterknife-gradle-plugin:9.0.0-rc2'
    }
}
于 2018-11-26T18:47:56.933 回答
62

添加黄油刀依赖项的最新版本,如果它发生变化,您可以在此处检查它(https://github.com/JakeWharton/butterknife)。它支持androidX。然后转到您的应用程序构建 graddle 并将旧版本替换为以下内容:

dependencies {
implementation 'com.jakewharton:butterknife:10.0.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.0.0'
}
于 2019-01-24T11:41:11.367 回答
29

对于 androidx,只需将您的依赖项升级到版本 '10.0.0'

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

在此处查找文档

于 2019-05-18T18:03:46.883 回答
19

将 ButterKnife 升级到最新版本并确保将这些添加到您的 build.gradle(app) 中:

android {
...
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
}
于 2019-05-15T13:01:25.207 回答
9

我的项目没有使用黄油刀,但我遇到了同样的错误“给定的工件包含一个字符串文字,其中包含无法安全重写的包引用'android.support.v4.widget'。使用反射的库(如注释处理器)需要手动更新以添加对 androidx 的支持“这是我为解决它所做的:更新您的 parceler 版本

梯度构建文件

代替:

annotationProcessor 'org.parceler:parceler:1.1.6'
implementation 'org.parceler:parceler-api:1.1.6'

和:

  annotationProcessor 'org.parceler:parceler:1.1.13'
  implementation 'org.parceler:parceler-api:1.1.13'

gradle 文件代码 gradle 文件视图

于 2020-08-21T19:37:19.070 回答
2

如果您使用 Butterknife 哪个版本?最新版本 9.0.0-rc2 支持 androidx。

UPD:butterknife 的github repo上有一个已关闭的问题。临时解决方法

将 android.jetifier.blacklist=butterknife-compiler 添加到您的 gradle.properties 文件中。

于 2018-11-26T16:37:32.843 回答
2

使用最新版本的 Butterknife 解决了这个问题。使用 >= 9.0.0-rc2 (Butterknife 版本) 来支持 androidX。有关最新版本,请查看链接 - https://github.com/JakeWharton/butterknife/releases

于 2019-02-27T09:52:06.833 回答
1

如果未使用 Buterknife,则更新 Buterknife + 无效缓存并重新启动 + 同步 gradle 只需清除缓存并重新启动

于 2020-08-30T13:07:39.353 回答
1

改变

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">

或其他材料主题。在 Android Studio 4.0.1 中以“无活动”启动新项目后出现此错误

于 2020-08-03T17:41:32.783 回答