47

I am new to cordova and am trying to create an android HelloWold project.

When I use cordova platforms add android, it throws some exception:

D:\CordovaSpace\helloWorld>cordova platforms add android
Adding android project...
Creating Cordova project for the Android platform:
        Path: platforms\android
        Package: com.example.helloworld
        Name: HelloWorld
        Activity: MainActivity
        Android target: android-24
Subproject Path: CordovaLib
Android project created with cordova-android@6.0.0
Installing "cordova-plugin-whitelist" for android
ANDROID_HOME=D:\Java_Android_SDK\android_sdk
JAVA_HOME=C:\Program Files (x86)\Java\jdk1.8.0_73
Subproject Path: CordovaLib
Starting a new Gradle Daemon for this build (subsequent builds will be faster).

FAILURE: Build failed with an exception.

* What went wrong:
Unable to start the daemon process.
This problem might be caused by incorrect configuration of the daemon.
For example, an unrecognized jvm option is used.
Please refer to the user guide chapter on the daemon at https://docs.gradle.org/2.14.1/userguide/gradle_daemon.html
Please read the following process output to find out more:
-----------------------
Error occurred during initialization of VM
Could not reserve enough space for 2097152KB object heap


* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Failed to install 'cordova-plugin-whitelist':Error: cmd: Command failed with exit code 1
    at ChildProcess.whenDone (D:\CordovaSpace\helloWorld\platforms\android\cordova\node_modules\cordova-common\src\superspawn.js:169:23)
    at emitTwo (events.js:106:13)
    at ChildProcess.emit (events.js:191:7)
    at maybeClose (internal/child_process.js:850:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:215:5)
Error: cmd: Command failed with exit code 1
ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
Mr_Yang
  • 481
  • 1
  • 4
  • 3

9 Answers9

111

Try again after increasing your Java VM(?) memory size.

Here is how to fix it on a Windows platform:

Go to Start -> Control Panel -> System -> Advanced(tab) -> Environment Variables -> System Variables -> New:
Variable name: _JAVA_OPTIONS
Variable value: -Xmx512M

Don't ignore the score and underscore characters.

A1rPun
  • 16,287
  • 7
  • 57
  • 90
Kemal Yalcinkaya
  • 1,818
  • 1
  • 13
  • 25
13

I had the exact same problem. I switched to use 64-bit JDK as suggested in here and it worked fine. I was using Ionic 3.5.0 on Windows 10.

Hamed
  • 1,175
  • 3
  • 20
  • 46
8

this worked for my ionic app.

Change line on

// to allow dex in process
'org.gradle.jvmargs': '-Xmx1024m',

from

// to allow dex in process
'org.gradle.jvmargs': '-Xmx2048m',

in your

<app path>\platforms\android\cordova\lib\config

thanks

Reznik
  • 2,663
  • 1
  • 11
  • 31
fnu dudes
  • 81
  • 1
  • 1
  • Thanks, your answer solved my problem, I was stuck for 3 days. I applied many different things. but ignoring your answer as score is less. Thank you – Abhishek Joshi Mar 20 '20 at 04:41
6
args.push('-Dorg.gradle.jvmargs=-Xmx2048m') 
into 
args.push('-Dorg.gradle.jvmargs=-Xmx1024m');

on the following location files.

  1. project-folder\platforms\android\cordova\lib\builders\GradleBuilder.js

  2. project-folder\platforms\android\cordova\lib\builders\StudioBuilder.js

Kernel
  • 165
  • 4
  • 14
3

Go to : \platforms\android\cordova\lib\config\GradlePropertiesParser.js

constructor (platformDir) {
        this._defaults = {
            // 10 seconds -> 6 seconds
            'org.gradle.daemon': 'true',

            // to allow dex in process
            'org.gradle.jvmargs': '-Xmx2048m',

            // allow NDK to be used - required by Gradle 1.5 plugin
            'android.useDeprecatedNdk': 'true'

            // Shaves another 100ms, but produces a "try at own risk" warning. Not worth it (yet):
            // 'org.gradle.parallel': 'true'
        };

And change the 'org.gradle.jvmargs': '-Xmx2048m', to 'org.gradle.jvmargs': '-Xmx1024m',

2
args.push('-Dorg.gradle.jvmargs=-Xmx2048m') 
 to  
args.push('-Dorg.gradle.jvmargs=-Xmx1024m');

on the following location files.

  1. project-folder\platforms\android\cordova\lib\builders\builders.js
  2. project-folder\platforms\android\cordova\lib\builders\GradleBuilder.js
  3. project-folder\platforms\android\cordova\lib\builders\StudioBuilder.js
Abd Abughazaleh
  • 4,615
  • 3
  • 44
  • 53
2

Go to Start -> Control Panel -> System -> Advanced(tab) -> Environment Variables -> System Variables

Add/change JAVA_HOME value from C:\Program Files (x86)\Java\jdk1.8.0_192 to C:\Program Files\Java\jdk1.8.0_192

That is, change from Program Files (x86) path to Program Files path.

Manohar Reddy Poreddy
  • 25,399
  • 9
  • 157
  • 140
1

I had a same problem.

Anyway, here is how to fix it: Go to Start->Control Panel->System->Advanced(tab)->Environment Variables->System Variables->New: Variable name: _JAVA_OPTIONS

Variable value: -Xmx512M

if 512 not work then change it to.

Variable value: -Xmx1024M

And also change the path.

Variable name: Path

Variable value: ;C:\Program Files\Java\jre6\bin;F:\JDK\bin;

Change this to your appropriate path.

Community
  • 1
  • 1
Zaid Bin Khalid
  • 748
  • 8
  • 25
0

Setting the environment variable at system level as in the @kemal's solution will set the memory size for all the JVM applications in the system, which is not a what most would want to do.

Instead, set the heap size in your cordova gradlebuilder property file.

args.push('-Dorg.gradle.jvmargs=-Xmx2048m');
Anand
  • 9,672
  • 4
  • 55
  • 75