diff --git a/bin/templates/cordova/lib/android_sdk.js b/bin/templates/cordova/lib/android_sdk.js index 58d301ef..a1a806a6 100755 --- a/bin/templates/cordova/lib/android_sdk.js +++ b/bin/templates/cordova/lib/android_sdk.js @@ -1,5 +1,3 @@ - - /* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file @@ -68,65 +66,38 @@ module.exports.version_string_to_api_level = { '7.1.1': 25 }; -module.exports.list_targets_with_android = function() { - return superspawn.spawn('android', ['list', 'targets']) - .then(function(stdout) { - var target_out = stdout.split('\n'); - var targets = []; - for (var i = target_out.length - 1; i >= 0; i--) { - if(target_out[i].match(/id:/)) { - targets.push(target_out[i].match(/"(.+)"/)[1]); - } +function parse_targets(output) { + var target_out = output.split('\n'); + var targets = []; + for (var i = target_out.length - 1; i >= 0; i--) { + if(target_out[i].match(/id:/)) { // if "id:" is in the line... + targets.push(target_out[i].match(/"(.+)"/)[1]); //.. match whatever is in quotes. } - return targets; - }); + } + return targets; +} + +module.exports.list_targets_with_android = function() { + return superspawn.spawn('android', ['list', 'target']) + .then(parse_targets); }; -module.exports.list_targets_with_sdkmanager = function() { - return superspawn.spawn('sdkmanager', ['--list']) - .then(function(stdout) { - var parsing_installed_packages = false; - var lines = stdout.split('\n'); - var targets = []; - for (var i = 0, l = lines.length; i < l; i++) { - var line = lines[i]; - if (line.match(/Installed packages/)) { - parsing_installed_packages = true; - } else if (line.match(/Available Packages/) || line.match(/Available Updates/)) { - // we are done working through installed packages, exit - break; - } - if (parsing_installed_packages) { - // Match stock android platform - if (line.match(/platforms;android-\d+/)) { - targets.push(line.match(/(android-\d+)/)[1]); - } - // Match Google APIs - if (line.match(/addon-google_apis-google-\d+/)) { - var description = lines[i + 1]; - // munge description to match output from old android sdk tooling - var api_level = description.match(/Android (\d+)/); //[1]; - if (api_level) { - targets.push('Google Inc.:Google APIs:' + api_level[1]); - } - } - // TODO: match anything else? - } - } - return targets; - }); +module.exports.list_targets_with_avdmanager = function() { + return superspawn.spawn('avdmanager', ['list', 'target']) + .then(parse_targets); }; module.exports.list_targets = function() { - return module.exports.list_targets_with_android() + return module.exports.list_targets_with_avdmanager() .catch(function(err) { - // there's a chance `android` no longer works. - // lets see if `sdkmanager` is available and we can figure it out - var avail_regex = /"?android"? command is no longer available/; - if (err.code && ((err.stdout && err.stdout.match(avail_regex)) || (err.stderr && err.stderr.match(avail_regex)))) { - return module.exports.list_targets_with_sdkmanager(); + // If there's an error, like avdmanager could not be found, we can try + // as a last resort, to run `android`, in case this is a super old + // SDK installation. + if (err && (err.code == 'ENOENT' || (err.stderr && err.stderr.match(/not recognized/)))) { + return module.exports.list_targets_with_android(); } else throw err; - }).then(function(targets) { + }) + .then(function(targets) { if (targets.length === 0) { return Q.reject(new Error('No android targets (SDKs) installed!')); }