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

29,785

Solution 1

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.

Solution 2

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

Solution 3

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

Solution 4

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

Solution 5

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.

Share:
29,785
Daniel_97
Author by

Daniel_97

Updated on February 02, 2021

Comments

  • Daniel_97
    Daniel_97 about 3 years

    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.

  • Eli
    Eli over 6 years
    Point 3. See MultiDexApplication
  • Eli
    Eli over 6 years
    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.
  • Daniel_97
    Daniel_97 over 6 years
    Maybe on the windows machine you only had installed android API level(s) >21 which would determinate the statement in my question?
  • Daniel_97
    Daniel_97 over 6 years
    @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...
  • Micer
    Micer over 6 years
    @Daniel_97 Yeah that was most likely the reason.
  • Shirkam
    Shirkam over 6 years
    Are you using same version of Xamarin and VS as the OP?
  • Vermoric Huynh
    Vermoric Huynh over 6 years
    I using VS version 7.2 (build 636)
  • james
    james over 6 years
    Thanks, that saved my time!
  • michaelitoh
    michaelitoh over 6 years
    @Elisa in which tag is the AndroidEnableMultipleDex tag? I don't have any of this tags.
  • Eli
    Eli over 6 years
    @MichaelAlexanderMontero This tag <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
  • Dave Rincon
    Dave Rincon over 5 years
    i add multidex but dont works i disabled sing and works, many thanks!
  • Davos
    Davos over 5 years
    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?
  • Le-roy Staines
    Le-roy Staines about 5 years
    Assuming you want to distribute an unsigned APK, which you cannot do with Google Play...
  • Hein Andre Grønnestad
    Hein Andre Grønnestad about 5 years
    @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.
  • DisturbedNeo
    DisturbedNeo almost 5 years
    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.
  • Alen.Toma
    Alen.Toma almost 5 years
    Man you are a life saver thank you. Your solution is the only one that worked :)
  • wislon
    wislon over 4 years
    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.
  • Eric Mutta
    Eric Mutta over 3 years
    In my case I had renamed the folder containing the keystore so changing the path fixed the issue.
  • Christopher Wray
    Christopher Wray about 2 years
    This was the issue for me as well. Thank you!