66
votes

I'm trying to build a Cordova Android project using the most recent tools. I followed the instructions here:

$ cordova create myApp com.myCompany.myApp myApp
$ cd myApp
$ cordova platform add [email protected] --save
$ cordova requirements android --verbose

But the result is:

Running command: android list targets
Command finished with error code 0: android list,targets

Requirements check results for android:
Java JDK: installed 1.8.0
Android SDK: installed true
Android target: installed android-25,android-23
Gradle: not installed 
Could not find an installed version of Gradle either in Android Studio,
or on your system to install the gradle wrapper. Please include gradle 
in your path, or install Android Studio
Error: CordovaError: Some of requirements check failed
    at /usr/local/nodejs_next/lib/node_modules/cordova/src/cli.js:401:45
    at _fulfilled (/usr/local/nodejs_next/lib/node_modules/cordova/node_modules/q/q.js:787:54)
    at self.promiseDispatch.done (/usr/local/nodejs_next/lib/node_modules/cordova/node_modules/q/q.js:816:30)
    at Promise.promise.promiseDispatch (/usr/local/nodejs_next/lib/node_modules/cordova/node_modules/q/q.js:749:13)
    at /usr/local/nodejs_next/lib/node_modules/cordova/node_modules/q/q.js:557:44
    at flush (/usr/local/nodejs_next/lib/node_modules/cordova/node_modules/q/q.js:108:17)
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickCallback (internal/process/next_tick.js:104:9)

It doesn't seem to be able to find Gradle (not sure where that's supposed to be).

I've seen reports of issues with Cordova and Android SDK Tools 25.3, but this seems to be different, and I have 25.2.3 (see below).

I have the following (this is on CentOS 6.8):

$ cordova --version
6.5.0

$ node --version
v6.10.2

$ npm --version
3.10.10

$ java -version
openjdk version "1.8.0_121"

$ echo $JAVA_HOME
/usr/lib/jvm/java-1.8.0

$ echo $ANDROID_HOME
/usr/local/android-sdk

# I installed the Android SDK from here
# https://developer.android.com/studio/index.html#downloads
# under "Get just the command line tools"

$ /usr/local/android-sdk/tools/bin/sdkmanager --list
Installed packages:
  Path                        | Version | Description                    | Location                    
  -------                     | ------- | -------                        | -------                     
  build-tools;23.0.3          | 23.0.3  | Android SDK Build-Tools 23.0.3 | build-tools/23.0.3/         
  build-tools;25.0.2          | 25.0.2  | Android SDK Build-Tools 25.0.2 | build-tools/25.0.2/         
  extras;android;m2repository | 47.0.0  | Android Support Repository     | extras/android/m2repository/
  extras;google;m2repository  | 46      | Google Repository              | extras/google/m2repository/ 
  patcher;v4                  | 1       | SDK Patch Applier v4           | patcher/v4/                 
  platform-tools              | 25.0.4  | Android SDK Platform-Tools     | platform-tools/             
  platforms;android-23        | 3       | Android SDK Platform 23        | platforms/android-23/       
  platforms;android-25        | 3       | Android SDK Platform 25        | platforms/android-25/       
  tools                       | 25.2.3  | Android SDK Tools 25.2.3       | tools/                      

UPDATE:

It doesn't seem to be mentioned anywhere in Cordova's or Android's docs, but if you're using the Android command-line tools without Android Studio, you have to manually install Gradle. Once you do, and it's on your PATH, this error will go away.

An additional thing that was confusing me: in older versions of Android SDK tools, there was a Gradle wrapper script in tools/templates/gradle/wrapper/gradlew. This can also be used to install Gradle, but I found it easier to just install it manually. This seems to have been removed as of the latest version.

19

19 Answers

71
votes

Solution for linux with apt-get (eg.: Ubuntu, Debian)

I have quite similar problem. I obtained error:

Error: Could not find an installed version of Gradle either in Android Studio,
or on your system to install the gradle wrapper. Please include gradle 
in your path, or install Android Studi

but without Exception. I solved it on Ubuntu by

sudo apt-get install gradle

I found also commands that allows install newest version of gradle in Ubuntu. It works only when first command is executed before (probably some dependecies are incorrect).

sudo add-apt-repository ppa:cwchien/gradle
sudo apt-get update
sudo apt-get install gradle-ppa

https://wtanaka.com/node/8079

If it does not work, try:

export PATH=$PATH:/opt/gradle/gradle-3.5/bin

More info:

https://askubuntu.com/questions/915980/package-is-installed-and-is-not-detected-gradle/915993#915993

For CentOS

Instruction of instalation gradle for CentOS is under this link

https://gist.github.com/parzonka/9371885

Update

Now I installing gradle by sdkman it is something like nvm for node.

Install sdkman

curl -s "https://get.sdkman.io" | bash 

Install gradle

sdk install gradle 4.0.2
43
votes

If you have android studio installed then you might want to try:

export PATH="$PATH:/home/<username>/android-studio/gradle/<gradle-4.0>/bin" 

This solved my problem.

37
votes

For Windows:

-Download last version of Gradle (https://gradle.org/releases)

-Create a folder and unzip files (I use C:\Program Files (x86)\gradle)

-Copy the path with the bin directory included (C:\Program Files (x86)\gradle\bin)

-Set the path C:\Program Files (x86)\gradle\bin (in my exemple) to "Path Environment Variables"

Variable name "Path" and variable value "C:\Program Files (x86)\gradle\bin" for both: User Variable table and System Variables table

You may need to reopen the "Prompt commad line"

To test, type gradle in prompt.

12
votes

I m using Cordova version 7.0.1 and Cordova android version is 6.2.3. I was facing the issue while performing android build. I m using only Cordova CLI and not using android studio at all.

The quick workaround for this issue before its official fixed in Cordova is as follows:

  1. Look for check_reqs.js file under platforms\android\cordova\lib folder
  2. Edit the else part of androidStudioPath variable null check in get_gradle_wrapper function as below:

Existing code:

else { //OK, let's try to check for Gradle! return forgivingWhichSync('gradle'); }

Modified code:

else { //OK, let's try to check for Gradle! var sdkDir = process.env['ANDROID_HOME']; return path.join(sdkDir, 'tools', 'templates', 'gradle', 'wrapper', 'gradlew'); }

NOTE: This change needs to be done everytime when the android platform is removed and re-added

UPDATE: In my case, I already had gradle wrapper inside my android SDK and I dint find necessity to install gradle explicitly. Hence, I made this workaround to minimize my impact and effort

7
votes

macOS

Gradle can be added on the Mac by adding the line below to ~/.bash_profile. If the file doesn't exist, please use touch ~/.bash_profile. This hidden file can be made visible in Finder by using Command + Shift + .

export PATH=${PATH}:/Applications/Android\ Studio.app/Contents/gradle/gradle-4.6/bin/

Use source ~/.bash_profile to load the new path directly into your current terminal session.

5
votes

I followed this Qiita tutorial to solve my trouble.

Environment: Cordova 8.1.1, Android Studio 3.2, cordova-android 7.0.0

  1. Set gradle PATH into .profile file.
export PATH="/Applications/Android Studio.app/Contents/gradle/gradle-4.6/bin":$PATH
  1. Export the setting.
source ~/.profle
  1. Now build cordova project.
cordova build android

PS: If [email protected] causes build error, downgrade your platform version to 6.3.0.

4
votes

Extending https://stackoverflow.com/users/5540715/surendra-shrestha answer for linux (~mint) users:
1. Install Android Studio (many tools have been deprecated for command line, so this is likely required). Asuming you follow the author instrucctions, your Android Studio will be installed in /usr/local/android-studio/. At the time of writing, the gradle version in Android Studio is 3.2, look at yours with ls /usr/local/android-studio/gradle/.
2. Add your gradle command, this can be done extending the PATH, as @surendra-shrestha suggested (should be written in ~/.bashrc to preserve the PATH change), adding an alias echo 'export alias gradle=/usr/local/android-studio/gradle/gradle-3.2/bin/gradle' >> ~/.bashrc && source ~/.bashrc. Or making a symbolic link: sudo ln -sn /usr/local/android-studio/gradle/gradle-3.2/bin/gradle /usr/bin/gradle (this was my choice).
3. Run cordova requirements to check everyhing is OK, should output something like:

Requirements check results for android:
Java JDK: installed 1.8.0
Android SDK: installed true
Android target: installed android-26,android-25,android-24,android-23,android-22,android-21,android-19,Google Inc.:Google APIs:19
Gradle: installed /usr/local/android-studio/gradle/gradle-3.2/bin/gradle
3
votes

For me the problem was that my android version was still on 6.1.2 which is not compatible with the newest Android Studio 2.3.1.

So what I did was run

cordova platform rm android cordova platform add [email protected]

2
votes

To answer OP's question:

Since you're on Linux you'll have to install gradle yourself, perhaps following this guide, and then put an entry in PATH to a folder that contains gradle executable.

Cordova has some code to look for gradle if you have Android Studio but only for Mac and Windows, see here:

https://github.com/apache/cordova-android/blob/e13e15d3e9aa4b9a61c6ece434e7c023fa5c3553/bin/templates/cordova/lib/check_reqs.js#L101-L132


Semi-related to OP's question, as I'm on Windows.

After upgrading to Android Studio 2.3.1, [email protected], [email protected] ([email protected]), I had build issues due missing target 25 and gradle.

First issue was solved with comment from X.Zhang (i.e. change android to avdmanager), BTW it seems a commit to fix that has landed on github so cordova-android should fix that in 6.3.0 (but I didn't test).

Second issue was as follows:

The problem turned out to be that process.env['ProgramFiles'] evaluates to 'C:\\Program Files (x86)'; whereas I have Android Studio in C:\\Program Files.

So the quick hack is to either override this value, or install Android Studio to the other place.

// platforms/android/cordova/lib/check_reqs.js

module.exports.get_gradle_wrapper = function() {
    var androidStudioPath;
    var i = 0;
    var foundStudio = false;
    var program_dir;
    if (module.exports.isDarwin()) {
        // ...
    } else if (module.exports.isWindows()) {
        // console.log(process.env['ProgramFiles'])';
        // add one of the lines below to have a quick fix...
        // process.env['ProgramFiles'] = 'C:\\Program Files (x86)';
        // process.env['ProgramFiles'] = 'C:\\Program Files';
        var androidPath = path.join(process.env['ProgramFiles'], 'Android') + '/';

I'm not sure what would be the proper fix to handle both folders in a robust way (other than iterating over both folders).

Obviously this has to be fixed in cordova-android project itself; otherwise whenever you do cordova platform rm your fixes will be gone.

I opened the ticket on Cordova JIRA:

https://issues.apache.org/jira/browse/CB-12677

2
votes

SOLUTION FOR Mac

Sample problem but I found my solution with brew.
1. Make sure you have the latest Android Studio installed.
2. Confirm from SDK manager that you have the required SDKs installed.
3. (optional)you could have an AVD installed as well.
4. install Homebrew.

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

5. Then run brew update to make sure Homebrew is up to date.

brew update

6. Run brew doctor to make sure everything is safe

brew doctor

7. Add Homebrew's location to your $PATH in your .bash_profile or .zshrc file.

export PATH="/usr/local/bin:$PATH"

8. If you don't have Node already installed, add:

brew install node

9. (Optional) To test out your Node and npm install, try installing Grunt (you might be asked to run with sudo)

npm install -g grunt-cli

10. Install Gradle

brew install gradle

Run: cordova run android --device with you device connected on a Mac and you have gradle working this time.

1
votes

I tried many but it didnt work finally ,

  1. I installed gradle using macos port
  2. I set GRADLE_HOME path as it told after installing gradle
  3. I executed ::

sudo chmod -R a+rwx /Library/Java/JavaVirtualMachines *

sudo chmod -R a+rwx *

sudo chmod 777 {your-gradle-path}

It immediately started working

1
votes

same problem but very simple on Mac with brew:

  • brew update
  • brew install gradle
1
votes

Windows 10 (with Android Studio installed)

With this solution, you don't have to install Gradle multiple times on your system.

  1. Find Gradle installation folder: Go to C:\Users\<username>\.gradle\wrapper\dists. Pick a version (in my case gradle-6.4-all). Navigate further until you reach the 'bin' folder.

  2. Add the path to the Path environment variable: Start menu > enter 'edit environment variables'. Look for the 'Path' variable in the lower section (system variable) and click 'Edit'. Then add as a new value the complete path, in my case C:\Users\<username>\.gradle\wrapper\dists\gradle-6.4-all\h8pi7famayzdg9sl28jayaip\gradle-6.4\bin. Don't forget to save your changes!

  3. Don't forget to restart all the terminals (e.g. restart VS Code). After that, it worked on my computer!

0
votes

Perhaps you installed the latest Android Studio 2.3.1, it changed command android list targets to avdmanager list targets, so there is how to fix it:

add path <android-sdk>/tools/bin to PATH, this is for MacOX.

export PATH="$PATH:/Users/<username>/Library/Android/sdk/tools/bin"

change your cordova project code <your-cordova-project>/platforms/android/cordova/lib/android_sdk.js.

change

module.exports.list_targets_with_android = function() {
    return superspawn.spawn('android', ['list', 'targets'])

to

module.exports.list_targets_with_android = function() {
    return superspawn.spawn('avdmanager', ['list', 'targets'])
0
votes

Update your cordova to the latest version and the issue should be resolved. In case the issue not resolved please set the path in the environment variable (in case of Windows). Example: System Variable Value name GRADLE_HOME Value D:\Android\Android Studio\gradle\gradle-4.3.1 (please replace with your path)

0
votes

Run in terminal:

$ sudo apt-get update
$ sudo apt-get install gradle

This works for me in Ubuntu 18.04

0
votes

Solution for linux and specifically Ubuntu 20:04. First ensure you have Java installed before proceeding:

1. java -version
2. sudo apt-get update
3. sudo apt-get install openjdk-8-jdk

Open .bashrc

vim $HOME/.bashrc 

Set Java environment variables.

export JAVA_HOME="/usr/lib/jvm/java-1.8.0-openjdk-amd64"
export JRE_HOME="/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre"

Visit Gradle's website and identify the version you would like to install. Replace version 6.5.1 with the version number you would like to install.

 1. sudo apt-get update
 2. cd /tmp && curl -L -O https://services.gradle.org/distributions/gradle-6.5.1-bin.zip
 3. sudo mkdir /opt/gradle
 4. sudo unzip -d /opt/gradle /tmp/gradle-6.5.1-bin.zip

To setup Gradle's environment variables use nano or vim or gedit editors to create a new file:

sudo vim /etc/profile.d/gradle.sh

Add the following lines to gradle.sh

export GRADLE_HOME="/opt/gradle/gradle-6.5.1/"
export PATH=${GRADLE_HOME}/bin:${PATH}

Run the following commands to make gradle.sh executable and to update your bash terminal with the environment variables you set as well as check the installed version.

1. sudo chmod +x /etc/profile.d/gradle.sh
3. source /etc/profile.d/gradle.sh
4. gradle -v
0
votes

Solution for Windows (Windows 10) is:

  1. Make sure you have Java 8 installed
  2. Download Gradle binary from https://gradle.org/install/
  3. Create a new directory C:\Gradle with File Explorer
  4. Extract and copy gradle-6.7.1 to C:\Gradle
  5. Configure your PATH environment variable to include the path C:\Gradle\gradle-6.7.1\bin
  6. Run gradle -v to confirm all is okay. C:>Gradle -v

Gradle 6.7.1

Build time: 2020-11-16 17:09:24 UTC Revision: 2972ff02f3210d2ceed2f1ea880f026acfbab5c0

Kotlin: 1.3.72 Groovy: 2.5.12 Ant: Apache Ant(TM) version 1.10.8 compiled on May 10 2020 JVM: 1.8.0_144 (Oracle Corporation 25.144-b01) OS: Windows 10 10.0 x86

C:>

Finally, run [Cordova environments] to check that all is set for development.

C:\Users\opiyog\AndroidStudioProjects\IONIC\SecondApp>cordova requirements

Requirements check results for android: Java JDK: installed 1.8.0 Android SDK: installed true Android target: installed android-30,android-29,android-28,android-27,android-26,android-25,android-24,android-23,android-22,android-21,android-19 Gradle: installed C:\Gradle\gradle-6.7.1\bin\gradle.BAT

-2
votes

“Android target: not installed”

Please install Android target / API level: "android-25".

You need to create an Android Virtual Device (API level: "android-25" or latest) or need to add android real device.

Also you can import the project in Android Studio and run.

You can check here:

https://cordova.apache.org/docs/en/latest/guide/platforms/android/index.html

https://developer.android.com/studio/command-line/adb.html