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

Viewed 13139

I have an android project where I cannot run anymore all the test gradle tasks locally (I have 3 different flavors in this project). I have this error message 50 times before the tasks fail. I have no issue running these tasks remotely with Gitlab CI/CD, and I have another project locally where I don't have this issue neither.

java.lang.NoClassDefFoundError: jdk/internal/reflect/GeneratedSerializationConstructorAccessor1
    at jdk.internal.reflect.GeneratedSerializationConstructorAccessor1.newInstance(Unknown Source)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at java.base/java.io.ObjectStreamClass.newInstance(ObjectStreamClass.java:1092)
    at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2150)
    at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1668)
    at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:482)
    at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:440)
    at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.deserializeWorker(SystemApplicationClassLoaderWorker.java:153)
    at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:121)
    at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:71)
    at worker.org.gradle.process.internal.worker.GradleWorkerMain.run(GradleWorkerMain.java:69)
    at worker.org.gradle.process.internal.worker.GradleWorkerMain.main(GradleWorkerMain.java:74)
5 Answers

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.*']
    }
}

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
}

}

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

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

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

For anyone looking how the fix look like for Kotlin DSL - working solution:

import com.android.build.gradle.BaseExtension
import org.gradle.kotlin.dsl.configure
import org.gradle.testing.jacoco.plugins.JacocoTaskExtension

configure<BaseExtension> {
    testOptions {
        unitTests {
            all { test ->
                test.extensions.configure<JacocoTaskExtension> {
                    excludes = listOf("jdk.internal.*")
                }
            }
        }
    }
}
Related