How can I fix this error with ButterKnife in Android Studio?

43.1k Views Asked by At

Building succeeds, but running on an emulator fails with this message in the console:

Cause: superclass access check failed: class butterknife.compiler.ButterKnifeProcessor$RScanner (in unnamed module @0x65e8e2f6) cannot access class com.sun.tools.javac.tree.TreeScanner (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.tree to unnamed module @0x65e8e2f6

What I've tried: Removing Butterknife from Gradle, syncing, adding back, syncing again. Removing the Butterknife lib completely which doesn't produce this error on-compile, but obviously fails to compile due to missing Butterknife.

My environment: Butterknife 10.2.3 Android Grade Plugin Version: 7.1.2 - Gradle Version: 7.2 - Android Studio Bumblebee

I've read that this error happens with other libraries in some rare cases that seem unrelated to mine and I still haven't found a solution. I'm aware the Butterknife is deprecated and I should migrate to ViewBinding, but that's a big task to convert this entire project to at the moment. Thanks!

12

There are 12 best solutions below

2
George On

You should compile with a different jdk, I was compiling with a jdk which was too advanced. Reverting it to jdk11 solved it for me

2
HMI On

Please use JDK 11. though we are setting the Gradle JDK as jdk11 or correto 11, Android studio is not recognizing that.

Solution:

  1. in your terminal type java --version to see it if is showing as JDK 11

  2. If the version is not JDK 11, then set the JAVA_HOME env variable export JAVA_HOME=/Users/${echo $USER}/documents/Android\Studio.app/Contents/jre/Contents/Home

  3. in your terminal type java --version to see it if is showing as JDK 11.

2
Farid Z On

Using ideas from answers above and using Android Studio: File | Settings | Build, Execution, Deployment | Build Tools | Gradle. Gradle JDK -> Android Studio default JDK (version 11.0.13 ...)

On Windows, to build from terminal, also had to add

JAVA_HOME=C:\Program Files\Android\Android Studio\jre

To the File | Settings | Terminal | Environment variables

0
Fernando Batista On

Deleting the folder .gradle from the folder C:\Users\yourUser.gradle\ fixed the issue.

0
Zaman Rajpoot On

I faced this issue just after updating android Studio to Android Studio Flamingo | 2022.2.1, my project was using embeded jdk, i fixed using simple steps

  1. https://www.oracle.com/pk/java/technologies/javase/jdk11-archive-downloads.html
  2. Download JDK 11 for your desired OS
  3. Install it
  4. Android Studio-> File -> Project Structure -> SDK Location -> Click on Gradle Settings (blue hightlighted text) -> Select the jdk 11 with 11.0 something version name from list
  5. Run The Project
  6. Boom! it's works
0
Diederik On

Some of the sun libraries needs to be made visible for the newer Java compilers. See this answer for more info. I added the following to our gradle.properties file and it fixed the problem. (We use Butterknife and Realm, and needed the below three packages added. (You might get away without "javac.code" for just Butterknife))

org.gradle.jvmargs=-Xmx1920M \
--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED \
--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
6
azzits On

Try adding the following code to the app build.gradle(project level) inside android section {}

android{
    tasks.withType(JavaCompile).configureEach{
    options.fork = true
    opti ons.forkOptions.jvmArgs +=[
    '--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED', 
    '--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED', 
    '--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED']
}

In gradle.properties file,

android.nonFinalResIds=false

for example

android {
    namespace = "com.xp.vrsr"
    compileSdk = 34

    defaultConfig {
        applicationId "com.interact.vrsrilanka"
        minSdk 24
        targetSdk 34
        versionCode 4
        versionName "1.3"
        multiDexEnabled true
    }
    tasks.withType(JavaCompile).configureEach{
        options.fork = true
        options.forkOptions.jvmArgs +=[
                '--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED',
                '--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED',
                '--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED']
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    lintOptions {
        checkReleaseBuilds false
        // Or, if you prefer, you can continue to check for errors in release builds,
        // but continue the build even when errors are found:
        abortOnError false
        }
    }
0
MEGHA RAMOLIYA On

please add this line in build.gradle(:app) file.

android { 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', ] } }

0
Rashid On

Step 1: gradle.properties add

android.nonFinalResIds=false

Step 2: app build.gradle add

tasks.withType(JavaCompile).configureEach{options.fork = true
options.forkOptions.jvmArgs +=[
'--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED', 
'--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED', 
'--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED']}
1
ZarNi Myo Sett Win On

I just got that issue when opening my old project with the latest Android Studio Flamingo and wasted around 3 hours. After I have set the project JDK to 11 then rebuild the project. It solved the issue well.

enter image description here

0
Yahya M On
 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',
    ]
}

reference image

add the following confguration line in your gradle file and Update your gradle plugin version to 8.0.1 and gradle version to 8.1.1

0
Patel Himanshu On

please add this line in gradle propertise file.

First you change this line

org.gradle.jvmargs=-Xmx2048m

To

org.gradle.jvmargs=-Xmx1920M \
--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED \
--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED

after steps follow working fine in my project.