skip to Main Content

Test Events were not Received Error:

NoClassDefFoundError on running unit test after updating android studio to ArticFox | 2020.3.1.

Below is the full error:

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:2180)
at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1679)
at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:493)
at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:451)
at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:122)
at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:69)
at worker.org.gradle.process.internal.worker.GradleWorkerMain.run(GradleWorkerMain.java:68)
at worker.org.gradle.process.internal.worker.GradleWorkerMain.main(GradleWorkerMain.java:73)

Caused by: java.lang.ClassNotFoundException: jdk.internal.reflect.GeneratedSerializationConstructorAccessor1
at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:471)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 11 more

I have just downloaded the latest android studio, clone a project and trying to run unit tests.

How can I fix this? I am using embedded JDK (which is Java 11.0).

3

Answers


  1. Chosen as BEST ANSWER

    It was happening due to the jacoco. Latest android studio comes with Java 11 but we were using Java 9. Making below changes fixed it.

    jacoco { includeNoLocationClasses = true }
    

    to

    jacoco {
       includeNoLocationClasses = true
       excludes = ['jdk.internal.*'] 
    }
    

  2. Android Studio now using JDK 11. So, if you are using a lower version of JDK and JaCoCo, it can be a solution:

    You need to add the following line to the JaCoCo block(s) in your build.gradle file:

    excludes = ['jdk.internal.*']
    

    So, the final blocks look likes this:

    jacoco {
        includeNoLocationClasses = true
        excludes = ['jdk.internal.*']
    }
    

    You can find the reason in release note:

    When running in Android Studio, the Gradle build tool uses Studio’s bundled JDK. In previous releases, JDK 8 was bundled with Studio. In 4.2, however, JDK 11 is now bundled instead. When using the new bundled JDK to run Gradle, this may result in some incompatibility or impact JVM performance due to changes to the garbage collector. These issues are described in the AGP release notes.

    Login or Signup to reply.
  3. For anyone looking how it is done in Kotlin:

    tasks.withType<Test> {
        extensions.configure(JacocoTaskExtension::class) {
            isIncludeNoLocationClasses = true
            excludes = listOf("jdk.internal.*")
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search