5
votes

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
Not all features of Java 1.8 are available for all versions of Android. Some of them require a minSDK of 26. It looks like you're trying to use some of those. You'll either need to remove that code, up your minimum SDK, or rewrite it in a way that doesn't use that feature - Gabe Sechan
Also want to add that this native library works fine in native android application with minSdkVersion of 16. It only has trouble with binding in Xamarin. - Shirley G
Since it's merged already, I would guess in our 15.6 previews. You should be able to download a master version of Xamarin.Android from the open source repository and use it today. github.com/xamarin/xamarin-android#downloads - Jon Douglas
Try to enable via $(AndroidEnableDesugar) included in your .csproj. - Jon Douglas
EX: <AndroidEnableDesugar>true</AndroidEnableDesugar> - Jon Douglas

1 Answers

3
votes

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.