Updating Gradle version for Cordova mobile apps

Ocassionally, a new Cordova or plugin version will force you to upgrade your Gradle version. This process must be performed with caution, and this article shows how to chose a correct gradle version and install it on Windows.

1: Stop gradle daemon

Run this command to stop all gradle daemons, if any:

gradle --stop

2. Delete gradle cache

Locate and delete the gradle cache folder:

%USER_HOME%\.gradle\caches\

To free up space, you may delete the other folders in the same level of the cache folder if you don’t plan to use the other versions anymore.

3. Download a new gradle version

Rather than directly jumping to the latest Gradle version, choose a version compatible with the desired gradle plugin for Android.

Android Gradle version

Android Gradle version compatibility

Download the desired gradle version from the Gradle Distributions site. In this case I’m downloading gradle-5.1.1-bin.zip.

4. Unzip the downloaded file

Place the contents of the downloaded ZIP in any location such as C:\gradle\gradle-5.1

5. Add the bin path to environment variables

To edit environment variables in Windows I use Rapid Environment Editor but you can use the clumsy editor provided by Microsoft.

Set the CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL system variable to the URL of the downloaded gradle ZIP file:

https://services.gradle.org/distributions/gradle-5.1.1-bin.zip

Add to the location you used in step 4 to the Path system variable:

C:\gradle\gradle-5.1\bin

Save the variables from the File > Save menu if using the Rapid Environment Editor tool.

6. Close existing command prompt and Visual Studio

Close any opened command prompts windows, otherwise your new environment variables won’t be updated. Also close Visual Studio to ensure it gets the new path later.

7. Check the new version

To check if the new version was installed correctly, run this command:

gradle -v
Gradle version for Cordova

Gradle version for Cordova

8. Done!

After the new gradle version has been installed, you should follow these steps:

Re-add the android platform from your app (optional)

cordova platform rm android
cordova platform add android

Clean your project

cordova clean

Check your project’s dependencies

To check if there are any dependency conflicts with your app, open the command prompt at your platforms/android folder and run this command:

gradle app:dependencies

Troubleshooting Cordova build errors

Check out this article if you get build errors after changing your gradle version: Common Apache Cordova build errors with Android

Also, switching gradle versions is another valid method to fix plugin-related errors.

All product names, logos, and brands in this website are property of their respective owners. All company, product and service names used in this website are for identification purposes only. Use of these names, logos, and brands does not imply endorsement.