5

Can someone show how to solve this issue associated with

ionic cordova build android

I have tried suggestions provided in this link but the issue remain as it is.

cordova.cmd build android Checking Java JDK and Android SDK versions ANDROID_SDK_ROOT=undefined (recommended setting) ANDROID_HOME=undefined (DEPRECATED) Failed to find 'JAVA_HOME' environment variable. Try setting it manually. [ERROR] An error occurred while running subprocess cordova.

    cordova.cmd build android exited with exit code 1.

    Re-running this command with the --verbose flag may provide more information.
Kairaoi T
  • 69
  • 1
  • 3
  • 14

3 Answers3

3

To use cordova your need Java, Android SDK, and Gradle. If your take a peek at the source files, mainly platforms/android/cordova/lib/check_reqs.js you can find the exact code that prints out the messages you get. Cordova checks environment variables you have set up on your system.

For Java, you need the JAVA_HOME variable, you can check variables by typing echo $JAVA_HOME on your terminal if you're using linux. If the variable does not get printed out, you have to set it manually. Install java 8 and in your terminal you can set it with export JAVA_HOME="path_to_java". My path is /usr/lib/jvm/java-8-openjdk-amd64/.

You need to do the same for ANDROID_HOME. When you install Android studio you will have the Android SDK installed. For me it's in my home directory /home/username/Android/Sdk.

For Gradle you don't need to set env variables. You can install it with sudo apt install gradle.

Note that once you close your terminal all the variables you added will disappear. You can make them permanent by adding the export commands to your ~/.profile file and rebooting your system. You can alternatively type source ~/.profile in your terminal to load the variables into that terminal instance.

You may get an error about licences for Android. Just type path_to_your_sdk/sdk/tools/bin/sdkmanager --licenses and accept all the licences.

If you have multiple Java versions installed Cordova may give you trouble since it doesn't only check the JAVA_HOME variable, even though it only need that location. If you're confident that your JAVA_HOME is set right, you can comment out lines 370-375 (the ones that throw an error when the following if statement is true !String(values[0]).startsWith('1.8.')) in the platforms/android/cordova/lib/check_reqs.js and Cordova will work fine.

nikvujic
  • 83
  • 1
  • 7
1

From the error log, it looks like either, you don't have Java installed or don't have it in your environment variable. Ensure that <path to java>/java/<jdk version>/bin is in your environment variable. Follow this answer for adding it. https://stackoverflow.com/a/32241360/10602679

Android SDK is required to build Android apps. The recommended way to download it is through Android Studio else you need to download sdk manually

Next, you need to add the Android sdk path in your Environment variable to access the command-line utility. Follow the screenshots provided in this answer for the same. https://stackoverflow.com/a/30900424/10602679

Pankaj Sati
  • 2,441
  • 1
  • 14
  • 21
0

For Mac

   unset JAVA_HOME


   export JAVA_HOME=$(/usr/libexec/java_home -v "xx.xx.xx")
Brijmohan Karadia
  • 437
  • 1
  • 5
  • 10