Cordova build changes distributionUrl in gradle-wrapper.properties file

UPDATE: 21 May 2017

Ok, this was a really nasty issue and I felt compelled to write complete instructions to fix in the hope it saves others the hours I wasted on it. The following is a combination of googling, reading the accepted answer, and working things out from trial and error.

Before you do anything else.. make sure you close Docker if it is running. A symptom is that in Android Studio, the emulator load dialog just silently vanishes. If you ran from the app start command, your application is simply left waiting to connect to device (message issued).

Assuming you have done that and still not working, read on.

Optional (maybe): fyi only - Personally, I used SDK Man to install a matching version of Gradle on my machine.

Assume you have already got a working Android Studio emulator with Marshmallow or Nougat (or both).

In my case, I was then trying to get Cordova to work.

Depending on whether you wish to start over (recommended) then:

cordova platform rm android
cordova platform add android

First a download is needed. Download the following according to your OS:

https://dl.google.com/android/repository/tools_r25.2.3-windows.zip

https://dl.google.com/android/repository/tools_r25.2.5-linux.zip

(I was running on Mac, so downloaded:)

https://dl.google.com/android/repository/tools_r25.2.5-macosx.zip

Unzip it, and copy the templates folder into /Users/arcseldon/Library/Android/sdk/tools

Explanation: See the logic inside module.exports.check_gradle function, declared under platforms/android/cordova/check_reqs.js to understand how it is trying to derive the path for the gradle wrapper. This co-erces it.

$ echo $ANDROID_HOME
/Users/arcseldon/Library/Android/sdk

I could see a tools subdirectory, but no templates folder inside that. So copy the templates folder from the downloaded and extracted zip, and paste it inside $ANDROID_HOME/tools

You should finish with a path hierarchy that includes:

/Users/arcseldon/Library/Android/sdk/tools/templates/gradle/wrapper 

This now satisfies the path search from the script perspective.

Secondly, check your cordova application for platforms/android/build.gradle, and search for the gradleVersion.

task wrapper(type: Wrapper) { gradleVersion = '2.14.1' }

For your version, download from URL as follows:

https://services.gradle.org/distributions/gradle-X.Y.Z-all.zip

replacing X, Y, Z as needed to match your version.

In my case,

https://services.gradle.org/distributions/gradle-2.14.1-all.zip

other examples:

https://services.gradle.org/distributions/gradle-3.3-all.zip https://services.gradle.org/distributions/gradle-2.4-all.zip

Then, once downloaded, place on your local drive and reference with ENV. variable. eg. (changing to your version)

export CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL=file:///Users/myusername/temp/gradle-2.14.1-all.zip

Explanation: If you run a search you'll find this environment variable is an optional override for this env. variable. See the variable assignment for check_gradle, declared under platforms/android/cordova/lib/builders/GradleBuilder.js

Now just run:

cordova run android --verbose

All going well, everything should now "just work" for Android. In my case, iOS was out of the box - but I did use Xcode 8.2 instead of Xcode 8.3 for other reasons.


As could be guessed, there is a script that is being run behind the scene when you issue the "Cordova build android" command. This script needs to be found in order to see the config specified for Gradle version.

You must go and check the following js file:

$PROJECT_ROOT/platforms/android/cordova/lib/builders/GradleBuilder.js

Then find the line including the following variable in the file:

GradleBuilder.prototype.prepEnv

And check the distributionUrl variable:

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

That's what you are looking for and what you need to change to gradle-2.10-all.zip URL;

Now you can run the build command, and there you go!

inspired from the following SO; install gradle for using in cordova build android


I think is better to export the variable CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL

Example:

export
CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL=http\\://services.gradle.org/distributions/gradle-2.14.1-all.zip

If you are using a Mac, for example, add the export command in to the .bash_profile

nano ~/.bash_profile