Mockk: Bug: Newer Objenesis 2.7 introduced in 1.9.3 is failing in Android Instrumented Tests

Created on 28 Mar 2019  路  29Comments  路  Source: mockk/mockk

  • [x] I am running the latest version
  • [x] I checked the documentation and found no answer
  • [x] I checked to make sure that this issue has not already been filed

Failure Information (for bugs)

Building a test apk fails with MockK 1.9.3.
No errors occur with MockK 1.9.2.

Context

  • MockK version: 1.9.3
  • Kotlin version: 1.3.21
  • JDK version: openjdk version "1.8.0_191"
  • Type of test: android instrumented test

This error occurs when building a test apk, not when running instrumented tests.

Expected Behavior

The build should be successful.

Current Behavior

The build fails:

$ ./gradlew :examples:mockk:assembleAndroidTest
D8: MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O (--min-api 26)

> Task :examples:mockk:transformClassesWithDexBuilderForDebugAndroidTest FAILED
com.android.builder.dexing.DexArchiveBuilderException: com.android.builder.dexing.DexArchiveBuilderException: Failed to process /home/tmura/.gradle/caches/modules-2/files-2.1/org.objenesis/objenesis/3.0.1/11cfac598df9dc48bb9ed9357ed04212694b7808/objenesis-3.0.1.jar
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at java.util.concurrent.ForkJoinTask.getThrowableException(ForkJoinTask.java:593)
        at java.util.concurrent.ForkJoinTask.reportException(ForkJoinTask.java:677)
        at java.util.concurrent.ForkJoinTask.join(ForkJoinTask.java:720)
        at com.android.ide.common.internal.WaitableExecutor.waitForTasksWithQuickFail(WaitableExecutor.java:146)
        at com.android.build.gradle.internal.transforms.DexArchiveBuilderTransform.transform(DexArchiveBuilderTransform.java:420)
        at com.android.build.gradle.internal.pipeline.TransformTask$2.call(TransformTask.java:239)
        at com.android.build.gradle.internal.pipeline.TransformTask$2.call(TransformTask.java:235)
        at com.android.builder.profile.ThreadRecorder.record(ThreadRecorder.java:102)
        at com.android.build.gradle.internal.pipeline.TransformTask.transform(TransformTask.java:230)
        at sun.reflect.GeneratedMethodAccessor172.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73)
        at org.gradle.api.internal.project.taskfactory.IncrementalTaskAction.doExecute(IncrementalTaskAction.java:50)
        at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:39)
        at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:26)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$1.run(ExecuteActionsTaskExecuter.java:131)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:301)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:293)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:175)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:91)
        at org.gradle.internal.operations.DelegatingBuildOperationExecutor.run(DelegatingBuildOperationExecutor.java:31)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:120)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:99)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:77)
        at org.gradle.api.internal.tasks.execution.OutputDirectoryCreatingTaskExecuter.execute(OutputDirectoryCreatingTaskExecuter.java:51)
        at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:59)
        at org.gradle.api.internal.tasks.execution.ResolveTaskOutputCachingStateExecuter.execute(ResolveTaskOutputCachingStateExecuter.java:54)
        at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:59)
        at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:101)
        at org.gradle.api.internal.tasks.execution.FinalizeInputFilePropertiesTaskExecuter.execute(FinalizeInputFilePropertiesTaskExecuter.java:44)
        at org.gradle.api.internal.tasks.execution.CleanupStaleOutputsExecuter.execute(CleanupStaleOutputsExecuter.java:91)
        at org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.java:62)
        at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:59)
        at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:54)
        at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
        at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:34)
        at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter$1.run(EventFiringTaskExecuter.java:51)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:301)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:293)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:175)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:91)
        at org.gradle.internal.operations.DelegatingBuildOperationExecutor.run(DelegatingBuildOperationExecutor.java:31)
        at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter.execute(EventFiringTaskExecuter.java:46)
        at org.gradle.execution.taskgraph.LocalTaskInfoExecutor.execute(LocalTaskInfoExecutor.java:42)
        at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$BuildOperationAwareWorkItemExecutor.execute(DefaultTaskExecutionGraph.java:277)
        at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$BuildOperationAwareWorkItemExecutor.execute(DefaultTaskExecutionGraph.java:262)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$ExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:135)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$ExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:130)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$ExecutorWorker.execute(DefaultTaskPlanExecutor.java:200)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$ExecutorWorker.executeWithWork(DefaultTaskPlanExecutor.java:191)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$ExecutorWorker.run(DefaultTaskPlanExecutor.java:130)
        at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
        at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
        at java.lang.Thread.run(Thread.java:748)
Caused by: com.android.builder.dexing.DexArchiveBuilderException: Failed to process /home/tmura/.gradle/caches/modules-2/files-2.1/org.objenesis/objenesis/3.0.1/11cfac598df9dc48bb9ed9357ed04212694b7808/objenesis-3.0.1.jar
        at com.android.build.gradle.internal.transforms.DexArchiveBuilderTransform.launchProcessing(DexArchiveBuilderTransform.java:909)
        at com.android.build.gradle.internal.transforms.DexArchiveBuilderTransform.lambda$convertToDexArchive$6(DexArchiveBuilderTransform.java:834)
        at java.util.concurrent.ForkJoinTask$AdaptedCallable.exec(ForkJoinTask.java:1424)
        at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
        at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
        at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
        at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
Caused by: com.android.builder.dexing.DexArchiveBuilderException: Error while dexing.
        at com.android.builder.dexing.D8DexArchiveBuilder.getExceptionToRethrow(D8DexArchiveBuilder.java:124)
        at com.android.builder.dexing.D8DexArchiveBuilder.convert(D8DexArchiveBuilder.java:101)
        at com.android.build.gradle.internal.transforms.DexArchiveBuilderTransform.launchProcessing(DexArchiveBuilderTransform.java:904)
        ... 6 more
Caused by: com.android.tools.r8.CompilationFailedException: Compilation failed to complete
        at com.android.tools.r8.utils.ExceptionUtils.withCompilationHandler(ExceptionUtils.java:70)
        at com.android.tools.r8.utils.ExceptionUtils.withD8CompilationHandler(ExceptionUtils.java:43)
        at com.android.tools.r8.D8.run(D8.java:94)
        at com.android.builder.dexing.D8DexArchiveBuilder.convert(D8DexArchiveBuilder.java:99)
        ... 7 more
Caused by: com.android.tools.r8.utils.AbortException: Error: MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O (--min-api 26)
        at com.android.tools.r8.utils.Reporter.failIfPendingErrors(Reporter.java:89)
        at com.android.tools.r8.utils.Reporter.fatalError(Reporter.java:60)
        at com.android.tools.r8.utils.ExceptionUtils.withCompilationHandler(ExceptionUtils.java:64)
        ... 10 more


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':examples:mockk:transformClassesWithDexBuilderForDebugAndroidTest'.
> com.android.build.api.transform.TransformException: com.android.builder.dexing.DexArchiveBuilderException: com.android.builder.dexing.DexArchiveBuilderException: Failed to process /home/tmura/.gradle/caches/modules-2/files-2.1/org.objenesis/objenesis/3.0.1/11cfac598df9dc48bb9ed9357ed04212694b7808/objenesis-3.0.1.jar

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 14s
47 actionable tasks: 47 executed

Steps to reproduce

  1. Run git clone https://github.com/tmurakami/dexopener.git && cd dexopener
  2. Change mockk-android:1.9.2 near the end of the root build.gradle to mockk-android:1.9.3
  3. Run ./gradlew :examples:mockk:assembleAndroidTest

The cause is that Objenesis 3.0.1 on which MockK 1.9.3 relies contains code using java.lang.invoke.MethodHandle that is not present in versions prior to Android 8.0.
https://github.com/easymock/objenesis/blob/8f601c8ba1aa20bbc640e2c2bc48d55df52c489f/main/src/main/java/org/objenesis/instantiator/util/DefineClassHelper.java

ait bug important

Most helpful comment

Any news on this?

This can be worked-around globally by forcing objenesis 2.6:

allprojects {
  configurations.all {
    resolutionStrategy {
      force("org.objenesis:objenesis:2.6")
    }
  }
}

All 29 comments

Thank you for the report

Having the same issue. Actually came to report it. Please help look into. Thanks!

I saw some Stack Overflow posts where people got around this type of error (not this one specifically) by specifying in gradle:

compileOptions {
        sourceCompatibility JavaVersion.VERSION_<whatever version is needed>
        targetCompatibility JavaVersion.VERSION_<whatever version is needed>
    }

but I'm not sure what jdk is required for java.lang.invoke.MethodHandle; the ones I tried didn't fix the issue.

A temporary workaround for this issue is to downgrade Objenesis to version 2.6, which does not contain code that calls MethodHandle.invokeExact.

androidTestImplementation('io.mockk:mockk-android:1.9.3') { exclude module: 'objenesis' }
androidTestImplementation 'org.objenesis:objenesis:2.6'

Are there any updates on this? We have the same issue as well. None of the temporary workarounds work for us

If there is no workarounds I'll try to address this one today/tomorrow

Any news on this?

This can be worked-around globally by forcing objenesis 2.6:

allprojects {
  configurations.all {
    resolutionStrategy {
      force("org.objenesis:objenesis:2.6")
    }
  }
}

Any fix?

+1

+1

I have the same problem. +1

+1

+1

Instead of adding +1 (which notifies everybody who is subscribed to this issue), you can thumb up the original description:

image

Apologies for THIS comment notifying everybody, but it's for the greater good (to reduce future notifications :stuck_out_tongue: )

rolling back to 1.9.2 is another workaround.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. If you are sure that this issue is important and should not be marked as stale just ask to put an important label.

@oleksiyp please put an important label.

This is a major blocking to all Android consumers.
I hope it will be solved at some point or we need to stick with @PaulWoitaschek workaround forever...

I decided to give Mockk a try and this is the first error i get. I assume every user with 1.9.3 gets this error.

Unfortunately yes, use 1.9.2 or workaround. This issue is a result of merging contributed code and the fact that I am not experienced enough to build an automated test pipeline for Android Instrumented testing.

Furthermore, I think there is no easy way because the version of Objenesis before 3.0.1 doesn't support Java 11(or Java 12, don't remember) and version 3.0.1 doesn't support AIT.

So one of ways would be to create bug in Objenesis to fix the problem there and afterwards just bump version to newer. In that way, it would be easy enough to take burden on testing and releasing a new version.

I am facing similar issue. Any fix for this yet?

Another temporary workaround for testing on devices API 26+ (which supports mentioned java 8 features)

pros: no manipulations with dependencies
cons: devices from yourMinSdk to API 26 remains out of the scope

Solution: use product flavors to override minSdk:

    flavorDimensions "testing"
    productFlavors {
        product {
            // nothing
        }
        // Temp fix for issue https://github.com/mockk/mockk/issues/281
        uiTest {
            dimension "testing"
            minSdkVersion 26
        }
    }

Then we can run AndroidTests with gradle:

gradlew connectedUiTestDebug

P.S. due to our manipulations with flavors Unit tests now runs as follows:

gradlew testProductDebug
or 
gradlew testProductRelease

None of the workarounds are doing anything for me. It would be good to get this sorted, as running instrumentation tests on API >= 26 only is not ideal. Thank you.

This issue has been fixed in v1.10.0.
Thank you!

@tmurakami I fail to see what did the v1.10.0 do about this issue ? I just tried but it still behave exactly like 1.93.0

EDIT: Ok it did fix it, if anyone still has the issue be sure to remove the dependency to
androidTestImplemention "io.mockk:mockk:1.10.0"
which should leave only
androidTestImplemention "io.mockk:mockk-android:1.10.0"

I just want to add a remark to this issue.

This thing with 1.9.3 happened by accident. It was a merged PR that was not properly tested (as one reason is because of the lack of automation for the instrumented tests). So big sorry regarding this trouble.

Recently for version 1.10.0, I was fighting not to raise minSdkVersion.
The trick was to use old Objenesis as a dependency for instrumented tests and new Objenesis for everything else (JVM/Android unit test). And this time I devoted quite a lot of time to testing.

Same problem with 1.10.0. Downgrade to 1.9.0 solve my problem.

Same problem with 1.10.0

removing androidTestImplementation "io.mockk:mockk" and just add androidTestImplementation "io.mockk:mockk-android worked

removing androidTestImplementation "io.mockk:mockk" and just add androidTestImplementation "io.mockk:mockk-android worked

Thank you, it worked for me.

Was this page helpful?
0 / 5 - 0 ratings