Is Java 8 features available for Xamarin.Android in Java binding project?

夙愿已清 提交于 2020-01-11 08:52:09

问题


I have an android library project that uses Java 8 features, i.e. via

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

When binding the output AAR to Xamarin.Android, the binding project compiles fine. However, when trying to use the binding project in a Xamarin.Android application project, I am getting the following error:

2>COMPILETODALVIK : Uncaught translation error : com.android.dx.cf.code.SimException: invalid opcode ba (invokedynamic requires --min-sdk-version >= 26)
2>COMPILETODALVIK : Uncaught translation error : com.android.dx.cf.code.SimException: invalid opcode ba (invokedynamic requires --min-sdk-version >= 26)
2>COMPILETODALVIK : Uncaught translation error : com.android.dx.cf.code.SimException: invalid opcode ba (invokedynamic requires --min-sdk-version >= 26)
2>COMPILETODALVIK : Uncaught translation error : com.android.dx.cf.code.SimException: invalid opcode ba (invokedynamic requires --min-sdk-version >= 26)
2>COMPILETODALVIK : Uncaught translation error : com.android.dx.cf.code.SimException: invalid opcode ba (invokedynamic requires --min-sdk-version >= 26)
2>COMPILETODALVIK : Uncaught translation error : com.android.dx.cf.code.SimException: invalid opcode ba (invokedynamic requires --min-sdk-version >= 26)
2>COMPILETODALVIK : Uncaught translation error : com.android.dx.cf.code.SimException: invalid opcode ba (invokedynamic requires --min-sdk-version >= 26)
2>COMPILETODALVIK : Uncaught translation error : com.android.dx.cf.code.SimException: invalid opcode ba (invokedynamic requires --min-sdk-version >= 26)
2>COMPILETODALVIK : Uncaught translation error : com.android.dx.cf.code.SimException: invalid opcode ba (invokedynamic requires --min-sdk-version >= 26)
2>  java.lang.RuntimeException: Translation has been interrupted
2>      at com.android.dx.command.dexer.Main.processAllFiles(Main.java:615)
2>      at com.android.dx.command.dexer.Main.runMultiDex(Main.java:368)
2>      at com.android.dx.command.dexer.Main.runDx(Main.java:289)
2>      at com.android.dx.command.dexer.Main.main(Main.java:247)
2>      at com.android.dx.command.Main.main(Main.java:94)
2>  Caused by: java.lang.InterruptedException: Too many errors
2>      at com.android.dx.command.dexer.Main.processAllFiles(Main.java:607)
2>      ... 4 more
2>D:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(2096,3): error : java.lang.RuntimeException :  Translation has been interrupted
========== Build: 1 succeeded, 1 failed, 3 up-to-date, 0 skipped ==========

With some googling, it seems to be related to Java 8 feature. I do use a number of new Java 8 features in the native android library. Is this not supported on Xamarin.Android yet?

I did check my Xamarin Diagnostics output, which shows it is using Java 8 to compile:

[I:sdk]:                    Key HKCU\SOFTWARE\Xamarin\VisualStudio\15.0_8541cfce\Android\JavaSdkDirectory found:
    Path contains jarsigner.exe in \bin (C:\Program Files\Java\jdk1.8.0_161).

Any help is highly appreciated! Thanks.


回答1:


Use the latest Visual Studio 2019 Preview 2.2 and add <AndroidDexTool>d8</AndroidDexTool> into the csproj of your android project, one for each Debug/Release build

D8 is the new dexer made by google and it enable Desugar mode by default.



来源:https://stackoverflow.com/questions/48532610/is-java-8-features-available-for-xamarin-android-in-java-binding-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!