From bf57aa1df061b3fcd429bb8d673de02bcb26f872 Mon Sep 17 00:00:00 2001 From: Vladimir Kotikov Date: Wed, 26 Aug 2015 16:09:27 +0300 Subject: [PATCH] CB-9557 Fixes apk install failure when switching from debug to release build --- bin/templates/cordova/lib/appinfo.js | 11 +++++-- bin/templates/cordova/lib/device.js | 13 ++++++-- bin/templates/cordova/lib/emulator.js | 46 +++++++++++++++------------ 3 files changed, 45 insertions(+), 25 deletions(-) diff --git a/bin/templates/cordova/lib/appinfo.js b/bin/templates/cordova/lib/appinfo.js index 080c2ba8..e37b89ba 100644 --- a/bin/templates/cordova/lib/appinfo.js +++ b/bin/templates/cordova/lib/appinfo.js @@ -33,9 +33,16 @@ function readAppInfoFromManifest() { var activityName = /\bandroid:name\s*=\s*"(.+?)"/.exec(activityTag); if (!activityName) throw new Error('Could not find android:name within ' + manifestPath); - return packageName[1] + '/.' + activityName[1]; + return (cachedAppInfo = { + packageName: packageName[1], + activityName: packageName[1] + '/.' + activityName[1] + }); } exports.getActivityName = function() { - return (cachedAppInfo = cachedAppInfo || readAppInfoFromManifest()); + return cachedAppInfo ? cachedAppInfo.activityName : readAppInfoFromManifest().activityName; +}; + +exports.getPackageName = function() { + return cachedAppInfo ? cachedAppInfo.packageName : readAppInfoFromManifest().packageName; }; diff --git a/bin/templates/cordova/lib/device.js b/bin/templates/cordova/lib/device.js index c13fdc40..560f26db 100644 --- a/bin/templates/cordova/lib/device.js +++ b/bin/templates/cordova/lib/device.js @@ -96,10 +96,17 @@ module.exports.install = function(target, buildResults) { }).then(function(resolvedTarget) { var apk_path = build.findBestApkForArchitecture(buildResults, resolvedTarget.arch); var launchName = appinfo.getActivityName(); + var pkgName = appinfo.getPackageName(); console.log('Using apk: ' + apk_path); - console.log('Installing app on device...'); - var cmd = 'adb -s ' + resolvedTarget.target + ' install -r "' + apk_path + '"'; - return exec(cmd, os.tmpdir()) + console.log('Uninstalling ' + pkgName + ' from device...'); + // This promise is always resolved, even if 'adb uninstall' fails to uninstall app + // or the app doesn't installed at all, so no error catching needed. + return exec('adb -s ' + resolvedTarget.target + ' uninstall ' + pkgName, os.tmpdir()) + .then(function() { + console.log('Installing app on device...'); + var cmd = 'adb -s ' + resolvedTarget.target + ' install -r -d "' + apk_path + '"'; + return exec(cmd, os.tmpdir()); + }) .then(function(output) { if (output.match(/Failure/)) return Q.reject('ERROR: Failed to install apk to device: ' + output); diff --git a/bin/templates/cordova/lib/emulator.js b/bin/templates/cordova/lib/emulator.js index 8fbdbc1f..a27d0d3e 100644 --- a/bin/templates/cordova/lib/emulator.js +++ b/bin/templates/cordova/lib/emulator.js @@ -326,31 +326,37 @@ module.exports.install = function(givenTarget, buildResults) { // install the app }).then(function () { + var pkgName = appinfo.getPackageName(); + console.log('Uninstalling ' + pkgName + ' from emulator...'); + // This promise is always resolved, even if 'adb uninstall' fails to uninstall app + // or the app doesn't installed at all, so no error catching needed. + return exec('adb -s ' + target.target + ' uninstall ' + pkgName, os.tmpdir()) + .then(function() { - var apk_path = build.findBestApkForArchitecture(buildResults, target.arch); - var execOptions = { - timeout: INSTALL_COMMAND_TIMEOUT, // in milliseconds - killSignal: EXEC_KILL_SIGNAL - }; + var apk_path = build.findBestApkForArchitecture(buildResults, target.arch); + var execOptions = { + timeout: INSTALL_COMMAND_TIMEOUT, // in milliseconds + killSignal: EXEC_KILL_SIGNAL + }; - console.log('Installing app on emulator...'); - console.log('Using apk: ' + apk_path); + console.log('Installing app on emulator...'); + console.log('Using apk: ' + apk_path); - var retriedInstall = retry.retryPromise( - NUM_INSTALL_RETRIES, - exec, 'adb -s ' + target.target + ' install -r -d "' + apk_path + '"', os.tmpdir(), execOptions - ); + var retriedInstall = retry.retryPromise( + NUM_INSTALL_RETRIES, + exec, 'adb -s ' + target.target + ' install -r -d "' + apk_path + '"', os.tmpdir(), execOptions + ); - return retriedInstall.then(function (output) { - if (output.match(/Failure/)) { - return Q.reject('Failed to install apk to emulator: ' + output); - } else { - console.log('INSTALL SUCCESS'); - } - }, function (err) { - return Q.reject('Failed to install apk to emulator: ' + err); + return retriedInstall.then(function (output) { + if (output.match(/Failure/)) { + return Q.reject('Failed to install apk to emulator: ' + output); + } else { + console.log('INSTALL SUCCESS'); + } + }, function (err) { + return Q.reject('Failed to install apk to emulator: ' + err); + }); }); - // unlock screen }).then(function () {