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 properly upgrade or revert your Gradle version for Windows and MacOS.
1: Stop gradle daemon
Run this command to stop any active gradle services, if any:
2. Delete gradle cache
Locate and delete the gradle cache folder:
Unix: %USER_HOME%\.gradle\caches\ Windows: C:\Users\Andres\.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.
Gradle and Gradle Plugin Version Support Update
4. Unzip the downloaded file
Place the contents of the downloaded ZIP in any location such as C:\gradle\gradle-5.1.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:
Add to the location you used in step 4 to the Path system variable:
Save the variables from the File > Save menu if using the Rapid Environment Editor tool.
6. Close opened command prompts and IDEs
Close any opened command prompts windows, otherwise your new environment variables won’t be updated. Also restart NetBeans, Visual Studio or whaever IDE you’re using.
7. Check the new version
To check if the new version was installed correctly, run this command:
8. Re-add the android platform from your app
cordova platform rm android cordova platform add android
9. Clean your project
10. 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:
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.