The given artifact contains a string literal with a package reference 'android.support.v4.content' that cannot be safely rewritten. for androidx
Asked Answered
U

10

127

I upgraded my android studio to 3.4 canary and now I cannot successfully build anymore due to the following error:

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.

More details:

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.)

Clearly, its something to do with Butterknife, androidx and Jetifier

Do anybody know how to fix this?

Uncivil answered 26/11, 2018 at 16:7 Comment(4)
tried this: this but no luckUncivil
What was the regular expression you tried to add to the blacklist?Castilian
The given artifact contains a string literal with a package reference 'android.support.v4.widget' that cannot be safely rewritten. Libraries using reflection such as annotation processors need to be updated manually to add support for androidx. I am receiving above error, when i am trying to create signed apk with minifyenabled and shrinkresources set to true. I am able to run application in emulator or device otherwise. How to resolve this issue?Eyesore
Nice question! Very Helpful, ThanksEmaemaciate
C
204

New correct answer:

Butterknife 10.0.0 added support for AndroidX.

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

Old answer for Butterknife < 10.0.0:

Try blacklisting butterknife from the jetifier:

gradle.properties file:

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

You need to be on the 3.3.0-rc1 of the AGP and the 1.3.0 version of the Kotlin Gradle plugin:

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'
    }
}
Castilian answered 26/11, 2018 at 18:47 Comment(4)
I think you don't add android.jetifier.blacklist = butterknife.*\\.jar because I tested it didn't work. You only add classpath 'com.jakewharton:butterknife-gradle-plugin:9.0.0-rc2' then it will working when debug and export file apk without a problem.Groh
More info can be found here issuetracker.google.com/issues/119135578#comment5Colwen
This answer is correct, but have a look at: https://mcmap.net/q/174228/-the-given-artifact-contains-a-string-literal-with-a-package-reference-39-android-support-v4-content-39-that-cannot-be-safely-rewritten-for-androidx update butterknife to 10, also resolves this issue in a better way.Selfinductance
This solution works fine for debug build but I am not able to create signed release APK. The given artifact contains a string literal with a package reference 'android.support.v4.widget' that cannot be safely rewritten. Libraries using reflection such as annotation processors need to be updated manually to add support for androidx.Eyesore
P
63

Add the last version of the butterknive dependency you can check it here if it changes (https://github.com/JakeWharton/butterknife). It supports androidX. Then go to your app build graddle and replace the old version with the following:

dependencies {
implementation 'com.jakewharton:butterknife:10.0.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.0.0'
}
Postmark answered 24/1, 2019 at 11:41 Comment(0)
R
28

For androidx simply upgrade your dependencies to version '10.0.0'

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

Look for documentation here

Rollo answered 18/5, 2019 at 18:3 Comment(1)
My Click edit text box gives null pointer. it is not initialize.Quinonez
E
20

Upgrade ButterKnife to latest version and make sure to add these into your build.gradle(app):

android {
...
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
}
Estimation answered 15/5, 2019 at 13:1 Comment(2)
best answer, after the upgeade I've searched this solution for --> Error: Static interface methods are only supported starting with Android N (--min-api 24): void butterknife.Unbinder.lambda$static$0()Shillelagh
love you man you made it ... i wasted half the day searching then this answer comes.Blacktail
C
10

My project doesn't use butterknife, but I had the same error "The given artifact contains a string literal with a package reference 'android.support.v4.widget' that cannot be safely rewritten. Libraries using reflection such as annotation processors need to be updated manually to add support for androidx " This what I did to solve it: Update your parceler version

gradle build file

Replace:

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

With:

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

gradle file code gradle file view

Counterirritant answered 21/8, 2020 at 19:37 Comment(0)
S
3

Which version if Butterknife you use? Latest version 9.0.0-rc2 supports androidx.

UPD: There is closed issue on butterknife's github repo. Temporary workaround

Add android.jetifier.blacklist=butterknife-compiler to your gradle.properties file.

Syllogism answered 26/11, 2018 at 16:37 Comment(1)
I'm on 9.0.0-rc2 and I'm getting the exact same errorCastilian
L
2

Using the latest version of Butterknife resolved the issue. Use >= 9.0.0-rc2 (Butterknife Version) to support androidX. For the latest release check the link - https://github.com/JakeWharton/butterknife/releases

Lung answered 27/2, 2019 at 9:52 Comment(0)
D
2

Change

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

to

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

or other Material Themes. Got this error after starting New Project with "No Activity" in Android Studio 4.0.1

Disarming answered 3/8, 2020 at 17:41 Comment(0)
P
1

update butterknife + invalidate cache and restart + sync gradle if buterknife not used just clear cache and restart

Proudlove answered 30/8, 2020 at 13:7 Comment(0)
A
0

For my old project I was facing the same issue, tried all options but not worked, only this change works for me:

Do following changes in gradle.properties file

android.useAndroidX=false
android.enableJetifier=false
Ashlar answered 25/5, 2023 at 6:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.