Skip to content

Instantly share code, notes, and snippets.

@bummzack
Last active March 29, 2018 21:29
Show Gist options
  • Save bummzack/0069867297d590ee128dadbb54e88e82 to your computer and use it in GitHub Desktop.
Save bummzack/0069867297d590ee128dadbb54e88e82 to your computer and use it in GitHub Desktop.
Fix build issues when creating release builds with corber (cordova)

Memory issues

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment