问题
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.
回答1:
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.
回答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.
回答3:
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
来源:https://stackoverflow.com/questions/50633710/how-to-find-cause-of-error-when-dexing-methodhandle-invoke-and-methodhandle-inv