给定的工件包含一个字符串文字,其中包含无法安全重写的包引用“android.support.v4.content”。对于 androidx

Posted

技术标签:

【中文标题】给定的工件包含一个字符串文字,其中包含无法安全重写的包引用“android.support.v4.content”。对于 androidx【英文标题】:The given artifact contains a string literal with a package reference 'android.support.v4.content' that cannot be safely rewritten. for androidx 【发布时间】:2019-04-28 07:50:47 【问题描述】:

我升级了我的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有关

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

【问题讨论】:

试过这个:this 但没有运气 您尝试添加到黑名单的正则表达式是什么? 给定的工件包含一个字符串文字,其中包含无法安全重写的包引用“android.support.v4.widget”。使用反射的库(例如注解处理器)需要手动更新以添加对 androidx 的支持。当我尝试使用 minifyenabled 和 shrinkresources 设置为 true 创建签名 apk 时,我收到了上述错误。我可以在模拟器或设备中运行应用程序。如何解决这个问题? 好问题!很有帮助,谢谢 【参考方案1】:

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

UPD:butterknife 的 github repo 已关闭问题。 Temporary workaround

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

【讨论】:

我在 9.0.0-rc2 上,我得到了完全相同的错误【参考方案2】:

新的正确答案:

Butterknife 10.0.0 添加了对 AndroidX 的支持。

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


Butterknife

尝试将jetifier中的butterknife列入黑名单:

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'
    

【讨论】:

我认为您没有添加 android.jetifier.blacklist = butterknife.*\\.jar 因为我测试它不起作用。您只需添加类路径 'com.jakewharton:butterknife-gradle-plugin:9.0.0-rc2' 即可在调试和导出文件 apk 时正常工作。 更多信息可以在这里找到issuetracker.google.com/issues/119135578#comment5 这个答案是正确的,但是看看:***.com/a/54345816/371749 update butterknife to 10,也能更好地解决这个问题。 此解决方案适用于调试构建,但我无法创建签名发布 APK。给定的工件包含一个字符串文字,其中包含无法安全重写的包引用“android.support.v4.widget”。使用反射的库(例如注解处理器)需要手动更新以添加对 androidx 的支持。 非常有帮助,谢谢【参考方案3】:

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

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

【讨论】:

【参考方案4】:

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

【讨论】:

【参考方案5】:

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

android 
...
compileOptions 
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8


【讨论】:

最佳答案,在升级后我搜索了这个解决方案 --> 错误:仅支持从 Android N (--min-api 24) 开始的静态接口方法:void butterknife.Unbinder。 lambda$static$0() 爱你,你做到了……我浪费了半天时间寻找答案。【参考方案6】:

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

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

查找文档here

【讨论】:

我的点击编辑文本框给出了空指针。它没有初始化。【参考方案7】:

改变

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

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

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

【讨论】:

【参考方案8】:

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

gradle 构建文件

替换:

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'

【讨论】:

【参考方案9】:

更新黄油刀 + 无效缓存并重新启动 + 同步 gradle 如果没有使用buterknife,则清除缓存并重新启动

【讨论】:

以上是关于给定的工件包含一个字符串文字,其中包含无法安全重写的包引用“android.support.v4.content”。对于 androidx的主要内容,如果未能解决你的问题,请参考以下文章

Intellij 错误:无法构建工件“XXX:战争爆炸”,因为它包含在循环依赖项中

ejb与客户端工件 - 运行时依赖?

java 给定一个算术表达式的字符串;其中只包含两个运算符:'+','*',编写一个计算表达式的函数

xpath:查找具有给定属性的节点,其值包含字符串

如果 HTML 包含空格,则 JQuery HTML 未终止的字符串文字错误

在包含字符串的 ArrayList 中查找索引