Define Android Gradle Plugin Version when Building with Cordova

Viewed 2543

When building my Android project with Cordova, I get the following message when opening in Android Studio:

enter image description here

My build.gradle file reflects the plugin discrepancy (although the Gradle version defined is correct):

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

I'd like to know if there is a way to configure Cordova to explicitly define an Android Gradle Plugin version.

2 Answers

The Android Gradle plugin version is hard-coded into the build.gradle template in cordova-android.

If you install the platform off the master branch, you can see it's using a higher version (3.2.0) than the most recent npm release cordova-android@7.1.4 which is configured for 3.0.1.

To install directly from the master branch:

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

If you want to use the very latest version, you could fork the cordova-android repo, pin the version you want in the build.gradle template, then install the plugin directly from your forked repo:

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

I had a problem which required an alternate version of the gradle plugin to be specified in the platforms/android/build.gradle file than what the default Cordova android build generated. So, to fix it, I used created a Cordova hook script to run on after_platform_add which modifies the gradle plugin version.

Details

The Cordova 9 build was generating a build.gradle file with the following gradle plugin version 3.3.0 dependency:

buildscript {
    repositories {
        google()
        jcenter()
    }

    dependencies {
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files

        classpath 'com.android.tools.build:gradle:3.3.0'
    }
}

But, I needed version 3.3.3 of the plugin.

  1. In the Cordova config.xml file I added this hook in my android platform section:
    <platform name="android">
        <hook type="after_platform_add" src="hooks/android/modify-android-gradle-plugin-version.js" />
        ...
    </platform>
  1. Then I added this modify-android-gradle-plugin-version.js hook script. (Please excuse my javascript imperfection, I am not a js expert.)
const fs = require('fs');
const path = require('path');
const deferral = require('q').defer();
const async = require('async');

module.exports = function(ctx) {
    console.log('Running modify-android-gradle-plugin-version.js...');

    const platformRoot = path.join(ctx.opts.projectRoot, 'platforms/android');
    const gradleFiles = [path.join(platformRoot, 'build.gradle')];
    async.each(gradleFiles, function(f, cb) {
        fs.readFile(f, 'utf8', function(err, data) {
            if (err) {
                cb(err);
                return;
            }
            // regex to replace version 3.3.0 with version 3.3.3
            const result = data.replace(/com\.android\.tools\.build:gradle:3\.3\.0/g, 'com.android.tools.build:gradle:3.3.3');
            fs.writeFile(f, result, 'utf8', cb);
        });
    }, function(err) {
        if (err) {
            deferral.reject();
        } else {
            deferral.resolve();
        }

    });
    return deferral.promise;
}
  1. I also had to add the async node module to the devDependencies section of my cordova project's package.json:
  "devDependencies": {
    "async": "3.2.2"
  }
Related