'Xamarin Android: java.exe exited with Code 2 -- MultiDex enabled

After the most recent update (Xamarin.Android.Support libraries, V28.0.0, Build tools 28.0.3), every build is failing with

"java.exe" exited with Code 2

I had previously enabled Multi-Dex to get rid of this issue, and that has worked without a problem until now. I also tried re-enabling ProGuard, and following all of the steps from this answer, including the change to MainDexClasses.bat.

There are a couple of new warnings, but I haven't been able to work out whether or not they are causing the new problem:

CREATEMULTIDEXMAINDEXCLASSLIST: warning : can't write resource [META-INF/maven/com.squareup.okio/okio/pom.xml] (Duplicate zip entry [okio.jar:META-INF/maven/com.squareup.okio/okio/pom.xml])

CREATEMULTIDEXMAINDEXCLASSLIST: warning : can't write resource [META-INF/maven/com.squareup.okio/okio/pom.properties] (Duplicate zip entry [okio.jar:META-INF/maven/com.squareup.okio/okio/pom.properties])

UPDATE: It looks like this is a bug that has crept in with the V28 SDK. There is a repro at https://github.com/SpiegelSoft/Repro, and I have created an issue at https://github.com/xamarin/xamarin-android/issues/2565

If anyone can find any neat workarounds, I will be very grateful.



Solution 1:[1]

I had this issue and Fix for me was:

  1. Close Project
  2. Delete bin and obj folders from PCL/Shared Project and all other platforms including Android
  3. Open project
  4. Rebuild

Solution 2:[2]

I have the same issue it seems:

(Duplicate zip entry [openpay.jar:META-INF/LICENSE])
10>CREATEMULTIDEXMAINDEXCLASSLIST : warning : can't write resource [META-INF/NOTICE] (Duplicate zip entry [openpay.jar:META-INF/NOTICE])
10>CREATEMULTIDEXMAINDEXCLASSLIST : warning : can't write resource [META-INF/services/com.fasterxml.jackson.core.JsonFactory] (Duplicate zip entry [openpay.jar:META-INF/services/com.fasterxml.jackson.core.JsonFactory])
10>CREATEMULTIDEXMAINDEXCLASSLIST : warning : can't write resource [META-INF/maven/com.fasterxml.jackson.core/jackson-core/pom.xml] (Duplicate zip entry [openpay.jar:META-INF/maven/com.fasterxml.jackson.core/jackson-core/pom.xml])
10>CREATEMULTIDEXMAINDEXCLASSLIST : warning : can't write resource [META-INF/maven/com.fasterxml.jackson.core/jackson-core/pom.properties] (Duplicate zip entry [openpay.jar:META-INF/maven/com.fasterxml.jackson.core/jackson-core/pom.properties])
10>CREATEMULTIDEXMAINDEXCLASSLIST : warning : can't write resource [com/fasterxml/jackson/core/json/VERSION.txt] (Duplicate zip entry [__reference__jackson-core-2.1.3.jar:com/fasterxml/jackson/core/json/VERSION.txt])
10>CREATEMULTIDEXMAINDEXCLASSLIST : warning : can't write resource [META-INF/services/com.fasterxml.jackson.core.JsonFactory] (Duplicate zip entry [__reference__jackson-core-2.1.3.jar:META-INF/services/com.fasterxml.jackson.core.JsonFactory])
10>CREATEMULTIDEXMAINDEXCLASSLIST : warning : can't write resource [META-INF/maven/com.fasterxml.jackson.core/jackson-core/pom.xml] (Duplicate zip entry [__reference__jackson-core-2.1.3.jar:META-INF/maven/com.fasterxml.jackson.core/jackson-core/pom.xml])
10>CREATEMULTIDEXMAINDEXCLASSLIST : warning : can't write resource [META-INF/maven/com.fasterxml.jackson.core/jackson-core/pom.properties] (Duplicate zip entry [__reference__jackson-core-2.1.3.jar:META-INF/maven/com.fasterxml.jackson.core/jackson-core/pom.properties])
10>CREATEMULTIDEXMAINDEXCLASSLIST : warning : can't write resource [META-INF/ASL2.0] (Duplicate zip entry [__reference__jackson-core-asl-1.9.11.jar:META-INF/ASL2.0])
10>CREATEMULTIDEXMAINDEXCLASSLIST : warning : can't write resource [META-INF/LICENSE] (Duplicate zip entry [__reference__jackson-core-asl-1.9.11.jar:META-INF/LICENSE])
10>CREATEMULTIDEXMAINDEXCLASSLIST : warning : can't write resource [META-INF/NOTICE] (Duplicate zip entry [__reference__jackson-core-asl-1.9.11.jar:META-INF/NOTICE])
10>CREATEMULTIDEXMAINDEXCLASSLIST : warning : can't write resource [org/codehaus/jackson/impl/VERSION.txt] (Duplicate zip entry [__reference__jackson-core-asl-1.9.11.jar:org/codehaus/jackson/impl/VERSION.txt])
10>CREATEMULTIDEXMAINDEXCLASSLIST : warning : can't write resource [META-INF/maven/com.squareup.okio/okio/pom.xml] (Duplicate zip entry [okio.jar:META-INF/maven/com.squareup.okio/okio/pom.xml])
10>CREATEMULTIDEXMAINDEXCLASSLIST : warning : can't write resource [META-INF/maven/com.squareup.okio/okio/pom.properties] (Duplicate zip entry [okio.jar:META-INF/maven/com.squareup.okio/okio/pom.properties])
10>C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(2610,3): error MSB6006: "java.exe" exited with code 2.

For me it started happening after I migrated from packages.config to PackageReference. It all worked with packages.config previously. I have tried removing bin/obj, clean/rebuild, and multidex is on. My target framework is Pie.

After fighting this for 3 days, what "worked" for me was downgrading all of my Xamarin.Android.Support nugets from 28.0.0.1 to 27.0.2.1 and removing all Xamarin.Android.Support nugets that were added starting with v28.0.0 (i.e. that are only available as v28 and 28.0.0.1, e.g. Xamarin.Android.Support.CoordinaterLayout). This allows building and running the app.

Have you been able to find a work around, perhaps a cleaner one?

Solution 3:[3]

I had this error when switching from VS Mac to VS Windows.

Here the topic Switching From VS Mac To VS Windows got “java.exe” exited with code 2

The issue was comming from the Android.csproj and the Keystore Path.
It was set to my Mac Path even if I had already changed it on
VSWindows AndroidProject => Properties => Signin

So I removed this lines on Android.csproj (edited with third text editor):

<AndroidKeyStore>True</AndroidKeyStore>
<AndroidSigningKeyStore>YourMacPath/Alias.keystore</AndroidSigningKeyStore>
<AndroidSigningStorePass>Password</AndroidSigningStorePass>
<AndroidSigningKeyAlias>Alias</AndroidSigningKeyAlias>
<AndroidSigningKeyPass>AliasPassword</AndroidSigningKeyPass>

On debug & release Part.

Then you can edit them on VS Windows.

Hope I helped someone. See ya

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 fewlinesofcode
Solution 2 Nikita K
Solution 3 G Clovs