Cannot run gradle test tasks because of java.lang.NoClassDefFoundError: jdk/internal/reflect/GeneratedSerializationConstructorAccessor1

We Are Going To Discuss About Cannot run gradle test tasks because of java.lang.NoClassDefFoundError: jdk/internal/reflect/GeneratedSerializationConstructorAccessor1. So lets Start this Java Article.

Cannot run gradle test tasks because of java.lang.NoClassDefFoundError: jdk/internal/reflect/GeneratedSerializationConstructorAccessor1

  1. Cannot run gradle test tasks because of java.lang.NoClassDefFoundError: jdk/internal/reflect/GeneratedSerializationConstructorAccessor1

    In such case, even if your JAVA_HOME is set in bash_profile, you also need to set in Android, to do so:
    Go to File -> Project Structure -> SDK location
    set JDK location.
    At times, it might a different location from bash_profile one.

  2. Cannot run gradle test tasks because of java.lang.NoClassDefFoundError: jdk/internal/reflect/GeneratedSerializationConstructorAccessor1

    In such case, even if your JAVA_HOME is set in bash_profile, you also need to set in Android, to do so:
    Go to File -> Project Structure -> SDK location
    set JDK location.
    At times, it might a different location from bash_profile one.

Solution 1

This is an old issue, but for future google searchers and perhaps interested parties that could work around this, it is worth mentioning that if you are using one of the more popular walk-throughs to set up JaCoCo + Robolectric + Espresso – https://medium.com/@rafael_toledo/setting-up-an-unified-coverage-report-in-android-with-jacoco-robolectric-and-espresso-ffe239aaf3fa . Please add below this:

tasks.withType(Test) {
    jacoco.includeNoLocationClasses = true
    jacoco.excludes = ['jdk.internal.*']
}

In my case:

junitJacoco {
    jacocoVersion = '0.8.4' // type String
    includeInstrumentationCoverageInMergedReport = true // type boolean
    tasks.withType(Test) {
        jacoco.includeNoLocationClasses = true
        jacoco.excludes = ['jdk.internal.*']
    }
}

Original Author Of This Content

Solution 2

Thanks to @Tungken

I had to add it inside testOptions { unitTests.all { jacoco {

android {
  compileSdk 30
  buildToolsVersion "30.0.3"

  defaultConfig {
 
     ...

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
       
    }
    debug {
        testCoverageEnabled true
        debuggable true
    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_11
    targetCompatibility JavaVersion.VERSION_11
}
kotlinOptions {
    jvmTarget = '11'
}

testOptions {
    unitTests.all {
        jacoco {
            includeNoLocationClasses = true
            excludes = ['jdk.internal.*']       //This line
        }
    }
    unitTests.returnDefaultValues = true
}

}

Original Author Of This Content

Solution 3

In such case, even if your JAVA_HOME is set in bash_profile, you also need to set in Android, to do so:
Go to File -> Project Structure -> SDK location
set JDK location.

At times, it might a different location from bash_profile one.

Refer to screenshot for reference
enter image description here

Original Author Of This Content

Solution 4

For anyone looking how the fix looks like in Kotlin *.kts:

tasks.withType<Test> {
    extensions.configure(JacocoTaskExtension::class) {
        isIncludeNoLocationClasses = true
        excludes = listOf("jdk.internal.*")
    }
}

Original Author Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment