Ensure apks are run through zipalign

This commit is contained in:
bootstraponline
2014-03-14 20:58:53 -04:00
parent 6d0b6c1728
commit 40c8c6b8bc
2 changed files with 52 additions and 19 deletions

View File

@@ -18,6 +18,7 @@ var spawn = require('win-spawn')
, rimraf = require('rimraf')
, Logcat = require('./logcat.js')
, isWindows = helpers.isWindows()
, temp = require('temp')
, helperJarPath = path.resolve(__dirname, 'helpers');
@@ -131,6 +132,10 @@ ADB.prototype.checkAaptPresent = function (cb) {
this.checkSdkBinaryPresent("aapt", cb);
};
ADB.prototype.checkZipAlignPresent = function (cb) {
this.checkSdkBinaryPresent("zipalign", cb);
};
ADB.prototype.exec = function (cmd, opts, cb) {
if (!cb && typeof opts === 'function') {
cb = opts;
@@ -312,17 +317,16 @@ ADB.prototype.insertManifest = function (manifest, srcApk, dstApk, cb) {
], cb);
};
ADB.prototype.signWithDefaultCert = function (apks, cb) {
ADB.prototype.signWithDefaultCert = function (apk, cb) {
var signPath = path.resolve(helperJarPath, 'sign.jar');
var resign = 'java -jar "' + signPath + '" "' + apks.join('" "') +
'" --override';
logger.debug("Resigning apks with: " + resign);
var resign = 'java -jar "' + signPath + '" "' + apk + '" --override';
logger.debug("Resigning apk with: " + resign);
exec(resign, { maxBuffer: 524288 }, function (err, stdout, stderr) {
if (stderr.indexOf("Input is not an existing file") !== -1) {
logger.warn("Could not resign apk(s), got non-existing file error");
return cb(new Error("Could not sign one or more apks. Are you sure " +
"the file paths are correct: " +
JSON.stringify(apks)));
logger.warn("Could not resign apk, got non-existing file error");
return cb(new Error("Could not sign apk. Are you sure " +
"the file path is correct: " +
JSON.stringify(apk)));
}
cb(err);
});
@@ -368,14 +372,39 @@ ADB.prototype.signWithCustomCert = function (apk, cb) {
});
};
ADB.prototype.sign = function (apks, cb) {
if (this.useKeystore) {
async.each(apks, this.signWithCustomCert.bind(this), function (err) {
cb(err);
ADB.prototype.sign = function (apk, cb) {
async.series([
function (cb) {
if (this.useKeystore) {
this.signWithCustomCert(apk, cb);
} else {
this.signWithDefaultCert(apk, cb);
}
}.bind(this),
function (cb) { this.zipAlignApk(apk, cb); }.bind(this),
], cb);
};
ADB.prototype.zipAlignApk = function (apk, cb) {
this.checkZipAlignPresent(function (err) {
if (err) return cb(err);
var alignedApk = temp.path({prefix: 'appium', suffix: '.tmp'});
var alignApk = [this.binaries.zipalign, '-f', '4', '"' + apk + '"', '"' + alignedApk + '"'].join(' ');
logger.debug("zipAlignApk: " + alignApk);
exec(alignApk, { maxBuffer: 524288 }, function (err, stdout, stderr) {
if (err || stderr) {
logger.warn(stderr);
return cb(new Error("zipAlignApk failed. " + err));
}
if (fs.existsSync(apk)) fs.unlinkSync(apk);
fs.renameSync(alignedApk, apk);
cb();
});
} else {
this.signWithDefaultCert(apks, cb);
}
}.bind(this));
};
// returns true when already signed, false otherwise.
@@ -398,8 +427,12 @@ ADB.prototype.checkApkCert = function (apk, pkg, cb) {
return cb(null, false);
}
logger.debug("App already signed.");
cb(null, true);
});
this.zipAlignApk(apk, function (err) {
if (err) return cb(err);
cb(null, true);
});
}.bind(this));
};
ADB.prototype.checkCustomApkCert = function (apk, pkg, cb) {
@@ -1136,7 +1169,7 @@ ADB.prototype.checkAndSignApk = function (apk, pkg, cb) {
this.checkApkCert(apk, pkg, function (err, appSigned) {
if (err) return cb(err);
if (!appSigned) {
this.sign([apk], cb);
this.sign(apk, cb);
} else {
cb();
}

View File

@@ -166,7 +166,7 @@ Selendroid.prototype.checkSelendroidCerts = function (cb) {
this.adb.checkApkCert(apk, this.args.appPackage, function (err, isSigned) {
if (err) return cb(err);
if (isSigned) return onDoneSigning();
this.adb.sign([apk], function (err) {
this.adb.sign(apk, function (err) {
if (err && !alreadyReturned) {
alreadyReturned = true;
return cb(err);