Project won't build. Keeps failing with error message "java.exe" has exited with code 2

I've run into a problem whilst developing an android app in visual studio. For some reason, and I have been fighting with this for hours and found no cause for this to happen in the code, the project fails to build every time, citing the error:

"java.exe" exited with code 2.

The output log also says:

Done executing task "CompileToDalvik" -- FAILED.

I can tell you I have set the Java Heap size to 1G, uninstalled and reinstalled Xamarin, tried several clean and rebuilds, and nothing seems to work. Deleting the debug folder from the bin and obj files and restarting Visual Studio/the computer also made no difference.

Anyone encountered this before? If so how did you solve it?

Best Answer

Answers

  • PeanutButterCerealPeanutButterCereal USMember ✭✭

    Never mind, got it.

    I was over the 65k reference limit ( https://medium.com/@rotxed/dex-skys-the-limit-no-65k-methods-is-28e6cb40cf71 )

    Just deleted Google Play Services (takes up 20k of the 65k limit) and boom. Hope this knowledge helps anyone else :-)

  • TomStandaert.0575TomStandaert.0575 BEMember ✭✭

    @LucasTeixeira how did you even delete the ones you don't need, they are all dependent on eachother

  • TomStandaert.0575TomStandaert.0575 BEMember ✭✭

    @PeanutButterCereal yes, but the problem is that xamarin.forms.maps is still dependent on the xamarin.googleplayservices package which in its turn is dependent on all the modules.

    So atm it's impossible to add the xamarin.forms.maps package to a project unless you first add the xamarin.googleplayservices version 22 package. They shouldn't have updated the xamarin.googleplayservices package imho, it should have been left at version 22.

  • ChrisMcBrideChrisMcBride USMember ✭✭

    I tried to update to 1.4.3 but had to back it out for this exact problem. Is it possible to include maps with 1.4.3, or will we need to wait for the next version?

  • KeshavGupta.4083KeshavGupta.4083 USMember, University ✭✭

    Can we please have a solution for this? I have the same problem after adding Forms.Maps in my Droid project.

  • PeanutButterCerealPeanutButterCereal USMember ✭✭

    @KeshavGupta.4083 I think for now the best solution would be to have your Google Play Services kept in a separate project that you can reference in your actual app project, I think that should work but I agree it's not ideal.

  • KeshavGupta.4083KeshavGupta.4083 USMember, University ✭✭

    @PeanutButterCereal I just managed to solved this issue by increasing the Java Heap Size to 1G

  • PeanutButterCerealPeanutButterCereal USMember ✭✭

    @KeshavGupta.4083 I tried that solution too, still didn't help me; removed Google Play Services and suddenly the problem was gone (shaved the APK size by about half too)

  • @LucasTeixeira I managed with your method but when Google Play Services updated it brought all the references back in. In visual studio I went into properties, Android Options, and selected the check box 'Enable multi-dex' and it works now, might be prudent to start using this multi-dex option to avoid the potential for Google updates to reintroduce this Dalvik 65k issue.

  • MauroGaggero.1198MauroGaggero.1198 USMember ✭✭

    I'm check 'Enable multi.dex' option bat the project fails to build with this error:

    C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(3,3): Error MSB6004: The specified task executable location "C:\Users\f.didonna\AppData\Local\Android\android-sdk\build-tools\19.1.0\mainDexClasses.bat" is invalid. (MSB6004) (MobileCRM.Android)

  • ParthSavadiyaParthSavadiya INMember ✭✭

    how to delete any one google play services out of two

  • Chris_LamontChris_Lamont USUniversity ✭✭

    Enabling Multi-Dex fixed the error, but it now does not deploy to devices lower than 5.0

    https://developer.android.com/studio/build/multidex.html

    I then found out you have to add a support gradle to your project.

    https://developer.android.com/topic/libraries/support-library/features.html#v7

    To add a gradle to a Xamarin android project in Visual Studio you will need the extension.

    https://visualstudiogallery.msdn.microsoft.com/3a3257c7-473a-4790-9610-9a561eed0b8c

    However, I am now getting the error "Duplicate zip entry [classes.jar:android/support/multidex/MultiDex.class]".
    If anyone knows how to fix that, please let me know.

  • ankur_abankur_ab USMember

    Hi,
    Tried all solutions.
    When I am adding GooglePlayService packages.
    It give me build Time error.
    C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(3,3): Error MSB6006: "java.exe" exited with code 2. (MSB6006).
    Any help will appreciated.

    Thank you. :)

  • batmacibatmaci DEMember ✭✭✭✭

    enable multi dex on android options will fix it also

  • edited January 2017

    I get this error as solved! -- > "java.exe" exited with code 2

  • SeacenSeacen USMember
    edited February 2017

    In my case rebuilt the project solved the problem

  • 15mgm1515mgm15 USMember ✭✭✭

    With latest Xamarin Studio Updates (today 03/15/2017) I am getting the same issue:

    "/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Common.targets: Error: Tool exited with code: 2. Output:
    trouble writing output: Too many field references: 65720; max is 65536."

    Enable Multi-Dex does not resolve the problem.

  • EmanBuhagiarEmanBuhagiar MTMember

    @SteveShaw.5557 solution solved it for me. Thanks!

  • mubimubi USMember ✭✭✭

    ***** SOLVED!!!!

    delete Bin and Obj folder from all project,
    restart IDE
    clean and rebuild project

    here u go :smile:

  • AfzalAfzal USUniversity

    Hi, I was also facing the same issue..

    Actually its because of 65K method limit, and yes mainly because of google play service (it has lots of libraries)

    Solution:
    Enable MultiDex option for android,
    Project Options -> Build -> Android Build -> Check "Enable Multi-Dex"

    Behind the Scene:
    What it does is, it divides your dex in multiple segment of 65K chunk. so now you are dealing with more than one dex, sometimes.. I repeat sometime it may behave unexpected because of reference mismatch,

    When you unable MultiDex, Test your application throughly, and your main class should be in first Dex(that Android does automatically, but when it fails, you get exception.. )

  • Antonio24991Antonio24991 USMember ✭✭

    @15mgm15 said:
    With latest Xamarin Studio Updates (today 03/15/2017) I am getting the same issue:

    "/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Common.targets: Error: Tool exited with code: 2. Output:
    trouble writing output: Too many field references: 65720; max is 65536."

    Enable Multi-Dex does not resolve the problem.

    You should enable it also in release mode!!

  • @KeshavGupta.4083 said:
    @PeanutButterCereal I just managed to solved this issue by increasing the Java Heap Size to 1G

    How did you do that?

  • MageHDMageHD Member

    This issue resurfaced but the fix was removing JDK 9. It doesn't appear as though it's supported.

    To uninstall the JDK, you must have Administrator privileges and execute the remove command either as root or by using the sudo(8) tool.

    Navigate to /Library/Java/JavaVirtualMachines and remove the directory whose name matches the following format:Foot1

    /Library/Java/JavaVirtualMachines/jdkmajor.minor.macro[_update].jdk
    For example, to uninstall 8u6:

    % rm -rf jdk1.8.0_06.jdk
    Do not attempt to uninstall Java by removing the Java tools from /usr/bin. This directory is part of the system software and any changes will be reset by Apple the next time you perform an update of the OS.

Sign In or Register to comment.