38

so I have my Xamarin.Android project which contains several libraries. When I try to build I receive an error which says "java.exe" exited with code 2.

After some hours of google research I found out that I could enable Multidex. According to the blog entry of Jon Doublas I did the steps mentioned. http://www.jon-douglas.com/2016/09/05/xamarin-android-multidex/

According to the blog entry Android API Level > 21 handles Multidex itself. Well my experience is that it does not. I am building on API Level 25 and get the same error again and again with the same information. The steps in the article do not help. It seems like Xamarin.Android does not support Multidex.

It seems like you have to handle Multidex by yourself but I haven't found any solution which is working. Dos anybody found a solution which works?

I am using Xamarin 4.6 and Visual studio 2017.

4
  • Please tell us which libs did you installed into your project?
    – Grace Feng
    May 17, 2017 at 2:10
  • I installed pretty much e.g. firebase, pubnub, playservices(packed) and some scanner libs for special devices.
    – Daniel_97
    May 17, 2017 at 9:30
  • I faced the same error after install a nugget package Aug 10, 2017 at 7:13
  • 1
    @AjaySharma yea i believe it can happen anytime if you hit the mehtod limit of ~65k :) Try the answer it might help you too
    – Daniel_97
    Aug 10, 2017 at 9:06

19 Answers 19

43

Found the answer after about 20 hours of searching in my project.. So here are the steps to resolve the problem "java.exe" exited with code 2

1) Change the MainDexClasses.bat as described in the link:

http://www.jon-douglas.com/2016/09/05/xamarin-android-multidex/

2) Make sure you have enabled Multidex Option in your Android Application! Have a look at the .csprj file if Multidex is set to true (in my case it wasn't even if I enabled Multidex)

3) Add the following part in your AndroidManifest file

<application android:name="android.support.multidex.MultiDexApplication"></application>

This will tell your Android project to actually use the Multidex-Option given.

After a clean and build I was able to run my application as it is.

5
  • Point 3. See MultiDexApplication
    – Eli
    Sep 29, 2017 at 10:30
  • 2
    Point 2. In my case the .csproj file had only this line <AndroidEnableMultipleDex>true</AndroidEnableMultipleDex> and I had to manually add <AndroidEnableMultiDex>true</AndroidEnableMultiDex> to make it work! Note the MultiDex instead of MultipleDex. Thanks go to this discussion.
    – Eli
    Sep 30, 2017 at 10:00
  • @Elisa a collegue of mine had to face the same problem like you I have literally no idea why such things are happening but as long as we can solve them it might be fine I guess...
    – Daniel_97
    Oct 6, 2017 at 7:24
  • @Elisa in which tag is the AndroidEnableMultipleDex tag? I don't have any of this tags. Jan 14, 2018 at 17:05
  • @MichaelAlexanderMontero This tag <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    – Eli
    Jan 15, 2018 at 14:52
31

After finding this question and @Daniel_97s answer I was able to solve this error only by adding multidex to the project without changing MainDexClasses.bat:

  1. Check the option in Project Options:

Check the option in Project Options:

  1. Add MultiDexApplication to Android Manifest:

    <application android:name="android.support.multidex.MultiDexApplication"></application>
    

Note I was getting this error code 2 on Visual Studio for Mac and on Windows the same app was building successfully. (why?)

2
  • 2
    Maybe on the windows machine you only had installed android API level(s) >21 which would determinate the statement in my question?
    – Daniel_97
    Oct 6, 2017 at 7:22
  • @Daniel_97 Yeah that was most likely the reason.
    – Micer
    Oct 11, 2017 at 7:02
30

The solution in my case was to disable the "Sign the .APK file using the following keystore details". Because the keystore location and/or password was incorrect, java.exe exited with code 2.

enter image description here

5
  • 3
    i add multidex but dont works i disabled sing and works, many thanks! Dec 10, 2018 at 19:40
  • 3
    Assuming you want to distribute an unsigned APK, which you cannot do with Google Play... Mar 7, 2019 at 8:11
  • 2
    @Le-royStaines, you obviously can't do this for the release build. As stated in my answer, my keystore location was incorrect (actually on an unavailable network path). This is just another reason for the general java error message, which isn't covered in the other answers. Mar 8, 2019 at 9:41
  • 3
    Changing the path to the correct location fixed it for me. Seems like the build failing can be attributed to not being able to find the keystore at the specified location, even if you're just running a Debug build and don't even need to sign the APK. Apr 25, 2019 at 10:10
  • 1
    In my case I had renamed the folder containing the keystore so changing the path fixed the issue.
    – Eric Mutta
    Sep 27, 2020 at 6:33
12

I only go to option > Build > Android Build and check Enable Multi-Dex it will resolve

4
  • Are you using same version of Xamarin and VS as the OP?
    – Shirkam
    Oct 11, 2017 at 10:49
  • I using VS version 7.2 (build 636) Oct 18, 2017 at 3:35
  • Thanks, that saved my time!
    – james
    Jan 4, 2018 at 6:36
  • I actually had to add the <AndroidEnableMultiDex>true</AndroidEnableMultiDex> line, it looks like mine got wiped out somehow. After I put it back, reloaded the project and rebuilt, it worked perfectly.
    – wislon
    Sep 23, 2019 at 22:40
9

My .csproj file had an entry for the KeyStore that was no longer valid, even thought right-clicking the project and inspecting the properties indicated that I had pointed it to the new location.

