When building a large app, the default memory of the gradle engine is potentially too low (it's at 2GB per default).
The memory can be increased via the build-extras.gradle
file (also see: https://cordova.apache.org/docs/en/latest/guide/platforms/android/#setting-gradle-properties).
The problem with this approach is, that the build-extras.gradle
resides in the platforms/android
directory and will be
deleted if you choose to remove/reinstall the platform.
To solve that, create a build hook that copies the file from your project root.
In your project root (eg. cordova
), add build-extras.gradle
with the following content:
android.lintOptions = {
checkReleaseBuilds false
abortOnError false
}
android {
dexOptions {
javaMaxHeapSize "8192m"
}
}
Then create a script, in cordova/scripts/
, named androidAfterPrepare.js
, with the following contents:
module.exports = function(context) {
// make sure android platform is part of build
if (context.opts.platforms.indexOf('android') < 0) {
return;
}
var fs = context.requireCordovaModule('fs'),
path = context.requireCordovaModule('path');
var platformRoot = path.join(context.opts.projectRoot, 'platforms/android');
if(fs.existsSync(platformRoot)){
console.log('Add build-extras.gradle');
fs.createReadStream(context.opts.projectRoot + '/build-extras.gradle')
.pipe(fs.createWriteStream(platformRoot + '/build-extras.gradle'));
}
};
And as the last step, add the build hook in your cordova/config.xml
:
<platform name="android">
<!-- This is what you add -->
<hook src="scripts/androidAfterPrepare.js" type="after_prepare" />
…
</platform>
If you also create a cordova/build.json
with the following content:
{
"android": {
"release": {
"keystore": "../keystore.jks",
"storePassword": "",
"alias": "",
"password": "",
"keystoreType": "jks"
}
}
}
And supply the path to your keystore and credentials, you can build a signed APK. This is also explained here: https://cordova.apache.org/docs/en/latest/guide/platforms/android/#using-buildjson