How to find cause of error when dexing: MethodHandle.invoke and MethodHandle.invokeExact
Asked Answered
H

7

11

I have a project that has started throwing this error when building in Android Studio or Gradle:

com.android.tools.r8.ApiLevelException: MethodHandle.invoke and MethodHandle.invokeExact are only supported starting with Android O (--min-api 26)

Now I assume that it's related to use of Java 8 features, perhaps a lambda, but the error message gives no clue as to where the problem lies - it could be my code or it could be a library.

What's the best way to find out where the offending code is? It's a reasonably large app with several modules and quite a few libraries.

Hebraize answered 31/5, 2018 at 22:33 Comment(0)
S
5

In my case i tried to mock something via mockk in an instrumentation test running Api Version 26 or higher, removing the dependency was my only solution as long as following issue is not resolved: https://github.com/mockk/mockk/issues/281


Update: It seems that the issue got resolved and using mockk version 1.10.0 will resolve this issue

Steersman answered 27/3, 2020 at 19:56 Comment(0)
T
2

One approach would be to set your min api to 26, to avoid the error and build the apk, and then inspect the bytecode of the app (dexdump, baksmali, etc.) to find any usages of the MethodHandle class.

Timberland answered 31/5, 2018 at 22:46 Comment(0)
H
2

Looking through the error log, it did give a bit more information, specifically nominating one jar file that it failed on:

/Users/.../androidApp/build/intermediates/transforms/desugar/debug/41.jar

On examining that jar file, the classes were all from a library recently added to the project, and the very first class listed was:

META-INF/versions/9/org/h2/util/Bits.class

This turns out to be a Java 9 customised class. Deleting it from the jar file (using zip -d) solved the problem.

Hebraize answered 31/5, 2018 at 23:34 Comment(2)
Can you please mention how you got the idea that this jar is causing the issue?Laurenlaurena
@iDroidExplorer I already said how I found it - I looked through the error log.Hebraize
M
0

Just change the mockk version to 1.10.0, this will resolve the

Madea answered 5/6, 2020 at 10:4 Comment(0)
I
0

I have same case when used too high version of Mockito library. The solution was downgrading from 3.8.0 to 3.4.6. and everything started to work properly.

Error occurred for:

org.mockito:mockito-android:3.8.0

but work for:

org.mockito:mockito-android:3.4.6
Irretentive answered 5/3, 2021 at 15:4 Comment(0)
S
0

Downgrading "com.google.gms:google-services" from 4.4.0 to 4.3.15 helped me

Swiger answered 21/9, 2023 at 13:16 Comment(0)
E
-2

We need to update the java version to java 8. For that please click on File -> Project structure -> app (under Module folder) -> Then select the source compatibility and target compatibility to java 8. After that you might encounter another error with MethodHandle.invoke and MethodHandle.invokeExact, if you get this error then set the minSdkVersion to 26 or higher

Eugine answered 29/8, 2019 at 19:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.