Within .csproj look for all instances of <AndroidSigningKeyStore> and update accordingly.

1
  • 1
    Man you are a life saver thank you. Your solution is the only one that worked :)
    – Alen.Toma
    May 19, 2019 at 12:16
6

I've struggled with this issue for quite a long time. I found source of the error in signing - as I cloned project repo from company TFS, Android csproj file had entries for KeyStore settings and location. I didn't have the keystore and the location on my drive was invalid. Visual Studio is then unable to sign the app while deploying and instead of some sort of FileNotFoundException it gave me just java exited with code 2. So...

tl;dr: Go to android project properties, select package signing a uncheck "Sign the apk file..." (which changes csproj <AndroidKeyStore>true</AndroidKeyStore> to false) and voila, you can now run your solution.

enter image description here

2

Reassign the path of the keystore worked for me

==EDIT 01==

Double click on Android Project > Android Package Signing

On the Keystore path, don't know why but apparently the path was wrong, I re-select the .keystore file again from the same path, and the error "exited with code 2" went away and I was able to Archive for release

1
  • I am not sure what you mean by that, could you elaborate your answer with some steps of how you did that and why it was the solution?
    – Davos
    Jan 10, 2019 at 13:29
2

Disable sign the .APK file using the following keystore details as @Klemikaze

0
2

Note: Multi-dex is enabled.

After 5 hours of looking or an answer, this is the conclusion I've found -

  1. Enable diagnostic MSBuild output within Visual Studio, so you can see more details about your error:
  • Click Tools > Options...
  • In the left-hand tree view, select Projects and Solutions > Build and Run
  • In the right-hand panel, set the MSBuild build output verbosity dropdown to Diagnostic, Click OK
  • Clean and rebuild your package. Diagnostic output is visible within the Output panel.
  1. If your error shows "java.io.IOException: Invalid keystore format", you are probably using an outdated Java sdk file, so do next:

Open Visual Studio and update the Java Development Kit Location to point to the new JDK under Tools > Options > Xamarin > Android Settings > Java Development Kit Location:

enter image description here

Be sure to restart Visual Studio.

2
  • Thank you. This is the only thing that worked as of year 2023 - Visual Studio 2019 version 16.11.115 Mar 14 at 15:14
  • @jaysonragasa You're welcome, lol. A year later someone found this useful, nice.
    – aca
    Mar 15 at 8:19
1

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 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

1

In my case this was link to the "Sign the .APK file using the following keystore details" option as Hein Andre Grønnestad mentioned. The location was not correct.

I wanted to put a path that could work for everyone at work so I used $HOME/Library/... instead of /Users/Me/Library/...

The problem is $HOME doesn't mean anything special for java so the keystore file could not be found.

Solution: use relative path or absolute path without $HOME variable or ~

1

For me, it was a problem with my Keystore. Follow this link to enable diagnostics. This will give you a more detailed error message in the output window. If it says that your keystore failed to verify, you will have to fix your keystore. This solved the problem for me.

1
  • This was the issue for me as well. Thank you!
    – Chris Wray
    Mar 1, 2022 at 3:05
1

For me it turned out I had Eclipse Temurin JDK installed and that was being used. In VS Tools>Options>Xamarin I changed the path to Java Development Kit Location to from the Eclipse path to C:\Program Files\Microsoft\jdk-11.0.12.7-hotspot. Maybe more importantly, I discovered Eclipse Temurin JDK was being used by running: msbuild /bl /t:"Restore;SignAndroidPackage" in my solution dir and then opening the resulting msbuild.binlog and clicking on the few errors and a path with Eclipse stood out. Your problem may be different but finding it this way may be the key to solving it. EDIT: My settings don't stick after closing and reopening VS 2019. At this point I started using VS 2022 and don't have the issue.

1

In my case solved the problem by signing the .APK whit the values signed into the keystore that we configured to create the APK file in Release mode.

I taked the Microsoft example in here

enter image description here

and here I put the same values

enter image description here

That way a solved my problem with

"java.exe" exited with code 2

But the real information problem was in output:

Failed to load signer "signer #1" java.io.IOException: Invalid keystore format

At the moment I don't find the real solution to this problem but, this works.

0

Please Update your Visual Studio to Latest version, if you are still using an old version.

If you think there is no solution then go and enable the Multidex

0

In my case the one of keystore details was wrong, after saving-> cleaning -> building process it worked.

0

Try reset the keystore <AndroidSigningKeyStore> or change to false the key

>

<AndroidKeyStore>true</AndroidKeyStore>

Only to check. But remembering that it needs to be set to true.

After Clean and Rebuild the solution.

0

Right click on the project and click on Properties Android Options > Check the Enable Multi-Dex and save the

image 1

Now click on Tools > Options >Xamarin > Android Settings Change the Java development Kit location

C:\Program Files\Java\jdk-17

image 2

-1

My experience is...

I updated the following elements:

  • Xamarin.Android.Support.Design
  • Xamarin.Android.Support.v4
  • Xamarin.Android.Support.v7.AppCompat
  • Xamarin.Android.Support.v7.CardView
  • Xamarin.Android.Support.v7.MediaRouter

from 26.1.0.1 to 28.0.0.3

Then, after it started the issue with:

"java.exe" exited with code 2 Xamarin.Android project.

I just ticked the Enable Multi-Dex inside the Android project property. I am using VS 2017 in windows system.

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.