Xamarin-android: Desugar from a new project doesn't work at all (Java 8 > Java 7 Bytecode)

Created on 28 May 2018  Â·  14Comments  Â·  Source: xamarin/xamarin-android

Steps to Reproduce

  1. Create a new project (shared or pcl)
  2. Add in the csproj file sections : < AndroidEnableDesugar >True< /AndroidEnableDesugar >
  3. Generate solution

Expected Behavior

Compile should work

Actual Behavior

Dx fail with Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/google/devtools/build/android/desugar/runtime/ThrowableExtension;

Version Information

Build Tools v 27.0.3
Sdk tools 26.1.1
Platform : All

Log File

1> Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/google/devtools/build/android/desugar/runtime/ThrowableExtension;
1> Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/google/devtools/build/android/desugar/runtime/ThrowableExtension$AbstractDesugaringStrategy;
1> Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/google/devtools/build/android/desugar/runtime/ThrowableExtension$ConcurrentWeakIdentityHashMap;
1> Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/google/devtools/build/android/desugar/runtime/ThrowableExtension$ConcurrentWeakIdentityHashMap$WeakKey;
1> Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/google/devtools/build/android/desugar/runtime/ThrowableExtension$MimicDesugaringStrategy;
1> Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/google/devtools/build/android/desugar/runtime/ThrowableExtension$NullDesugaringStrategy;
1> Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/google/devtools/build/android/desugar/runtime/ThrowableExtension$ReuseDesugaringStrategy;
1> Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/google/devtools/build/android/desugar/runtime/ThrowableExtension;
1> Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/google/devtools/build/android/desugar/runtime/ThrowableExtension$AbstractDesugaringStrategy;
1>
1> UNEXPECTED TOP-LEVEL EXCEPTION:
1> java.lang.RuntimeException: Translation has been interrupted
1> at com.android.dx.command.dexer.Main.processAllFiles(Main.java:615)
1> at com.android.dx.command.dexer.Main.runMonoDex(Main.java:313)
1> at com.android.dx.command.dexer.Main.runDx(Main.java:291)
1> at com.android.dx.command.dexer.Main.main(Main.java:247)
1> at com.android.dx.command.Main.main(Main.java:94)
1> Caused by: java.lang.InterruptedException: Too many errors
1> at com.android.dx.command.dexer.Main.processAllFiles(Main.java:607)
1> ... 4 more
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(2254,3): error : java.lang.IllegalArgumentException: already added : Lcom/google/devtools/build/android/desugar/runtime/ThrowableExtension;
1> DexOutputs:
1>Exécution de la tâche "CompileToDalvik" terminée -- ÉCHEC.
1>Génération de la cible "_CompileToDalvikWithDx" terminée dans le projet "test01.Android.csproj" -- ÉCHEC.
1>
1>ÉCHEC de la build.
1>
1>"c:\mimichat\test01\test01\test01.Android\test01.Android.csproj" (cible par défaut) (1) ->
1>(_CompileToDalvikWithDx cible) ->
1> C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(2254,3): error : java.lang.IllegalArgumentException: already added : Lcom/google/devtools/build/android/desugar/runtime/ThrowableExtension;
1>
1> 0 Avertissement(s)
1> 1 Erreur(s)
1>
1>Temps écoulé 00:00:31.21
========== Génération : 0 a réussi, 1 a échoué, 0 mis à jour, 0 a été ignoré ==========

test01 - Copie.zip

-->

App+Library Build bug

Most helpful comment

@atsushieno , yes i've see that Android studio use d8; and i've see your nugget package , but it doesn't help; D8 with project xamarin seem to don't work. Have you an idea when this will work ? (this year ? on few month ? few day ? )

Thanks

All 14 comments

I can confirm this behavior. @duchnoun I'm curious as to why you are adding desugar support to a new project? Desugar is mostly needed in binding librarys that make use of Java 8 Language features.

Secondly, to ensure proper desugar support for certain APIs, your minSdkVersion needs to be set appropriately. See https://developer.android.com/studio/write/java8-support#supported_features for more details.

For example, if I took your sample and increased the minSdkVersion to API 24, the project will build successfully. We should definitely improve this and throw an error message in this area giving a note about increasing the minSdkVersion rather than the "Already added" from the Xamarin.Android.Common.targets. I've thus marked this as a bug given the current experience.

@JonDouglas , i add desugar on new project only because i've try to identify my problem. The main project is a try to use the AAR library WebRTC , but since november 2017 they have down the java 7 support (before they was Desugar it) ; and now they use java 8 and lambda and other feature from java 8. They tell that now the developper need to desugar the code itself. That's why i would use Desugar ...

Before when they desugar themself , the Api work on API 19 ...

The goal of project is to made a xamarin webrtc plugin with a maximum compatibility

(Sorry , my english is bad .. i'm french)

@duchnoun Do you perhaps have a binding project with the WebRTC .aar included that has desugar enabled so we can look into that further? A file->New project shouldn't require desugar which is why I was asking you in the first place.

Sure, Sorry the size of the archive , the AAR is big.

This project should require desugar operation. It was working before was older api from webrtc because they desugar before ...

The AAR was create by me from sources of webrtc in this month. (But same with precompiled library from webrtc website ..)

Sure i could use an old version, but i want to do a maintain project with recent version of webrtc ..

Some discussion about it here : https://groups.google.com/forum/#!topic/discuss-webrtc/V1h2uQMDCkA

MimichatV2.zip

Please update the work around. I am having same error as @duchnoun

oops, sorry, wrong issue, and this explicitly says true.

@atsushieno: It crashes my application if I desugar is true!

The desugar situation is complicated. Android Gradle Plugin (used in Android Studio) once incorporated desugar_deploy.jar to deal with desugaring. Now Google introduced d8 and quickly abandoned standalone desugar tool. Since Google has already removed desugar_deploy from Android Gradle Plugin setup, we will have to do the same.

Right now d8 is offered as an independent package on NuGet (xamarin-android-d8-build) but d8 will be also part of our standard build implementation along with r8 (Xamarin.Android.Build.Tasks) at https://github.com/xamarin/xamarin-android/pull/1489 (which is suffered from another blocker that the build logs are not investigatable). Things will be resolved accordingly.

@atsushieno , yes i've see that Android studio use d8; and i've see your nugget package , but it doesn't help; D8 with project xamarin seem to don't work. Have you an idea when this will work ? (this year ? on few month ? few day ? )

Thanks

I just recorded an issue to explain current situation regarding this issue. (I feel sorry for inconvenience, it's a problem in maintaining / managing product development which is irrelevant to any of you.)

Any updates on this?

@duchnoun did you work around this using the Google Groups discussion?

So to comment on the original problem:

  • File | New Xamarin.Android Project | Target Android 4.4 KitKit (API 19)
  • Add <AndroidEnableDesugar>True</AndroidEnableDesugar>
  • Get a build error

It appears the cause here is using the 27.x support libraries along with desugar targeting API level 19.

I can confirm that building an app _without_ the support libraries works with API level 19. Such as our sample here. However, that isn't too helpful, since all apps need the support library!

The real fix here is to get D8 support, but we don't have a timeline for when it will be done, unfortunately.

However, I can say it is definitely a priority for us--since D8 also has the promise of faster build times.

I'll try to follow up when I have more info.

It crashes my application if I desugar is true!

@Raj123456788 if you are still getting a crash when enabling desugar, we have a fix in master, but you might try the workaround I mention here for now.

Same here, I get a NoClassDefFoundError when building with Desugar enabled.

Was this page helpful?
0 / 5 - 0 ratings