"java.exe" exited with code 2 Xamarin.Android project
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:
- Check the option in Project Options:
-
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.
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.
Daniel_97
Updated on February 02, 2021Comments
-
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 over 6 yearsPoint 3. See MultiDexApplication
-
Eli over 6 yearsPoint 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 over 6 yearsMaybe on the windows machine you only had installed android API level(s) >21 which would determinate the statement in my question?
-
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 over 6 years@Daniel_97 Yeah that was most likely the reason.
-
Shirkam over 6 yearsAre you using same version of Xamarin and VS as the OP?
-
Vermoric Huynh over 6 yearsI using VS version 7.2 (build 636)
-
james over 6 yearsThanks, that saved my time!
-
michaelitoh over 6 years@Elisa in which tag is the AndroidEnableMultipleDex tag? I don't have any of this tags.
-
Eli over 6 years@MichaelAlexanderMontero This tag
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
-
Dave Rincon over 5 yearsi add multidex but dont works i disabled sing and works, many thanks!
-
Davos over 5 yearsI 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 about 5 yearsAssuming you want to distribute an unsigned APK, which you cannot do with Google Play...
-
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 almost 5 yearsChanging 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 almost 5 yearsMan you are a life saver thank you. Your solution is the only one that worked :)
-
wislon over 4 yearsI 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 over 3 yearsIn my case I had renamed the folder containing the keystore so changing the path fixed the issue.
-
Christopher Wray about 2 yearsThis was the issue for me as well. Thank you!