ionic 2 - Error Could not find an installed version of Gradle either in Android Studio

CordovaIonic2Android Gradle-PluginCordova Plugins

Cordova Problem Overview


I create ionic 2 project and add diagnostic cordova plugin like this :

ionic plugin add cordova.plugins.diagnostic

npm install --save @ionic-native/diagnostic

and add android platform like this :

ionic platform add android@latest

but when build with ionic build android console give me this 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 Studio

and I try to download manualy gradle.3.3-all.zip and change this distributionUrl var in platform/android/cordova/lib/builders/GradleBuilder.js

 var distributionUrl = process.env['CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL'] || 'https\\://services.gradle.org/distributions/gradle-3.3-all.zip'

to:

var distributionUrl = process.env['CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL'] || 'file:///E:/gradles/gradle-3.3-all.zip';

but not working and console give me last error.

I dont know how to add gradle for android@latest version

Cordova Solutions


Solution 1 - Cordova

Life is too short to spend it on messing with paths, etc.

Since I had Homebrew installed on the iMac, I just ran this command:

brew install gradle

The Ionic3 project started to build successfully.

Solution 2 - Cordova

Try installing gradle and include it into your path. Click the link below to get manual. https://gradle.org/install

Solution 3 - Cordova

Just install gradle on linux, Even if Android Studio is installed,

> sudo apt install gradle

Solution 4 - Cordova

For windows users:

Download gradle binary from the link in the answer Gradle Download

Extract the zip file to 'C:\Gradle' or somewhere else

open Edit Environment variable dialog from start menu > Search

Click 'New' under system variables and add as below

Variable Name GRADLE_HOME Variable Value C:\Gradle\gradle-4.0.1

Then choose PATH variable from system variable list

append the gradle path to variable value like this C:\Gradle\gradle-4.0.1\bin

then press win Key+R type cmd then enter > in command terminal type gradle -v

if the setup is correct you will see the gradle installation details

Solution 5 - Cordova

I was already installed Android Studio and it's just need to add gradle PATH to ~/.bash_profile on my MacOSX Mojave. Also if gradle is upgraded then path might need to update again.

Example .bash_profile :

export ANDROID_SDK_ROOT="~/Library/Android/sdk"
export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
export JAVA_HOME=$(/usr/libexec/java_home)
export GRADLE_PATH="~/.gradle/wrapper/dists/gradle-4.10.1-all/455itskqi2qtf0v2sja68alqd/gradle-4.10.1/bin"
export ANDROID_STUDIO="/Applications/Android Studio.app/Contents/MacOS"
export PATH="$PATH:$GRADLE_PATH:$ANDROID_STUDIO"

When edited your .bash_profile then run a command below to read it again.

source ~/.bash_profile

Solution 6 - Cordova

For Windows you can try below solution

Copy your gradle-->bin path and add it to system environment variable path.

In my case gradle path is

C:\Users\username\.gradle\wrapper\dists\gradle-3.3-all\55gk2rcmfc6p2dg9u9ohc3hw9\gradle-3.3\bin

This solution worked for me.

Solution 7 - Cordova

In Ubuntu, Installing latest version of gradle solved the issue for me.

Try these steps to install the latest version,

sudo add-apt-repository ppa:cwchien/gradle

sudo apt-get update

sudo apt-get install gradle

then build using,

cordova build android or ionic cordova build android

Note: If you install gradle from ubuntu repo, it will install the old version 1.4 and will not help, so sudo apt-get install gradle alone will not help most times, if you did not add the repo ppa:cwchien/gradle earlier

Solution 8 - Cordova

There is a problem with cordova version 6.4.0 and android 6.2.1 .so,please try with below compatible versions

cordova version must be 6.4.0

> sudo npm install -g [email protected]

Install android in your project

> sudo cordova platform add [email protected]

 or

> sudo ionic platform add [email protected]

Solution 9 - Cordova

brew install gradle

In short that will save time :) Ionic team please fix this

Solution 10 - Cordova

On Ubuntu 18.04, I intalled gradle with:

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

And Ready.

Solution 11 - Cordova

If you dont want to install gradle explicitly just to address this issue, you can overcome this by following the workaround as mentioned below:

  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: The above workaround will work fine till Cordova Android version 6.3.0. For Cordova Android 6.4.0 and above, Gradle needs to be installed as a standalone dependency. Please find Cordova Android 6.4.0 release notes for more info on this.

Solution 12 - Cordova

For Windows users:

Set-ExecutionPolicy RemoteSigned -scope CurrentUser
iex (new-object net.webclient).downloadstring('https://get.scoop.sh')
scoop install gradle

Solution 13 - Cordova

I am on Microsoft Windows:

I encountered this error after upgrading Android Studio from 2.3.3 to 3.0. I solved it by changing the path to gradle in the PATH environment variable, which is now "gradle-4.1" for Android Studio 3.0 (previously it was "gradle-3.2" for Android Studio 2.3.3)

Solution 14 - Cordova

Just install the Gradle through the command line If you are using Mac try

brew install gradle

Solution 15 - Cordova

I moved Android folder path to another path and taked this error.

I resolved to this problem in below.

I was changed to Gradle path in system variables. But not path in user variables. You must change to path in system variables

Screenshot this

Alternative Screenshot this

Solution 16 - Cordova

I would like to add my two cents to this conversation. If you are a Windows user, you would like to take advantage of Chocolatey

Per https://community.chocolatey.org/packages/gradle, you need to run in your console (it assumes you already have Chocolatey installed).

choco install gradle

If you haven't installed Chocolatey, follow the instructions at https://chocolatey.org/install

Solution 17 - Cordova

First of all uninstall ionic and cordova, later set your variables envarionment JAVA_HOME, ANDROID_HOME and PATH then reinstall ionic cordova and add platform like follow:

$ sudo npm install -g ionic cordova

$ ionic platform add android

The ionic will setup gradle packages properly.

Solution 18 - Cordova

I solve this problem

Download gradle.zip from this site https://gradle.org/install. And follow the instructions indicated in the site.

Then open another terminal cmd and execute the command:

Cordova build android --prod --release
End

Solution 19 - Cordova

In Arch Linux/Manjaro: sudo pacman -S gradle

Solution 20 - Cordova

just install android studio and select your sdk folder as a android studio's sdk default folder and then android studio repair all the damage area and download needed files.

Solution 21 - Cordova

Download the latest gradle from https://gradle.org/install and set the gradle path upto bin in your PATH variable and export path in the directory you are working in

example : export PATH=/home/abc/android-sdk-linux/tools:/home/abc/android-sdk-linux/platform-tools:/home/abc/android-sdk-linux/tools:/home/abc/android-sdk-linux/platform-tools:/home/abc/Downloads/gradle-4.4.1/bin

I spent my whole day resolve this and ultimately this solution worked for me,

Solution 22 - Cordova

If your gradle was working normally before and this came up (probably after you upgraded or reinstalled ionic or cordova - as was my case), you might want to leave the environment variable as is, and check if the folder listed there actually exists.

If it doesn't, rather than reinstalling gradle as mentioned above and changing the value of the environment variable, I had to recreate the old missing folder and copy the gradle files into them.

It seems the value of the gradle location is saved somewhere in Android Studio, and updating the PATH in system variables doesn't affect Android Studio checking for that same location after something triggers a change.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionReza MazarlouView Question on Stackoverflow
Solution 1 - CordovaAFDView Answer on Stackoverflow
Solution 2 - CordovaArjun G PerambraView Answer on Stackoverflow
Solution 3 - CordovaNicolasView Answer on Stackoverflow
Solution 4 - CordovarashidnkView Answer on Stackoverflow
Solution 5 - CordovaAlmas DusalView Answer on Stackoverflow
Solution 6 - CordovaMaheshvirusView Answer on Stackoverflow
Solution 7 - CordovaMohammed SafeerView Answer on Stackoverflow
Solution 8 - CordovaVeerendra BorraView Answer on Stackoverflow
Solution 9 - CordovaMadhup Singh YadavView Answer on Stackoverflow
Solution 10 - CordovaOvidioView Answer on Stackoverflow
Solution 11 - CordovaGandhiView Answer on Stackoverflow
Solution 12 - CordovaQuezakoView Answer on Stackoverflow
Solution 13 - CordovaKok Wai WongView Answer on Stackoverflow
Solution 14 - CordovaKuldeep KumarView Answer on Stackoverflow
Solution 15 - CordovaMetin SEVİNDİKView Answer on Stackoverflow
Solution 16 - CordovaAmadeus SánchezView Answer on Stackoverflow
Solution 17 - CordovaSilas BrasilView Answer on Stackoverflow
Solution 18 - CordovaRabah Maroc M2IView Answer on Stackoverflow
Solution 19 - CordovaMarcelo AgimóvelView Answer on Stackoverflow
Solution 20 - CordovaAjoy KarmakarView Answer on Stackoverflow
Solution 21 - CordovaMandeep SinghView Answer on Stackoverflow
Solution 22 - CordovaCedric IpkissView Answer on Stackoverflow