New Cordova project fails to build in Visual Studio 2015: Could not create the Java Virtual Machine
I've been playing around with Visual Studio's cross-platform capabilities over the last couple days and decided to try a 'raw' Cordova project to see if it fits my needs better than the other options I've explored (Xamarin, ionic). Visual studio abstracts most of the complexity away which is nice most of the time, but what if you run into a problem?
In my case I ran into a few cryptic error messages when I tried to build and run my Cordova app on a real Android device from within Visual Studio:
MSB3073 The command "platforms\android\cordova\clean.bat" exited with code 1.
Could not create the Java Virtual Machine.
A fatal exception has occurred. Program will exit.
(As it turns out, these messages aren't super helpful. The resolution lies in further troubleshooting)
This is the troubleshooting information I had to go on:
The Output tab had some additional information:
1> ANDROID_HOME=C:\Program Files (x86)\Android\android-sdk
1> JAVA_HOME=C:\Program Files (x86)\Java\jdk1.7.0_71
1> Running: c:\Users\rion\Documents\Visual Studio 2015\Projects\proj\proj\platforms\android\gradlew clean -b c:\Users\rion\Documents\Visual Studio 2015\Projects\proj\proj\platforms\android\build.gradle -Dorg.gradle.daemon=true
1>
1> FAILURE: Build failed with an exception.
1>
1> * What went wrong:
1> Unable to start the daemon process.
1> This problem might be caused by incorrect configuration of the daemon.
1> For example, an unrecognized jvm option is used.
1> Please refer to the user guide chapter on the daemon at http://gradle.org/docs/2.2.1/userguide/gradle_daemon.html
1> Please read the following process output to find out more:
1> -----------------------
1> Error occurred during initialization of VM
1> Could not reserve enough space for object heap
1>EXEC : error : Could not create the Java Virtual Machine.
1>EXEC : error : A fatal exception has occurred. Program will exit.
1>
1>
1> * Try:
1> Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Troubleshooting
I pulled up a VS command prompt (Start -> Visual Studio 2015 -> Visual Studio Tools -> Developer Command Prompt) and tried to run the command specified and found this:
The key text being:
* What went wrong:
A problem occurred evaluating root project 'android'.
> No installed build tools found. Please install the Android build tools version 19.1.0 or higher.
(This didn't make a lot of sense given that the ANDROID_HOME variable is set in the script above)
Solution
The solution for me was to add an ANDROID_HOME environment variable that points to the same android tools directory the script was trying to set. I also had to override the Cordova tools for Visual studio to point to an x64 version of my Java 8 JDK
ANDROID_HOME=C:\Program Files (x86)\Android\android-sdk
After I added that I could build from the Developer CLI.
After editing the Cordova tools options, I could build from withing Visual Studio (Stack Overflow link for this solution):