You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Gradle build error when building in android studio #1157
I also encountered this error and think I have found the cause of it.
Apparently, the newest version of the android build tools do not like the call to MethodHandle.invoke in com.graphhopper.util.Helper#cleanMappedByteBuffer
If I comment out the code depending on Constants.JRE_IS_MINIMUM_JAVA9, I can use the built jar in an android project. If I leave it in, it results in the error above.
I think changing the code to use "old-school" reflection would fix the issue.
I meant instead of this: MethodHandle unmapper = MethodHandles.lookup().findVirtual(unsafeClass, "invokeCleaner", MethodType.methodType(void.class, ByteBuffer.class));
Using something like this: Method unmapper = unsafeClass.getDeclaredMethod("invokeCleaner")
it is not raised when using build tools < 26 - which is not the same as the API level.
I am speculating a bit here, but this is how I see it:
When MethodHandle was introduced in API level 26, a check was added to the newest build tools version (also 26 - probably by design, but this does not mean that the build tools only build for API level 26) to check whether they were used in a project that does not target the necessary API level.
why does it work on older phones at all?
Because the code is not executed on android at all - it is only executed for Java 9 (check for Constants.JRE_IS_MINIMUM_JAVA9). However, the build tools can't detect that.
Android Studio 3 release (Gradle 4, Android plugin 3, SDK 27) shows immediately the issue, as cannot use GraphHopper without commenting the aforementioned code.
(see PR #1205 for the latest Android update)
Desugar currently doesn't support MethodHandle.invoke or MethodHandle.invokeExact. If your source code or one of your module dependencies use one of these methods you need to specify minSdkVersion 26 or higher. Otherwise, you get the following error:
Dex: Error converting bytecode to dex:
Cause: signature-polymorphic method called without --min-sdk-version >= 26
In some cases, your module may not be using the invoke or invokeExact methods even when they're included in a library dependency. So, to keep using that library with minSdkVersion 25 or lower, enable code shrinking to remove unused methods. If that doesn't work, consider using an alternative library that doesn't use the unsupported methods.
Activity
karussell commentedon Sep 18, 2017
See this issue comment: #1144 (comment)
If you know the underlying reason and how to fix this - would love to hear about it.
ChristophKaser commentedon Oct 4, 2017
I also encountered this error and think I have found the cause of it.
Apparently, the newest version of the android build tools do not like the call to MethodHandle.invoke in com.graphhopper.util.Helper#cleanMappedByteBuffer
If I comment out the code depending on Constants.JRE_IS_MINIMUM_JAVA9, I can use the built jar in an android project. If I leave it in, it results in the error above.
I think changing the code to use "old-school" reflection would fix the issue.
karussell commentedon Oct 4, 2017
Interesting! Thanks for the update!
What do you mean here?
ChristophKaser commentedon Oct 4, 2017
I meant instead of this:
MethodHandle unmapper = MethodHandles.lookup().findVirtual(unsafeClass, "invokeCleaner", MethodType.methodType(void.class, ByteBuffer.class));Using something like this:
Method unmapper = unsafeClass.getDeclaredMethod("invokeCleaner")I have to admit that I have not tried it though.
karussell commentedon Oct 4, 2017
Strange, as e.g. MethodHandle was introduced in Android API level 26 - exactly where the problems seems to start!
ChristophKaser commentedon Oct 4, 2017
I think that is the point of the build error: It checks whether MethodHandles are used in a project that does not target API level 26.
karussell commentedon Oct 4, 2017
But why is this error not raised when using API level <26 and why does it work on older phones at all?
ChristophKaser commentedon Oct 4, 2017
it is not raised when using build tools < 26 - which is not the same as the API level.
I am speculating a bit here, but this is how I see it:
When MethodHandle was introduced in API level 26, a check was added to the newest build tools version (also 26 - probably by design, but this does not mean that the build tools only build for API level 26) to check whether they were used in a project that does not target the necessary API level.
Because the code is not executed on android at all - it is only executed for Java 9 (check for Constants.JRE_IS_MINIMUM_JAVA9). However, the build tools can't detect that.
karussell commentedon Oct 4, 2017
Thanks, this makes 100% sense!
devemux86 commentedon Nov 1, 2017
Android Studio 3 release (Gradle 4, Android plugin 3, SDK 27) shows immediately the issue, as cannot use GraphHopper without commenting the aforementioned code.
(see PR #1205 for the latest Android update)
According to documentation:
Desugar currently doesn't support MethodHandle.invoke or MethodHandle.invokeExact. If your source code or one of your module dependencies use one of these methods you need to specify minSdkVersion 26 or higher. Otherwise, you get the following error:
In some cases, your module may not be using the invoke or invokeExact methods even when they're included in a library dependency. So, to keep using that library with minSdkVersion 25 or lower, enable code shrinking to remove unused methods. If that doesn't work, consider using an alternative library that doesn't use the unsupported methods.
ChristophKaser commentedon Nov 2, 2017
I just added a PR (#1207) that fixes this issue for the android build and still works in Java 9.