208

I know there are lots of questions similiar to this one, but i couldn't find a solution for my problem in any of those. Besides, I'll provide details for my specific case.

I coded an Ionic project in Ubuntu 16.04 LTS, and now I have to build it for release. So I run the command:

cordova build --release android

And I'm shown the following error:

Error: Could not find gradle wrapper within Android SDK. Might need to update your Android SDK.
Looked here: /home/user/Android/Sdk/tools/templates/gradle/wrapper
  • I don't have this templates/gradle/wrapper directory.
  • My Android Studio is 2.3, the latest version for now
  • Android SDK Platform-Tools 25.0.3
  • Android SDK Tools 25.3.1
  • All Android versions from 2.1 (Eclair) to 7.1.1 (Nougat)

After extensive research, I put all the Android Studio-related environment variables in the file /etc/environment. So now it looks like this:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/game:/home/<user>/Android/Sdk:/home/<user>/Android/Sdk/tools:/home/<user>/Android/Sdk/platform-tools"

ANDROID_HOME=/home/<user>/Android/Sdk
export ANDROID_HOME

JAVA_HOME=/usr/lib/jvm/java-8-oracle
export JAVA_HOME

GRADLE_HOME=/opt/android-studio/gradle/gradle-3.2
export GRADLE_HOME

Now, for the sake of testing the environment variables, I run the following commands:

source /etc/environment
echo $PATH
echo $ANDROID_HOME
echo $JAVA_HOME
echo $GRADLE_HOME

And all the path variables are correctly displayed.

So, it looks like the environment variables are like they should be according to the various similar questions and in tutorials i've searched. Does anyone know what am I doing wrong? Why do I still get the Gradle Wrapper error?

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
rbenvenuto
  • 2,415
  • 2
  • 12
  • 20

24 Answers24

258

Edit 3:

Cordova Android 6.2.2 has been released and it's fully compatible with SDK tools 26.0.x and 25.3.1. Use this version:

cordova platform update android@6.2.2

or

cordova platform rm android
cordova platform add android@6.2.2

Edit 2:

There has been another Android SDK tools release (26.0.x) that is not fully compatible with cordova-android 6.2.1.

Edit: Cordova Android 6.2.1 has been released and it's now compatible with latest Android SDK.

You can update your current incompatible android platform with cordova platform update android@6.2.1

Or you can remove the existing platform and add the new one (will delete any manual change you did inside yourProject/platforms/android/ folder)

cordova platform rm android cordova platform add android@6.2.1

You have to specify the version because current CLI installs 6.1.x by default.

Old answer:

Sadly Android SDK tools 25.3.1 broke cordova-android 6.1.x

For those who don't want to downgrade the SDK tools, you can install cordova-android from github url as most of the problems are already fixed on master branch.

cordova platform add https://github.com/apache/cordova-android

jcesarmobile
  • 51,328
  • 11
  • 132
  • 176
  • Will be correct answer once cordova-android 6.2.0 is released, at the moment is in development and not everybody is willing to use it before it's released. – jcesarmobile Mar 15 '17 at 18:32
  • Where can I keep track of this issue and cordova-android releases? – Shawn Mar 16 '17 at 00:31
  • I also got the following error when running `cordova run android --list`. Is this related? `An unexpected error has occured while running list-emulator-images with code 2: Error: /Users/Shawn/Desktop/FRCCApp/platforms/android/cor dova/lib/list-emulator-images: Command failed with exit code 2` – Shawn Mar 16 '17 at 01:09
  • Yes, that error is also related. Most of the problems are linked in this issue https://issues.apache.org/jira/browse/CB-12554. Once it's released, it will be announced on the blog https://cordova.apache.org/blog/ and on twitter. – jcesarmobile Mar 16 '17 at 08:18
  • 7
    The release is now up on the npm repositories, although it doesn't seem to be being used automatically yet. `cordova platform add android@6.2.0` works, though. – Jules Apr 01 '17 at 04:43
  • Yeah, it requires a CLI release to be used as default, that will take longer – jcesarmobile Apr 01 '17 at 07:45
  • Removing platform and adding it again just worked for me. – Irfan Raza Apr 14 '17 at 23:31
  • I'm using Android Tools 26.0.2 with android@6.2.3 but I still have the issue 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 – Michael Burger May 08 '17 at 15:43
  • @MichaelBurger that's a different issue, you now need Gradle installed in your system and set it in your path. – jcesarmobile May 08 '17 at 16:12
  • Using `cordova platform add https://github.com/apache/cordova-android` solved it for me. Thanks. – chrjs May 09 '17 at 10:37
  • 3
    @chrjs `cordova platform add android@6.2.3` is safe to use now, you don't need to add github version and it might be unstable, better use 6.2.3 as is the latest released one – jcesarmobile May 09 '17 at 10:42
  • 1
    I'm on Cordova 7.0.1 and I'm getting the error: `Error: Could not find gradle wrapper within Android SDK. Might need to update your Android SDK.` Any advice? – Ethan Keller May 16 '17 at 13:26
  • 1
    @EthanKeller but did you had the android platform added previously? If that's the case, remove it `cordova platform rm android` re add it `cordova platform add android` – jcesarmobile May 16 '17 at 14:19
  • Removed, added 6.2.2, got error `cordovaProject.projectConfig.getFileResources is not a function` –  May 26 '17 at 17:17
  • You also need Cordova CLI 7.0.1 – jcesarmobile May 26 '17 at 18:46
  • just removing and adding the platform or updating the android platfrom fixes this issue. Confirmed after 4 hours of frustration. Thanks. – Vignesh May 31 '17 at 08:12
  • What must we write in config.xml in case using 7.0.1 ? Which SDK Version? – Stephan Kristyn Jun 30 '17 at 15:54
  • @nottinhill I don't understand the question, I didn't mention anything about the config.xml on my answer – jcesarmobile Jun 30 '17 at 17:24
  • 1
    Why not just `android@latest`? – Seanny123 Sep 04 '17 at 01:54
  • @Seanny123 by the time I answered, it wasn't even released, I've been editing with the version numbers that were fixing issues related to the Google changes. Now you can use `android@latest` without problems. – jcesarmobile Sep 04 '17 at 07:39
  • I m using latest version of cordova, android@6.2.3 is default. The error still exists – sziraqui Sep 24 '17 at 15:30
  • @sziraqui did you remove and re add the platform? – jcesarmobile Sep 24 '17 at 17:31
  • yes, tried **each and very solution mentioned on this post**, finally i had to install gradle from ubuntu repo – sziraqui Sep 24 '17 at 18:06
  • @sziraqui, ah, yeah, since cordova-android 6.2.1 it requires Android Studio or Gradle to be installed on the user's system to work – jcesarmobile Sep 24 '17 at 18:28
  • I have Android Studio installed and working, I work on native android, I m trying cordova. Why can't cordova detect gradle that comes with Android Studio? – sziraqui Sep 24 '17 at 18:31
  • It's supposed to detect it, it works in macOS, probably nobody tested it on ubuntu, can you report it on issues.cordova.io? – jcesarmobile Sep 24 '17 at 18:38
  • ionic cordova platform add android@latest fixed my prob . Thanks for ans – Usman Iqbal Oct 12 '17 at 12:56
  • @LAnantaPrasad latest is 7.1.1, that should also work and it's newer, or at least 6.4.0 that is latest 6.x.x – jcesarmobile Aug 03 '18 at 13:15
  • Great fix! Recently upgrade to latest android studio and just needed to update cordova android to the above. Way to go! – Alex Rindone Mar 26 '19 at 12:42
  • well, you could give cordova-android 8.0.0 a go as it's latest one – jcesarmobile Mar 26 '19 at 12:50
  • Got me past there - now I get `Could not determine java version from '13.0.2'.` – kris Oct 26 '20 at 04:22
  • Cordova only supports java 8 at the moment – jcesarmobile Oct 26 '20 at 08:09
247

I just experienced the same problem.

It may be an occlusion in the instructions regarding how to install (or upgrade) Android Studio with all the SDK Tools which both you and I missed or possibly a bug created by a new release of Studio which does not follow the same file conventions as the older versions. I lean towards the latter since many of the SO posts on this topic seems to point to an ANDROID_PATH with a folder called android-sdk which does not appear in the latest (2.3.0.8) version.

There appears to be a workaround though, which I just got to work on my machine. Here's what I did:

  • Download tools_r25.2.3-windows.zip from Android Downloads.

  • Extracted zip on desktop

  • Replaced C:\Users\username\AppData\Local\Android\sdk\tools with extracted sub-folder tools/

  • In project folder:

    $ cordova platforms remove android
    $ cordova platforms add android

You may also need to force remove the node_modules in android. Hopefully this helps.

Arunkumar Chandrasekaran
  • 1,211
  • 4
  • 21
  • 40
R J
  • 4,473
  • 2
  • 22
  • 29
  • 1
    THX, I've been trapped in the same phenomenon, now recovered my environment by your remedy. I think it is the fault of Google, so cordova developers shall wait for the next, corrected update. – HirofumiTamori Mar 07 '17 at 09:38
  • 1
    Had exactly the same issue. Downgraded "Android SDK Tools" 25.2.3 which fixed the issue. We should probably wait for update from Google/Android team. – gregor Mar 07 '17 at 10:31
  • 1
    THANKS! Your answer worked perfectly. We'll have to wait for an update on this bug. You saved my work :D – rbenvenuto Mar 08 '17 at 13:56
  • Thanks, this solved the problem, found similar workaround here https://forum.ionicframework.com/t/error-could-not-find-gradle-wrapper-within-android-sdk/79527/6 – Vadim Tofan Mar 08 '17 at 21:37
  • 3
    Replacing only the tools folder did the trick for me. Did not have to remove and add the platform. – vahissan Mar 13 '17 at 15:05
  • I did the same thing and worked. But when I updated tools from Android SDK manager, the same problem resurfaced - 'ANDROID_HOME' missing and after setting that I got error message - 'gradle file missing' while building the App via PhoneGap CLI. – aB9 Apr 20 '17 at 07:52
  • 16
    Very useful, for Mac you can do the same except for downloading tools for Windows, you download from Mac on this address: https://dl.google.com/android/repository/tools_r25.2.3-macosx.zip – O. Edholm May 09 '17 at 15:24
  • 14
    The original file can be accessed directly here: https://dl.google.com/android/repository/tools_r25.2.3-windows.zip?hl=id –  May 26 '17 at 15:36
  • 1
    Tools for linux: https://dl.google.com/android/repository/tools_r25.2.3-linux.zip – Ferdinand.kraft Jul 20 '17 at 01:28
  • I am still get similar after manually downloading sdk tools .please any one me.getting Error: Could not find an installed version of Gradle either in Android Studio"Android SDK Tools : 25.2.5,Cordova Platforms : android 6.2.3 ,Ionic Framework : ionic1 1.3.2.please any one help me i open new question.here (https://stackoverflow.com/questions/45770757/error-while-building-android-project-in-ionic-v1) – arun G Aug 19 '17 at 13:25
  • downloaded https://afterroot.wordpress.com/2016/01/01/android-sdk-tools-direct-links/ and replaced from /tools to 25.2.5 and tried to install plugin again and worked fine – Kiran S Sep 14 '17 at 10:04
  • For me, removing and then adding the android plugin did the trick (didn't have to do anything else). – Vladimir Oct 13 '17 at 13:16
  • Thank you so much it's worked for me while replacing tools_r25.2.3. – Karnan Muthukumar Jul 10 '18 at 12:35
  • tnx, i was searching all day – Godfather Jul 27 '20 at 12:33
57

(Basically what @user3464070 already said)

For Mac:

cd ~/Library/Android/sdk
# download latest tools
curl -O https://dl.google.com/android/repository/tools_r25.2.3-macosx.zip
# overwrite existing tools folder without prompting
unzip -o tools_r25.2.3-macosx.zip
# clean up
rm tools_r25.2.3-macosx.zip
basecode
  • 1,490
  • 12
  • 13
45
  1. If you have android studio installed within your system, then copy the templates folder from C:\Program Files\Android\Android Studio\plugins\android\lib\templates
  2. Paste it in the folder C:\Users\<user-name>\AppData\Local\Android\sdk\tools
  3. Run the command: ionic build android

All necessary jar files will be downloaded and apk file for the application will be generated.

Note: Set environment variables to C:\Users\<user-name>\AppData\Local\Android\sdk\tools. Also set user-name to your current username.

Dirk Jan
  • 2,355
  • 3
  • 20
  • 38
vinayb21
  • 535
  • 4
  • 11
25

For Linux there is even a bit better solution.

Navigate to https://developer.android.com/studio/index.html#downloads

and download the command line tools zip file (bottom of the page) for linux. Extract them to your ..../Android/Sdk/ folder so you overrwrite/extend your tools folder. Now everything works fine.

user3464070
  • 359
  • 2
  • 2
  • Yeah, R J answered the Windows workaround for the problem, and your answer is the Linux solution. Useful for someone who doesn't know how to solve Windows' `C:\Users\username\AppData\Local\Android\sdk\tools` to Ubuntu's `/home/user/Android/Sdk` – rbenvenuto Mar 09 '17 at 19:52
  • @rbenvenuto The answers are basically the same. Download tools, extract the zip folder and save the extracted content to your idk's tools folder. How difficult can it be to understand? I'm using a Mac and will save to ~/Library/Android/sdk/tools Need I steal someone's up votes for explaining the same thing in different words? – iOSAndroidWindowsMobileAppsDev Mar 10 '17 at 12:37
  • 4
    @Anon Yep, can't deny his answer was pretty much the same explanation with different words. But i can see some value in it for struggling beginners like me lol – rbenvenuto Mar 10 '17 at 17:08
  • previous versions are not available anymore in developer.android.com – Ferdinand.kraft Jul 20 '17 at 01:36
20

For anyone who is still having this issue, this worked for me:

    cordova platform update android@latest

then build and it will automatically download the newest gradle version and should work

Mans
  • 2,953
  • 2
  • 23
  • 32
  • 2
    +1 This works as @ August 2017 and updates you to `cordova-android@6.2.3`. Older answers may or may not work or be correct any more. This situation keeps changing and is directly caused by Android SDK 25.3.1 dropping support for the android binary, as described here: https://cordova.apache.org/announcements/2017/04/05/android-release.html – JK. Aug 11 '17 at 04:48
  • 1
    Still working as at Mar 2018. I don't create new cordova projects often, this is the first since my previous comment. It had the same `could not find gradle wrapper` error. I had to look up the solution on SO again. This is a per project fix, must be applied to every new cordova project you create. Would upvote twice if I could, this is the 2nd time this answer has saved me now. – JK. Mar 30 '18 at 07:21
12

There's no need to downgrade Android Tools. On Windows, Gradle moved from:

C:\Users\you_username\AppData\Local\Android\sdk\tools

to:

C:\Program Files\Android\Android Studio\plugins\android\lib\templates\gradle\wrapper

So you just need to adjust your path so that it points to the right folder.

Pang
  • 9,564
  • 146
  • 81
  • 122
herve
  • 525
  • 6
  • 17
  • 6
    I did the opposite, moving the wrapper to my AppData and it works fine ! Thanks ^_^ – Nicolas Le Bot Apr 25 '17 at 15:52
  • 1
    [A new version of Cordova has been published recenly (five days ago) to solve this problem](http://cordova.apache.org/announcements/2017/04/28/android-release.html) – herve May 03 '17 at 06:17
  • Copy the content of the wrapper folder from this path C:\Program Files\Android\Android Studio\plugins\android\lib\templates\gradle\wrapper to C:/Android/Sdk/tools/templates/gradle/wrapper – inspiredMichael Jun 06 '17 at 16:44
8

if you have this error when you ionic run android --device

## codova - android update 
npm install -g cordova
cordova platform update android
xsor
  • 1,074
  • 11
  • 11
6

Just copied everything from typical Android Studio installation. From: D:\sw\dev\android\studio\plugins\android\lib\templates

(the whole templates directory)

To:

D:\sw\dev\android\sdk\tools

Tested with ionic build android

AlexGera
  • 756
  • 9
  • 19
  • This was the solution in my case. After this solution, you might get the license message. See https://stackoverflow.com/questions/40383323/cant-accept-license-agreement-android-sdk-platform-24/40383457#40383457 – Jonathan Römer Jan 18 '19 at 14:54
6

This problem occurs when you dont have the gradle wrapper into your SDK tools.

Like the previous responses said, you can update to last cordova-android version (it works), but if you want to keep working with cordova-cli@6.4.0 and cordova-android@6.0.0, just copy the folder from android studio ex.;

cd /Applications/Android\ Studio.app/Contents/plugins/android/lib/
cp -r templates/ $ANDROID_HOME/tools/templates
chmod +x $ANDROID_HOME/tools/templates/gradle/wrapper/gradlew

In my case, I am using the cordova-plugin-fcm and it was tested with old version of cordova-android, so I can not use cordova-android@2.x (not yet).

Bruno Agutoli
  • 200
  • 3
  • 8
4

For Linux just make a symlink, like this:

ln -s /android/android-studio/plugins/android/lib/templates /android/sdk/tools/templates
Mac
  • 49
  • 2
4

On linux (Ubuntu in my case) just install gradle:

sudo apt-get install gradle

Edit: It seems as though ubuntu repo only has gradle 2.10, for newer versions: https://www.vultr.com/docs/how-to-install-gradle-on-ubuntu-16-10

Mawcel
  • 1,967
  • 15
  • 22
  • 1
    None of the above solutions worked for me. I am using latest ver of cordova 7.0.1. Installing gradle from ubuntu repo solved the issue. Its 2017 and still cordova doesn't know to use Android studio's builtin gradle – sziraqui Sep 24 '17 at 15:59
3

This solved my problem.

I just downloaded https://dl.google.com/android/repository/tools_r25.2.5-macosx.zip214 and overwrite the tools folder at ~/Library/Android/sdk/tools

Dinesh Vaitage
  • 2,983
  • 19
  • 16
2

Just recently, I also encountered similar problem, and after I did this, it works:

I edited the file in /etc/profile

sudo nano /etc/profile

export JAVA_HOME=/home/abdul/java/jdk1.8.0_131
export PATH=$PATH:$JAVA_HOME/bin

export ANDROID_HOME=/home/abdul/Android/Sdk
export PATH=$PATH:$ANDROID_HOME/tools
export PATH=$PATH:$ANDROID_HOME/platform-tools

export GRADLE_ANDROID_HOME=/home/abdul/android-studio/gradle
export PATH=$PATH:$GRADLE_ANDROID_HOME/gradle-3.2/bin
export PATH=$PATH:$GRADLE_ANDROID_HOME/m2repository

Other info (just in case):

Not quite sure about m2repository part, in the first try it pass the grandle but there is another error (gradlew-command-failed-with-exit-code-

  1. I check if in android studio the repository is active, and it's not active, I try to activate it, and when I try it again (Cordova build Android), it download a few other file, maybe from the repository? And then when I delete the path, it still works. (also thanks to Marcin Orlowski sample so then I can understand about export path better).

I use:

  • Linux Mint Serena
  • node : v6.10.3
  • npm : 3.10.10
  • Cordova : 7.0.0
  • Android Studio : 2.3.1
  • Android SDK platform-tools : 25.0.5
  • Android SDK tools : 26.0.2

Hope it can help anyone who might have the same problem like mine and need this too.

Thanks

Unheilig
  • 16,196
  • 193
  • 68
  • 98
1

No need to update sdk. Try to install it manually. Follow instructions in this link

https://gradle.org/install

Arjun G
  • 2,194
  • 1
  • 18
  • 19
1

None of the solutions worked for me as of today. My situation was that I got my Android studio updated. The most popular thing to replace the tools folder with the latest one wouldn't work for me. Infact is not required in some cases.

npm update -g cordova did the trick for me.

Then I removed the platform and added it again.

ionic platform remove android

ionic platform add android

This works for me in Ionic. I am surecordova platform remove/add android will do the same stuff. Not tested though.

Working again !

HalfWebDev
  • 7,022
  • 12
  • 65
  • 103
1

After trying everything here twice in different order, I reinstalled everything and before doing cordova platform add android I went to templates/gradle and ran gradlew.bat. After this completed, I was able to add the android platform without any problem.

0

This solved it for me:

cordova platform update android@6.1.2
Paranoid Android
  • 4,672
  • 11
  • 54
  • 73
-1

Run:

cordova platform update android@6.1.2

And make sure that Android Studio and Android Studio environment variable refrence to same location.

I fix this by copying Android Studio from:

D:\Program Files\Android

to

C:\Program Files\Android
Unheilig
  • 16,196
  • 193
  • 68
  • 98
-1

After cordova upgrade to 7.0.x, the gradle path has been changed as discussed above. To get it working before official fix comes out, just manually install the gradle and set it in your Environment.

  1. Go https://gradle.org/install download the Binary packages
  2. Unzip it,remember the path, eg., My one at: E:\Gradle
  3. Set the environment variables, like on Windows add E:\Gradle\bin

That's it.

Peter.Wang
  • 2,051
  • 1
  • 19
  • 13
-1

After experienced the same issue, just rename the package name in config.xml, attribute id with a name without "-" inside...

i.e.

com.web-projet.appname

renamed into :

com.webprojet.appname

and all was correct...

If it helps.

Jean-Luc Aubert
  • 620
  • 5
  • 19
-1

Installing cordova version 5.0 helps me:

npm install -g cordova@5.0
Bugs
  • 4,491
  • 9
  • 32
  • 41
fvukovic
  • 699
  • 1
  • 7
  • 14
-1

I am using Ubuntu 16.04 and for me, it worked by just using two commands:-

ionic cordova platform rm android
ionic cordova platform add android@6.3.0
Niraj
  • 1,039
  • 1
  • 10
  • 14
-2

Easy and simple solution for MAC

My Issue was

cordova build android
ANDROID_HOME=/Users/jerilkuruvila/Library/Android/sdk
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home
Error: Could not find gradle wrapper within Android SDK. Might need to update your Android SDK.
Looked here: /Users/jerilkuruvila/Library/Android/sdk/tools/templates/gradle/wrapper

Solution

jerilkuruvila@Jerils-ENIAC tools $ cd templates
-bash: cd: mkdir: No such file or directory
jerilkuruvila@Jerils-ENIAC tools $ mkdir templates
jerilkuruvila@Jerils-ENIAC tools $ cp -rf gradle templates/
jerilkuruvila@Jerils-ENIAC tools $ chmod a+x templates/

cordova build android again working now !!!

Jeril Kuruvila
  • 17,190
  • 1
  • 20
  • 23