0

拥有一个 android 库项目,它已使用 android studio 4.1.2 构建并进行了单元测试。现在改为使用android studio (fox) 2020.3.1 path 3。

它开始出现错误Cannot run gradle test tasks because of java.lang.NoClassDefFoundError: jdk/internal/reflect/GeneratedSerializationConstructorAccessor1

添加后task.jacoco.excludes = ['jdk.internal.*']

plugins.withId("jacoco") {
        tasks.withType(Test) { task ->
            task.jacoco.includeNoLocationClasses = true
            task.jacoco.excludes = ['jdk.internal.*'] //<=== added this line
        }
    }

错误消失了,但现在得到了:

java.lang.instrument.IllegalClassFormatException: Error while instrumenting com/mobile/notification/NotificationModule.
        at org.jacoco.agent.rt.internal_f3994fa.CoverageTransformer.transform(CoverageTransformer.java:94)
        at java.instrument/java.lang.instrument.ClassFileTransformer.transform(ClassFileTransformer.java:246)
        at java.instrument/sun.instrument.TransformerManager.transform(TransformerManager.java:188)
        at java.instrument/sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:563)
        at java.base/java.lang.ClassLoader.defineClass1(Native Method)
        at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
        at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:151)
        at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:802)
        at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:700)
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:623)
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
.........

        Caused by: java.io.IOException: Error while instrumenting com/mobile/notification/NotificationModule.
        at org.jacoco.agent.rt.internal_f3994fa.core.instr.Instrumenter.instrumentError(Instrumenter.java:160)
        at org.jacoco.agent.rt.internal_f3994fa.core.instr.Instrumenter.instrument(Instrumenter.java:110)
        at org.jacoco.agent.rt.internal_f3994fa.CoverageTransformer.transform(CoverageTransformer.java:92)
        ... 64 more
        Caused by: java.lang.IllegalStateException: Cannot process instrumented class com/mobile/notification/NotificationModule. Please supply original non-instrumented classes.
        at org.jacoco.agent.rt.internal_f3994fa.core.internal.instr.InstrSupport.assertNotInstrumented(InstrSupport.java:238)
        at org.jacoco.agent.rt.internal_f3994fa.core.internal.instr.ClassInstrumenter.visitField(ClassInstrumenter.java:56)
        at org.jacoco.agent.rt.internal_f3994fa.asm.ClassVisitor.visitField(ClassVisitor.java:339)
        at org.jacoco.agent.rt.internal_f3994fa.asm.ClassReader.readField(ClassReader.java:1111)
        at org.jacoco.agent.rt.internal_f3994fa.asm.ClassReader.accept(ClassReader.java:713)
        at org.jacoco.agent.rt.internal_f3994fa.asm.ClassReader.accept(ClassReader.java:401)
        at org.jacoco.agent.rt.internal_f3994fa.core.instr.Instrumenter.instrument(Instrumenter.java:90)
        at org.jacoco.agent.rt.internal_f3994fa.core.instr.Instrumenter.instrument(Instrumenter.java:108)
        ... 65 more

[Robolectric] WARN: Android SDK 16 requires Java 8 (have Java 1). Tests won't be run on SDK 16 unless explicitly requested.
[Robolectric] WARN: Android SDK 17 requires Java 8 (have Java 1). Tests won't be run on SDK 17 unless explicitly requested.
[Robolectric] WARN: Android SDK 18 requires Java 8 (have Java 1). Tests won't be run on SDK 18 unless explicitly requested.
[Robolectric] WARN: Android SDK 19 requires Java 8 (have Java 1). Tests won't be run on SDK 19 unless explicitly requested.
[Robolectric] WARN: Android SDK 21 requires Java 8 (have Java 1). Tests won't be run on SDK 21 unless explicitly requested.
[Robolectric] WARN: Android SDK 22 requires Java 8 (have Java 1). Tests won't be run on SDK 22 unless explicitly requested.
[Robolectric] WARN: Android SDK 23 requires Java 8 (have Java 1). Tests won't be run on SDK 23 unless explicitly requested.
[Robolectric] WARN: Android SDK 24 requires Java 8 (have Java 1). Tests won't be run on SDK 24 unless explicitly requested.
[Robolectric] WARN: Android SDK 25 requires Java 8 (have Java 1). Tests won't be run on SDK 25 unless explicitly requested.
[Robolectric] WARN: Android SDK 26 requires Java 8 (have Java 1). Tests won't be run on SDK 26 unless explicitly requested.
[Robolectric] WARN: Android SDK 27 requires Java 8 (have Java 1). Tests won't be run on SDK 27 unless explicitly requested.
[Robolectric] WARN: Android SDK 28 requires Java 8 (have Java 1). Tests won't be run on SDK 28 unless explicitly requested.
[Robolectric] WARN: Android SDK 29 requires Java 9 (have Java 1). Tests won't be run on SDK 29 unless explicitly requested.

Failed to create a Robolectric sandbox: Android SDK 28 requires Java 8 (have Java 1)
java.lang.UnsupportedOperationException: Failed to create a Robolectric sandbox: Android SDK 28 requires Java 8 (have Java 1)
    at org.robolectric.RobolectricTestRunner.getSandbox(RobolectricTestRunner.java:265)
    at org.robolectric.RobolectricTestRunner.getSandbox(RobolectricTestRunner.java:63)
    at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:215)
    at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.robolectric.internal.SandboxTestRunner$1.evaluate(SandboxTestRunner.java:96)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:110)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:58)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:38)
    at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:62)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33)
    at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94)
    at com.sun.proxy.$Proxy5.processTestClass(Unknown Source)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:119)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:182)
    at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:164)
    at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:414)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
    at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56)
    at java.base/java.lang.Thread.run(Thread.java:835)


该项目有

compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip


android {
    compileSdkVersion 30
    buildToolsVersion 30.0.0

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName version

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        
    }

和其他相关:


        classpath "com.android.tools.build:gradle:4.1.2"

        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.30"
        classpath 'com.google.gms:google-services:4.3.4'
        classpath "org.jacoco:org.jacoco.core:0.8.7"

...
testImplementation "junit:junit:4.13.2"
testApi "org.robolectric:robolectric:4,3,1"

错误IllegalStateException: Cannot process instrumented class ... Please supply original non-instrumented classes.是什么意思?为什么它抱怨它有have Java 1指定的地方JavaVersion.VERSION_1_8

在项目的 resourcec 文件夹中,它还必须robolectric.properties使用sdk=28api 版本 28 来询问它。

问题是可能缺少什么,因为它在 android studio 4.1.2 中运行良好。

4

0 回答 0