'Cordova build changes distributionUrl in gradle-wrapper.properties file

I keep getting the following build exception when I run

 cordova run android --verbose
  • What went wrong: A problem occurred evaluating root project 'android'.

    Failed to apply plugin [id 'android'] Gradle version 2.10 is required. Current version is 2.2.1. If using the gradle wrapper, try editing the distributionUrl in C:\Users\Project\gradle\wrapper\gradle-wrapper.properties to gradle-2.10-all.zip

The reason for this is the line being changed when I run the cordova build command from;

distributionUrl=http\://services.gradle.org/distributions/gradle-2.1.0-all.zip

to

distributionUrl=http\://services.gradle.org/distributions/gradle-2.2.1-all.zip

Any way to prevent this ?



Solution 1:[1]

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

Solution 2:[2]

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

Solution 3:[3]

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.

Solution 4:[4]

This happened to us because we accidentally let Android Studio build our cordova project causing endless errors.

Save yourself while you can:

cordova platform rm android
cordova platform add android

Solution 5:[5]

Inside platforms/android/cordova/lib/builders/ProjectBuilder.js

around line 232 you will see:

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

adjust the gradle to whatever you need.

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

Solution 6:[6]

You can try out the following to resolve the issue,

On Windows, Change File -> Settings -> Builds,Execution,Deployment -> Build Tools -> Gradle -> Gradle home path

On Mac OS, Change Android Studio -> Preferences -> Builds,Execution,Deployment -> Build Tools -> Gradle -> Gradle home path

Else use default gradle wrapper and edit Project\gradle\wrapper\gradle-wrapper.properties file's distributionUrl like this:

distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip

Solution 7:[7]

This was happening to me because I wasn't building with the latest version of cordova-android. so:

cordova platform rm android
cordova platform add android@latest

Take not of the @latest directive. Also be sure to update download the latest version of gradle and include it in the appropriate directory, as pointed out in the other answers.

Solution 8:[8]

Please check with Environment Variable.

My Computer > Properties > System Properties > Advanced > Environment Variables... > Under 'System variables' find following setting & try to change it as per your gradle version.

CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL = https://services.gradle.org/distributions/gradle-6.5-all.zip

Just save the settings and reopen command windows. And try to build again.

Solution 9:[9]

Try just open the file project/platforms/android/build.gradle and update

dependencies {
      classpath 'com.android.tools.build:gradle:X.X.X'
}

and

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

see the reference here https://developer.android.com/studio/releases/gradle-plugin.html#updating-gradle

to change the X.X

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 miken32
Solution 3
Solution 4 Lightbeard
Solution 5 AwesomeJ0sh
Solution 6 Devid Farinelli
Solution 7 Cedric Ipkiss
Solution 8 Arjun Bhatt
Solution 9 linhadiretalipe