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

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.


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