Define Android Gradle Plugin Version when Building with Cordova
Asked Answered
W

3

6

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.

Whitewall answered 18/12, 2018 at 19:16 Comment(4)
I can't make out what exactly you're asking, but this means your grade version is com.android.tools.build:gradle:3.0.0. The dialog is only asking to update it if you want but if you don't, just click ignore or close it.Sirree
@JoeyDalu It is asking me because cordova specified version 3.0.0 when it built the android platform. I want it to specify 3.2.1 so I don't get this warning.Whitewall
Alright, click on the update button and it should update it for you. If it doesn't work, you can always update it in the settings.gradle file at the root of your projectSirree
@JoeyDalu I want to configure Cordova to explicitly define an Android Gradle Plugin version as stated in the original post.Whitewall
Z
3

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 [email protected] 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
Zwick answered 19/12, 2018 at 11:3 Comment(2)
Thanks. I'll try it out and give you credit when I get a chance to try it out.Whitewall
Works, but just a note, even though I have verified through Project Structure that my Gradl and plugin Version are correct, it will still flash that update message the first time I open the newly created platform project.Whitewall
S
3

The AGP version or AndroidGooglePlugin can be specified in config.xml through AndroidGradlePluginVersion preference. i.e:

<preference name="AndroidGradlePluginVersion" value="7.2.2" />
Shirleenshirlene answered 28/6, 2023 at 14:37 Comment(0)
T
2

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"
  }
Tuchman answered 1/12, 2021 at 16:34 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.