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.
- 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>
- 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;
}
- 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"
}
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. – Sirreesettings.gradle
file at the root of your project – Sirree