Repository: mockito/mockito
Issue: Android instrumentation test packaging fails for mockito-android 3.5.0 with minSdk < 26
Labels: android
please contribute

We've upgraded from `mockito-android:3.4.6` to `mockito-android:3.5.0`. I'm attempting to package up my instrumented tests by running the `assembleAndroidTest` gradle task. The task fails with the following errors:
```
/Users/<me>/.gradle/caches/transforms-2/files-2.1/fb7959dc5bfc8ebb26ad0660efa1929e/jetified-mockito-core-3.5.0.jar: D8: MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O (--min-api 26)
/Users/<me>/.gradle/caches/transforms-2/files-2.1/4d4498fa5552ba637b5a9594a0871b80/jetified-objenesis-3.1.jar: D8: MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O (--min-api 26)
/Users/<me>/.gradle/caches/transforms-2/files-2.1/4d4498fa5552ba637b5a9594a0871b80/jetified-objenesis-3.1.jar: D8: MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O (--min-api 26)
```

and with root cause:
```
com.android.tools.r8.utils.b: Error: /Users/<me>/.gradle/caches/transforms-2/files-2.1/4d4498fa5552ba637b5a9594a0871b80/jetified-objenesis-3.1.jar:org/objenesis/instantiator/util/DefineClassHelper$Java8.class, java.lang.Class org.objenesis.instantiator.util.DefineClassHelper$Java8.defineClass(java.lang.String, byte[], int, int, java.lang.Class, java.lang.ClassLoader, java.security.ProtectionDomain), MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O (--min-api 26)
        at com.android.tools.r8.utils.y0.a(:21)
        at com.android.tools.r8.utils.O.a(:51)
        ... 35 more
```



Issue comments:
Should be fixed with #2006
We still have issues with 3.5.2. We are unable to update to new version and use `assembleAndroidTest` task.

```
/usr/local/gradle/caches/transforms-2/files-2.1/225c46d0e9f10ff5ef3f4d9a009c4924/jetified-objenesis-3.1.jar: D8: MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O (--min-api 26)

com.android.tools.r8.errors.a: MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O (--min-api 26)

```
@kozaxinan Are you certain that you are using 3.5.2 and aren't accidentally using an older version of either Mockito or ByteBuddy? We have had confirmations from other users that 3.5.2 fixed the issue you are seeing, so it would be good to double check that you have the correct version.
@TimvdLippe I confirm the issue still occurs also in our project with the exact same error.
- mockito-core 3.5.2
- mockito-android 3.5.2
- mockito-inline 3.5.2
- objenesis 3.1
- bytebuddy 1.10.13
Looks like the issue was introduced in objenesis 3.x (upgraded in https://github.com/mockito/mockito/pull/1998)
Forcing objenesis to 2.6 makes `assembleAndroidTest` task succeed.
Aha, thanks for letting us know. I will create a revert.
Still getting this error with 3.5.7
Here's the stacktrace that I get when using 3.5.7:

```
/Users/azizbekian/.gradle/caches/transforms-2/files-2.1/63e24576c0e1d1e5b675c1c61e4e5727/jetified-objenesis-3.1.jar: D8: MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O (--min-api 26)
Stack trace:
com.android.tools.r8.errors.a: MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O (--min-api 26)
        at com.android.tools.r8.errors.a.a(:7)
        at com.android.tools.r8.ir.conversion.O.b(:58)
        at com.android.tools.r8.ir.conversion.O.a(:104)
        at com.android.tools.r8.ir.conversion.O.a(:53)
        at com.android.tools.r8.graph.C.b(:43)
        at com.android.tools.r8.ir.conversion.O.b(:35)
        at com.android.tools.r8.utils.U0.a(:10)
        at com.google.common.util.concurrent.TrustedListenableFutureTask$TrustedFutureInterruptibleTask.runInterruptibly(TrustedListenableFutureTask.java:125)
        at com.google.common.util.concurrent.InterruptibleTask.run(InterruptibleTask.java:69)
        at com.google.common.util.concurrent.TrustedListenableFutureTask.run(TrustedListenableFutureTask.java:78)
        at com.google.common.util.concurrent.MoreExecutors$DirectExecutorService.execute(MoreExecutors.java:322)
        at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
        at com.google.common.util.concurrent.AbstractListeningExecutorService.submit(AbstractListeningExecutorService.java:66)
        at com.google.common.util.concurrent.AbstractListeningExecutorService.submit(AbstractListeningExecutorService.java:36)
        at com.android.tools.r8.utils.U0.a(:3)
        at com.android.tools.r8.ir.conversion.O.a(:10)
        at com.android.tools.r8.D8.d(:29)
        at com.android.tools.r8.D8.b(:1)
        at com.android.tools.r8.utils.W.a(:30)
        at com.android.tools.r8.D8.run(:11)
        at com.android.builder.dexing.D8DexArchiveBuilder.convert(D8DexArchiveBuilder.java:116)
        at com.android.build.gradle.internal.dependency.BaseDexingTransform.process(DexingTransform.kt:296)
        at com.android.build.gradle.internal.dependency.BaseDexingTransform.processNonIncrementally(DexingTransform.kt:243)
        at com.android.build.gradle.internal.dependency.BaseDexingTransform.doTransform(DexingTransform.kt:153)
        at com.android.build.gradle.internal.dependency.BaseDexingTransform.access$doTransform(DexingTransform.kt:69)
        at com.android.build.gradle.internal.dependency.BaseDexingTransform$transform$1.invoke(DexingTransform.kt:104)
        at com.android.build.gradle.internal.dependency.BaseDexingTransform$transform$1.invoke(DexingTransform.kt:69)
        at com.android.build.gradle.internal.tasks.BlocksUtilsKt.recordArtifactTransformSpan(BlocksUtils.kt:33)
        at com.android.build.gradle.internal.dependency.BaseDexingTransform.transform(DexingTransform.kt:100)
        at org.gradle.api.internal.artifacts.transform.DefaultTransformer.transform(DefaultTransformer.java:193)
        at org.gradle.api.internal.artifacts.transform.DefaultTransformerInvocationFactory$TransformerExecution.execute(DefaultTransformerInvocationFactory.java:332)
        at org.gradle.internal.execution.steps.ExecuteStep.lambda$execute$0(ExecuteStep.java:32)
        at java.util.Optional.map(Optional.java:215)
        at org.gradle.internal.execution.steps.ExecuteStep.execute(ExecuteStep.java:32)
        at org.gradle.internal.execution.steps.ExecuteStep.execute(ExecuteStep.java:26)
        at org.gradle.internal.execution.steps.CleanupOutputsStep.execute(CleanupOutputsStep.java:67)
        at org.gradle.internal.execution.steps.CleanupOutputsStep.execute(CleanupOutputsStep.java:36)
        at org.gradle.internal.execution.steps.ResolveInputChangesStep.execute(ResolveInputChangesStep.java:49)
        at org.gradle.internal.execution.steps.ResolveInputChangesStep.execute(ResolveInputChangesStep.java:34)
        at org.gradle.internal.execution.steps.CancelExecutionStep.execute(CancelExecutionStep.java:43)
        at org.gradle.internal.execution.steps.TimeoutStep.executeWithoutTimeout(TimeoutStep.java:73)
        at org.gradle.internal.execution.steps.TimeoutStep.execute(TimeoutStep.java:54)
        at org.gradle.internal.execution.steps.CatchExceptionStep.execute(CatchExceptionStep.java:34)
        at org.gradle.internal.execution.steps.CreateOutputsStep.execute(CreateOutputsStep.java:44)
        at org.gradle.internal.execution.steps.SnapshotOutputsStep.execute(SnapshotOutputsStep.java:54)
        at org.gradle.internal.execution.steps.SnapshotOutputsStep.execute(SnapshotOutputsStep.java:38)
        at org.gradle.internal.execution.steps.BroadcastChangingOutputsStep.execute(BroadcastChangingOutputsStep.java:49)
        at org.gradle.internal.execution.steps.CacheStep.executeWithoutCache(CacheStep.java:159)
        at org.gradle.internal.execution.steps.CacheStep.execute(CacheStep.java:72)
        at org.gradle.internal.execution.steps.CacheStep.execute(CacheStep.java:43)
        at org.gradle.internal.execution.steps.StoreExecutionStateStep.execute(StoreExecutionStateStep.java:44)
        at org.gradle.internal.execution.steps.StoreExecutionStateStep.execute(StoreExecutionStateStep.java:33)
        at org.gradle.internal.execution.steps.RecordOutputsStep.execute(RecordOutputsStep.java:38)
        at org.gradle.internal.execution.steps.RecordOutputsStep.execute(RecordOutputsStep.java:24)
        at org.gradle.internal.execution.steps.SkipUpToDateStep.executeBecause(SkipUpToDateStep.java:92)
        at org.gradle.internal.execution.steps.SkipUpToDateStep.lambda$execute$0(SkipUpToDateStep.java:85)
        at java.util.Optional.map(Optional.java:215)
        at org.gradle.internal.execution.steps.SkipUpToDateStep.execute(SkipUpToDateStep.java:55)
        at org.gradle.internal.execution.steps.SkipUpToDateStep.execute(SkipUpToDateStep.java:39)
        at org.gradle.internal.execution.steps.ResolveChangesStep.execute(ResolveChangesStep.java:76)
        at org.gradle.internal.execution.steps.ResolveChangesStep.execute(ResolveChangesStep.java:37)
        at org.gradle.internal.execution.steps.legacy.MarkSnapshottingInputsFinishedStep.execute(MarkSnapshottingInputsFinishedStep.java:36)
        at org.gradle.internal.execution.steps.legacy.MarkSnapshottingInputsFinishedStep.execute(MarkSnapshottingInputsFinishedStep.java:26)
        at org.gradle.internal.execution.steps.ResolveCachingStateStep.execute(ResolveCachingStateStep.java:94)
        at org.gradle.internal.execution.steps.ResolveCachingStateStep.execute(ResolveCachingStateStep.java:49)
        at org.gradle.internal.execution.steps.CaptureStateBeforeExecutionStep.execute(CaptureStateBeforeExecutionStep.java:79)
        at org.gradle.internal.execution.steps.CaptureStateBeforeExecutionStep.execute(CaptureStateBeforeExecutionStep.java:53)
        at org.gradle.internal.execution.steps.ValidateStep.execute(ValidateStep.java:74)
        at org.gradle.internal.execution.steps.SkipEmptyWorkStep.lambda$execute$2(SkipEmptyWorkStep.java:78)
        at java.util.Optional.orElseGet(Optional.java:267)
        at org.gradle.internal.execution.steps.SkipEmptyWorkStep.execute(SkipEmptyWorkStep.java:78)
        at org.gradle.internal.execution.steps.SkipEmptyWorkStep.execute(SkipEmptyWorkStep.java:34)
        at org.gradle.internal.execution.steps.legacy.MarkSnapshottingInputsStartedStep.execute(MarkSnapshottingInputsStartedStep.java:39)
        at org.gradle.internal.execution.steps.LoadExecutionStateStep.execute(LoadExecutionStateStep.java:40)
        at org.gradle.internal.execution.steps.LoadExecutionStateStep.execute(LoadExecutionStateStep.java:28)
        at org.gradle.internal.execution.impl.DefaultWorkExecutor.execute(DefaultWorkExecutor.java:33)
        at org.gradle.api.internal.artifacts.transform.DefaultTransformerInvocationFactory$2.lambda$call$2(DefaultTransformerInvocationFactory.java:200)
        at org.gradle.api.internal.artifacts.transform.DefaultTransformerInvocationFactory.fireTransformListeners(DefaultTransformerInvocationFactory.java:269)
        at org.gradle.api.internal.artifacts.transform.DefaultTransformerInvocationFactory.access$300(DefaultTransformerInvocationFactory.java:79)
        at org.gradle.api.internal.artifacts.transform.DefaultTransformerInvocationFactory$2.call(DefaultTransformerInvocationFactory.java:178)
        at org.gradle.api.internal.artifacts.transform.DefaultTransformerInvocationFactory$2.call(DefaultTransformerInvocationFactory.java:175)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor$CallableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:409)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor$CallableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:399)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor$1.execute(DefaultBuildOperationExecutor.java:157)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:242)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:150)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor.call(DefaultBuildOperationExecutor.java:94)
        at org.gradle.internal.operations.DelegatingBuildOperationExecutor.call(DelegatingBuildOperationExecutor.java:36)
        at org.gradle.api.internal.artifacts.transform.DefaultTransformerInvocationFactory.lambda$doTransform$0(DefaultTransformerInvocationFactory.java:175)
        at org.gradle.api.internal.artifacts.transform.ImmutableTransformationWorkspaceProvider.lambda$withWorkspace$0(ImmutableTransformationWorkspaceProvider.java:81)
        at org.gradle.cache.internal.LockOnDemandCrossProcessCacheAccess.withFileLock(LockOnDemandCrossProcessCacheAccess.java:90)
        at org.gradle.cache.internal.DefaultCacheAccess.withFileLock(DefaultCacheAccess.java:191)
        at org.gradle.cache.internal.DefaultPersistentDirectoryStore.withFileLock(DefaultPersistentDirectoryStore.java:177)
        at org.gradle.cache.internal.DefaultCacheFactory$ReferenceTrackingCache.withFileLock(DefaultCacheFactory.java:201)
        at org.gradle.api.internal.artifacts.transform.ImmutableTransformationWorkspaceProvider.withWorkspace(ImmutableTransformationWorkspaceProvider.java:76)
        at org.gradle.api.internal.artifacts.transform.AbstractCachingTransformationWorkspaceProvider.lambda$withWorkspace$0(AbstractCachingTransformationWorkspaceProvider.java:55)
        at com.google.common.cache.LocalCache$LocalManualCache$1.load(LocalCache.java:4718)
        at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3445)
        at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2194)
        at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2153)
        at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2043)
        at com.google.common.cache.LocalCache.get(LocalCache.java:3851)
        at com.google.common.cache.LocalCache$LocalManualCache.get(LocalCache.java:4713)
        at org.gradle.api.internal.artifacts.transform.AbstractCachingTransformationWorkspaceProvider.withWorkspace(AbstractCachingTransformationWorkspaceProvider.java:55)
        at org.gradle.api.internal.artifacts.transform.DefaultTransformerInvocationFactory.doTransform(DefaultTransformerInvocationFactory.java:175)
        at org.gradle.api.internal.artifacts.transform.DefaultTransformerInvocationFactory.access$000(DefaultTransformerInvocationFactory.java:79)
        at org.gradle.api.internal.artifacts.transform.DefaultTransformerInvocationFactory$1.invoke(DefaultTransformerInvocationFactory.java:141)
        at org.gradle.api.internal.artifacts.transform.CacheableInvocation$1.invoke(CacheableInvocation.java:58)
        at org.gradle.api.internal.artifacts.transform.TransformationOperation.run(TransformationOperation.java:39)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:395)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:387)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor$1.execute(DefaultBuildOperationExecutor.java:157)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:242)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:150)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor.access$1500(DefaultBuildOperationExecutor.java:49)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor$ParentPreservingQueueWorker.execute(DefaultBuildOperationExecutor.java:437)
        at org.gradle.internal.operations.DefaultBuildOperationQueue$WorkerRunnable.runOperation(DefaultBuildOperationQueue.java:231)
        at org.gradle.internal.operations.DefaultBuildOperationQueue$WorkerRunnable.access$600(DefaultBuildOperationQueue.java:173)
        at org.gradle.internal.operations.DefaultBuildOperationQueue$WorkerRunnable$1.create(DefaultBuildOperationQueue.java:210)
        at org.gradle.internal.operations.DefaultBuildOperationQueue$WorkerRunnable$1.create(DefaultBuildOperationQueue.java:204)
        at org.gradle.internal.work.DefaultWorkerLeaseService.withLocks(DefaultWorkerLeaseService.java:180)
        at org.gradle.internal.operations.DefaultBuildOperationQueue$WorkerRunnable.runBatch(DefaultBuildOperationQueue.java:204)
        at org.gradle.internal.operations.DefaultBuildOperationQueue$WorkerRunnable.run(DefaultBuildOperationQueue.java:178)
        at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
        at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48)
        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:56)
        at java.lang.Thread.run(Thread.java:748)
        Suppressed: java.util.concurrent.ExecutionException: com.android.tools.r8.errors.a: MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O (--min-api 26)
                at com.google.common.util.concurrent.AbstractFuture.getDoneValue(AbstractFuture.java:552)
                at com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:513)
                at com.google.common.util.concurrent.FluentFuture$TrustedFuture.get(FluentFuture.java:86)
                at com.android.tools.r8.utils.U0.a(:14)
                at com.android.tools.r8.utils.U0.a(:9)
                ... 114 more
        [CIRCULAR REFERENCE:com.android.tools.r8.errors.a: MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O (--min-api 26)]
Caused by: com.android.tools.r8.a: MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O (--min-api 26)
        at com.android.tools.r8.ir.conversion.N.a(:633)
        at com.android.tools.r8.ir.conversion.N.a(:674)
        at com.android.tools.r8.r.n.A.a(:236)
        at com.android.tools.r8.ir.conversion.o.a(:153)
        at com.android.tools.r8.ir.conversion.N.a(:133)
        at com.android.tools.r8.graph.l.a(:93)
        at com.android.tools.r8.graph.l.a(:71)
        at com.android.tools.r8.graph.l.buildIR(:1)
        at com.android.tools.r8.graph.C0.buildIR(:1)
        at com.android.tools.r8.graph.T.a(:152)
        at com.android.tools.r8.ir.conversion.O.a(:412)
        at com.android.tools.r8.ir.conversion.O.b(:53)
        ... 127 more


```
I updated all mockito packages to 3.5.9 but unfortunately I do get still the same error as above.
This still seems to fail for me:
```
Execution failed for task ':mergeDexGplayDebugAndroidTest'.
> Could not resolve all files for configuration ':gplayDebugAndroidTestRuntimeClasspath'.
   > Failed to transform objenesis-3.1.jar (org.objenesis:objenesis:3.1) to match attributes {artifactType=android-dex, dexing-enable-desugaring=true, dexing-incremental-desugaring-v2=false, dexing-is-debuggable=true, dexing-min-sdk=18, org.gradle.category=library, org.gradle.libraryelements=jar, org.gradle.status=release, org.gradle.usage=java-runtime}.
      > Execution failed for DexingWithClasspathTransform: /home/tobi/.gradle/caches/transforms-2/files-2.1/66df4f0b18795a51947f8881933478f9/jetified-objenesis-3.1.jar.
         > Error while dexing.

```

I removed     
`androidTestImplementation "org.mockito:mockito-core:$mockitoVersion"`
and changed:
```
testImplementation ("com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0") {
        exclude group: "org.mockito", module: "mockito-core"
    }
```

If you want to test it: https://github.com/nextcloud/android/pull/6768

This is still happening in 3.5.10:

- https://pastebin.ubuntu.com/p/rKzc6MVhBf/
- https://github.com/relaycorp/relaynet-gateway-android/pull/120
@gnarea You are depending on Objenesis 3, which is incompatible with Android <26. Please downgrade to Objenesis 2 and depend on `mockito-android`. For more info, see the context in #2024
@TimvdLippe looks like `mockito-core` still has objenesis 3.1 as a dependency. 
```
+--- project :my-project
|    \--- org.mockito:mockito-core:3.5.10
|         +--- net.bytebuddy:byte-buddy:1.10.13
|         +--- net.bytebuddy:byte-buddy-agent:1.10.13
|         \--- org.objenesis:objenesis:3.1

+--- org.mockito:mockito-android:3.5.10
|    +--- org.mockito:mockito-core:3.5.10 (*)
|    +--- net.bytebuddy:byte-buddy-android:1.10.13
|    |    +--- net.bytebuddy:byte-buddy:1.10.13
|    |    \--- com.jakewharton.android.repackaged:dalvik-dx:9.0.0_r3
|    \--- org.objenesis:objenesis:[2.6, 3.0) -> 3.1
```
@francescocervone That is correct. For non-Android projects, they can use Objenesis 3 just fine.
@TimvdLippe it's pretty common for Android projects having some pure JVM Gradle modules, not depending on Android. In that case we must depend on `mockito-core` which depends on Objenesis 3. What do you suggest in that case?

Anyway, even if we don't depend directly on `mockito-core`, we get the same error on `mockito-core` (I suppose that's because `mockito-android` depends on `mockito-core`).
```
> Transform artifact mockito-core-3.5.10.jar (org.mockito:mockito-core:3.5.10) with DexingNoClasspathTransform
D8: Private interface methods are only supported starting with Android N (--min-api 24): java.lang.Object org.mockito.plugins.MemberAccessor.lambda$newInstance$0(java.lang.reflect.Constructor, java.lang.Object[])
Stack trace:
com.android.tools.r8.a: Private interface methods are only supported starting with Android N (--min-api 24): java.lang.Object org.mockito.plugins.MemberAccessor.lambda$newInstance$0(java.lang.reflect.Constructor, java.lang.Object[])

[ stacktrace ]

* What went wrong:
Execution failed for task ':app:mergeExtDexDebugAndroidTest'.
> Could not resolve all files for configuration ':app:debugAndroidTestRuntimeClasspath'.
   > Failed to transform mockito-core-3.5.10.jar (org.mockito:mockito-core:3.5.10) to match attributes {artifactType=android-dex, dexing-enable-desugaring=false, dexing-incremental-desugaring-v2=false, dexing-is-debuggable=true, dexing-min-sdk=21, org.gradle.category=library, org.gradle.libraryelements=jar, org.gradle.status=release, org.gradle.usage=java-runtime}.
      > Execution failed for DexingNoClasspathTransform: /Users/me/.gradle/caches/transforms-2/files-2.1/9ebcc121d44dc6f808610b5ee83414a2/jetified-mockito-core-3.5.10.jar.
         > Error while dexing.
```

Do you need an example project?
@francescocervone That last stacktrace is yet another issue that is new to me, as it complains about private interface methods. That seems to be unrelated to Objenesis. The code in question is https://github.com/mockito/mockito/blob/b6ae6cf12b93ef9445e524224375aab1eb76129d/src/main/java/org/mockito/plugins/MemberAccessor.java#L24-L28

I guess older versions of Android do not support default interface methods. I am not sure what to do here, as that seems to be a problem where Android did not support all features of Java 8. Since Mockito 3 targets Java 8, that would be incompatible with older versions of Android.

Can you use Mockito 2 solely for tests running on older versions of Android?
> Can you use Mockito 2 solely for tests running on older versions of Android?

No, because this is a compilation error which depends on the minimum supported Android version. Almost every Android project should use Mockito 2 until android 26 will be the "generally accepted" minimum sdk supported version (probably in at least 5 years 😄).
@francescocervone What I mean is that you have 2 test suites. 1 testsuite for all tests >= 26 and 1 testsuite for all tests <26. For the testsuite >= 26 you can use Mockito 3 + Objenesis 3, as they support Java 8+. For the testsuite <26 you can use Mockito 2 + Objenesis 2, as they support Java 6+. By splitting up the test suites, you can use modern Mockito for modern Android and older Mockito for older Android. That should work right?
Also to reiterate, we have introduced Animal Sniffer to ensure we don't break Android again. We set the target to 24 per https://github.com/mockito/mockito/pull/2006/files#diff-c197962302397baf3a4cc36463dce5eaR104 which seemed the most reasonable cut-off for Java 8 support that Mockito 3 relies on. Therefore, rather than having a cut-off at 26, we would be talking about a cut-off at 24. I am not sure when the Android community moves past that cut-off?
@TimvdLippe as far as I know you cannot have 2 test suites in the same android project compiling against 2 Android versions. The minimum Android supported version is a property (`minSdkVersion`) defined in the android project's `build.gradle` file through the Android Gradle Plugin.

> I am not sure when the Android community moves past that cut-off? 

Maybe [this chart](https://gs.statcounter.com/android-version-market-share/mobile-tablet/worldwide) can be helpful? In my case, we currently support Android 21 as min SDK.
We could try to update Animal Sniffer to level 21 and see what the effect is. If we can make `mockito-core` compatible with that, without introducing breaking changes for the wider Java ecosystem, then I am open to that. However, when @raphw tried that previously, it turned out that a large portion of `mockito-core` was *already* incompatible. This led us to believe that we would have already broken our users and (since we hadn't received complaints before), 24 felt like the best option.

3.5.0 was a big release and a lot of work went into it to support modern Java, which is probably the reason we have hit numerous issues with it. I wonder how many changes in 3.5.0 affect compatibility compared to previous Mockito releases. Could you clone Mockito and try to work out what changes are required to make Mockito compatible with 21? Then we can assess breakages and go from there.
Reopening this issue, as I think it is clear by now we are going to need support from developers working in the Android ecosystem on figuring out how to resolve these issues.
FYI: at least in my case, the workaround suggested above worked: https://github.com/relaycorp/relaynet-gateway-android/pull/120. But that project doesn't have JVM modules (they're all external deps). We're also supporting API 21+.
@TimvdLippe 
>Could you clone Mockito and try to work out what changes are required to make Mockito compatible with 21?

I will.
If I remember correctly, types like predicate and function are not available which are part of our public API. This can be fixed but requires breaking public API. 
@gnarea , that didn't work for my setup, minSdk = 21:

```
* What went wrong:
Execution failed for task ':app:mergeExtDexDevDebugAndroidTest'.
> Could not resolve all files for configuration ':app:devDebugAndroidTestRuntimeClasspath'.
   > Failed to transform objenesis-3.1.jar (org.objenesis:objenesis:3.1) to match attributes {artifactType=android-dex, dexing-enable-desugaring=true, dexing-incremental-desugaring-v2=false, dexing-is-debuggable=true, dexing-min-sdk=21, org.gradle.category=library, org.gradle.libraryelements=jar, org.gradle.status=release, org.gradle.usage=java-runtime}.
      > Execution failed for DexingWithClasspathTransform: /Users/azizbekian/.gradle/caches/transforms-2/files-2.1/adb8b362b2ccf145263475708478d34b/jetified-objenesis-3.1.jar.
         > Error while dexing.
```
@raphw If I remember correctly, we are already shipping `Optional` and `Function` in our public API prior to 3.5.0. Since Android tests were passing with 3.4.0, I don't think including these symbols in our public API is problematic. Therefore, I don't think we should be changing these types in our API. (This is also what confuses me the most, as to why some parts work and why other parts don't work, even though Animal Sniffer is claiming nothing should in theory work)
I'm not an Android expert. `DefineClassHelper` is only used by specific instantiators that are not used in Android. So ideally this class can be excluded from the compilation by Dex it should solve the problem.
@TimvdLippe I played a little bit with mockito and the issue I mentioned [in this comment](https://github.com/mockito/mockito/issues/2007#issuecomment-688321579) is solved by adding in the android project's build.gradle:
```gradle
android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
```
Android Gradle Plugin 3.0 and higher [supports default interface methods](https://developer.android.com/studio/releases/gradle-plugin#j8-library-desugaring).

---

The objenesis 3.x issue included in `mockito-core` mentioned [here](https://github.com/mockito/mockito/issues/2007#issuecomment-688252464) instead can be avoided by excluding objenesis from `mockito-core` or any library/project depending on `mockito-core`:
```gradle
dependencies {
    androidTestImplementation 'org.mockito:mockito-android:3.5.10'
    androidTestImplementation('org.mockito:mockito-core:3.5.10') {
        exclude group: 'org.objenesis'
    }
    androidTestImplementation(project(':depending-on-mockito-core')) {
        exclude group: 'org.objenesis'
    }
}
```
This workaround is a little bit inconvenient IMHO, but solves the issue.
@francescocervone Thanks for the investigation. With regards to your first solution, can we automatically apply that to `mockito-android` or do users of that artifact need to specify it in their `build.gradle`? For the latter, if you solely depend on `mockito-android` you should be good to go. If that is inconvenient, maybe it can be solved in Objenesis 3 instead (not sure if that is possible).
@TimvdLippe 
> can we automatically apply that to mockito-android or do users of that artifact need to specify it in their build.gradle?

Users need to specify it in their `build.gradle`.

---

> if you solely depend on mockito-android you should be good to go

True, but I think this "solution" is pretty weak. For example, if the project declares [`mockito-kotlin`](https://github.com/nhaarman/mockito-kotlin) dependency, objenesis must be excluded since `mockito-kotlin` depends on `mockito-core`. In general, if the android project depends on some module/project/library depending on `mockito-core`, the workaround must be applied.
Again, I'm not sure but can't you just exclude `DefineClassHelper`? That will solve the problem and no need to remove Objenesis. I don't think I can solve it from the Objenesis side. If I can please tell me why? Deploy an objenesis-android excluding these classes will just make everything really messy.
@henri-tremblay is there a way to exclude a class from the compilation of a library? How can we exclude `DefineClassHelper`?
Even if we were able to exclude that class: Objenesis is built with Java 8 and it's likely to introduce similar issues in future. This circle will end when one of these classes using some unsupported Java 8 feature, will be part of the Objenesis core and we will not be able to just "exclude" it.
@TimvdLippe there is a handier workaround for objenesis 3.x, without excluding it from `mockito-core`.
In root `build.gradle` we can add:
```gradle
allprojects {
    configurations.all {
        resolutionStrategy.force 'org.objenesis:objenesis:2.6'
    }
}
```
@francescocervone I don't know. That's why I was saying "Not an Android expert" :-) Let me ask around. It's sad to downgrade to Android 2. The other solution would be to split objenesis in 2. One with the core instantiators and one with the exotic ones. Can you hack the objenesis jar to remote this class and just compile with the new jar to tell me if it works?
Ok. I had some feedback from @mhausherr. The error is in R8. So we could ignore like this in `proguard-rules.pro`:
```
-assumenosideeffects class org.objenesis.instanciator.util.DefineClassHelper {
    *;
}
```
@henri-tremblay 🤔 It cannot be R8. R8 obfuscates, optimizes and shrinks the code in release builds. It doesn't run in test unless you don't explicitly enable it also for debug builds.
More info here -> https://developer.android.com/studio/build/shrink-code
@jakewharton (if I remember correctly, you know a lot more about minification, R8 and problems like this. If that's not the case, please let me know and feel free to ignore this message)

Hey Jake,

Please see the above messages regarding problems with Mockito transitively depending on Objenesis 3 which broke Android compatibility. However, Objenesis compilation broke, despite the class in question not being used in Android tests. As such, the class could be removed with the proguard configuration @henri-tremblay (maintainer of Objenesis) posted.

@francescocervone thinks that is not the appropriate solution, as R8 should not be performing these kind of cleanups.

Sadly I am lacking the knowledge to figure out what is the appropriate solution here and how we can move forward and I had hoped you would be able to help us figuring out.

Thanks in advance for your time and once again, if you prefer not to be involved, feel free to say so and I will try to find another domain expert 😄 
@TimvdLippe I didn't say that R8 should not be performing these kind of cleanups. I said that R8 is not involved at all, therefore the R8 rule suggested by @henri-tremblay is not a valid workaround. 
@TimvdLippe I created #2053 to fix this issue. With range usage in `strictly` tag, gradle fails to force dependency. I verified with our project. For androidTest, If we just use moctiko-android and exclude mockito-core from mockito-kotlin (or any dependency that has  mockito-core), there shouldnt be any issue. 
This fix still not great but until objenesis supports android sdk < 26, it should work.
For anyone who is looking to contribute, please check out the upstream issue at https://github.com/easymock/objenesis/issues/79
I am going to close this issue in favor of https://github.com/easymock/objenesis/issues/79
I have pushed the changes moving some instantiators in a new project. Could you please check if it works?
Release 3.2 was done. It should work now.
Using version 3.8.0:

```
Execution failed for task ':App:mergeExtDexDebug'.
> Could not resolve all files for configuration ':App:debugRuntimeClasspath'.
   > Failed to transform objenesis-3.1.jar (org.objenesis:objenesis:3.1) to match attributes {artifactType=android-dex, dexing-enable-desugaring=true, dexing-incremental-transform=false, dexing-is-debuggable=true, dexing-min-sdk=21, org.gradle.category=library, org.gradle.libraryelements=jar, org.gradle.status=release, org.gradle.usage=java-runtime}.
      > Execution failed for DexingWithClasspathTransform: /home/circleci/.gradle/caches/modules-2/files-2.1/org.objenesis/objenesis/3.1/48f12deaae83a8dfc3775d830c9fd60ea59bbbca/objenesis-3.1.jar.
         > Error while dexing.
```
Release 3.2 of _Objenesis_ was done, Mockito still needs to have its dependency updated to 3.2. 

This can be worked around by declaring the Objenesis dependency yourself like `implementation 'org.objenesis:objenesis:3.2` and rely on gradle's dependency resolution to go with the 3.2 version instead of the 3.1 currently included with Mockito
@PaulKlauser Do you mind sending us a PR which updates the version?
https://github.com/mockito/mockito/pull/2249
Could you consider publishing this to Maven as well?
Mockito 3.9.0 should be published to Maven Central: https://github.com/mockito/mockito/runs/2288011033?check_suite_focus=true
Thanks! It's still at 3.8.0 on [mvnrepository](https://mvnrepository.com/artifact/org.mockito/mockito-core) and [maven](https://search.maven.org/search?q=a:mockito-core) but I suppose it can take several hours for the change to propagate.
Yes these systems take a while to obtain the new information. 3.9.0 is published here: https://repo1.maven.org/maven2/org/mockito/mockito-core/3.9.0/
Just mentioning that I had [this exact crash](https://github.com/mockito/mockito/issues/2007#issuecomment-686925445) and upgrading to 3.9.0 from 3.8.0 fixed it.

 However, it might be worth mentioning that any Android project that is just using `JCenter` as a repository will not return the right information for latest version. Android projects need to begin adding `mavenCentral()` as a repository, which previously was unnecessary because JCenter mirrored Maven Central. That's changing and it's because of this that my particular project didn't recognize the 3.9.0 update.
```
repositories {
    google()
    mavenCentral()  <-- many projects need to add this, explicitly
    jcenter()
    maven { url 'https://jitpack.io' }
}
```
https://blog.gradle.org/jcenter-shutdown

Pull request review comments:

Patch that resolved the issue:
diff --git a/subprojects/android/android.gradle b/subprojects/android/android.gradle
index bd5a6959a2..508cd7ed05 100644
--- a/subprojects/android/android.gradle
+++ b/subprojects/android/android.gradle
@@ -9,7 +9,7 @@ dependencies {
     compile libraries.bytebuddyandroid
     compile(libraries.objenesis2) {
         version {
-            strictly '[2.6, 3.0['
+            strictly '2.6'
         }
         because(
             '\n' +


Test patch: