Skip to content

Instantly share code, notes, and snippets.

@lucianomlima
Created May 5, 2016 15:36
Show Gist options
  • Save lucianomlima/ac7a0fbc34f56002f3a423979001e5ea to your computer and use it in GitHub Desktop.
Save lucianomlima/ac7a0fbc34f56002f3a423979001e5ea to your computer and use it in GitHub Desktop.
Cordova hook script to make a copy of an APK file after build.
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'),
ConfigParser = context.requireCordovaModule('cordova-lib').configparser;
var configFileLocation = path.join(context.opts.projectRoot, 'config.xml');
var config = new ConfigParser(configFileLocation);
var platformRoot = path.join(context.opts.projectRoot, 'platforms/android');
var buildType = 'debug';
var buildOptions = context.opts.options || {};
var apkFilename = String(config.name()).replace(/ /g, '_')+'_v'+config.version()+'-'+config.android_versionCode()+'.apk';
if (buildOptions.hasOwnProperty('release')) {
buildType = 'release';
apkFilename = String(config.name()).replace(/ /g, '_')+'_release.apk';
};
var apkFileLocation = path.join(platformRoot, 'build/outputs/apk/android-'+buildType+'.apk');
var apkDestination = path.join(process.env.HOME, 'Downloads', apkFilename);
fs.createReadStream(apkFileLocation).pipe(fs.createWriteStream(apkDestination));
console.log('Copy to', apkDestination);
return true;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment