diff --git a/bin/lib/check_reqs.js b/bin/lib/check_reqs.js index 7794594e..40f3ade3 100644 --- a/bin/lib/check_reqs.js +++ b/bin/lib/check_reqs.js @@ -67,7 +67,18 @@ module.exports.get_target = function() { // Returns a promise. Called only by build and clean commands. module.exports.check_ant = function() { return tryCommand('ant -version', 'Failed to run "ant -version", make sure you have ant installed and added to your PATH.'); -} +}; + +// Returns a promise. Called only by build and clean commands. +module.exports.check_gradle = function() { + var sdkDir = process.env['ANDROID_HOME']; + var wrapperDir = path.join(sdkDir, 'tools', 'templates', 'gradle', 'wrapper'); + if (!fs.existsSync(wrapperDir)) { + return Q.reject(new Error('Could not find gradle wrapper within android sdk. Might need to update your Android SDK.\n' + + 'Looked here: ' + wrapperDir)); + } + return Q.when(); +}; // Returns a promise. module.exports.check_java = function() { @@ -126,41 +137,44 @@ module.exports.check_java = function() { // Returns a promise. module.exports.check_android = function() { - var androidCmdPath = forgivingWhichSync('android'); - var adbInPath = !!forgivingWhichSync('adb'); - var hasAndroidHome = !!process.env['ANDROID_HOME'] && fs.existsSync(process.env['ANDROID_HOME']); - if (hasAndroidHome && !androidCmdPath) { - process.env['PATH'] += path.delimiter + path.join(process.env['ANDROID_HOME'], 'tools'); - } - if (androidCmdPath && !hasAndroidHome) { - var parentDir = path.dirname(androidCmdPath); - if (path.basename(parentDir) == 'tools') { - process.env['ANDROID_HOME'] = path.dirname(parentDir); - hasAndroidHome = true; + return Q().then(function() { + var androidCmdPath = forgivingWhichSync('android'); + var adbInPath = !!forgivingWhichSync('adb'); + var hasAndroidHome = !!process.env['ANDROID_HOME'] && fs.existsSync(process.env['ANDROID_HOME']); + if (hasAndroidHome && !androidCmdPath) { + process.env['PATH'] += path.delimiter + path.join(process.env['ANDROID_HOME'], 'tools'); } - } - if (hasAndroidHome && !adbInPath) { - process.env['PATH'] += path.delimiter + path.join(process.env['ANDROID_HOME'], 'platform-tools'); - } - - var valid_target = this.get_target(); - var msg = 'Failed to run "android". Make sure you have the latest Android SDK installed, and that the "android" command (inside the tools/ folder) is added to your PATH.'; - return tryCommand('android list targets', msg) - .then(function(output) { - if (!output.match(valid_target)) { - return Q.reject(new Error('Please install Android target ' + valid_target.split('-')[1] + - ' (the Android newest SDK). Make sure you have the latest Android tools installed as well.' + - ' Run "android" from your command-line to install/update any missing SDKs or tools.')); + if (androidCmdPath && !hasAndroidHome) { + var parentDir = path.dirname(androidCmdPath); + if (path.basename(parentDir) == 'tools') { + process.env['ANDROID_HOME'] = path.dirname(parentDir); + hasAndroidHome = true; + } + } + if (hasAndroidHome && !adbInPath) { + process.env['PATH'] += path.delimiter + path.join(process.env['ANDROID_HOME'], 'platform-tools'); } - }).then(function() { if (!process.env['ANDROID_HOME']) { throw new Error('ANDROID_HOME is not set and "android" command not in your PATH. You must fulfill at least one of these conditions.'); } if (!fs.existsSync(process.env['ANDROID_HOME'])) { throw new Error('ANDROID_HOME is set to a non-existant path: ' + process.env['ANDROID_HOME']); } + // Check that the target sdk level is installed. + return module.exports.check_android_target(module.exports.get_target()); }); -} +}; + +module.exports.check_android_target = function(valid_target) { + var msg = 'Failed to run "android". Make sure you have the latest Android SDK installed, and that the "android" command (inside the tools/ folder) is added to your PATH.'; + return tryCommand('android list targets', msg) + .then(function(output) { + if (!output.match(valid_target)) { + throw new Error('Please install Android target "' + valid_target + '".\n' + + 'Hint: Run "android" from your command-line to open the SDK manager.'); + } + }); +}; // Returns a promise. module.exports.run = function() { diff --git a/bin/lib/create.js b/bin/lib/create.js index cc42f194..13447da7 100755 --- a/bin/lib/create.js +++ b/bin/lib/create.js @@ -83,9 +83,38 @@ function copyJsAndLibrary(projectPath, shared, projectName) { } } -function runAndroidUpdate(projectPath, target_api, shared) { - var targetFrameworkDir = getFrameworkDir(projectPath, shared); - return exec('android update project --subprojects --path "' + projectPath + '" --target ' + target_api + ' --library "' + path.relative(projectPath, targetFrameworkDir) + '"'); +function extractSubProjectPaths(data) { + var ret = {}; + var r = /^\s*android\.library\.reference\.\d+=(.*)(?:\s|$)/mg + var m; + while (m = r.exec(data)) { + ret[m[1]] = 1; + } + return Object.keys(ret); +} + +function writeProjectProperties(projectPath, target_api, shared) { + var dstPath = path.join(projectPath, 'project.properties'); + var templatePath = path.join(ROOT, 'bin', 'templates', 'project', 'project.properties'); + var srcPath = fs.existsSync(dstPath) ? dstPath : templatePath; + var data = fs.readFileSync(srcPath, 'utf8'); + data = data.replace(/^target=.*/m, 'target=' + target_api); + var subProjects = extractSubProjectPaths(data); + subProjects = subProjects.filter(function(p) { + return !(/^CordovaLib$/m.exec(p) || + /[\\\/]cordova-android[\\\/]framework$/m.exec(p) || + /^(\.\.[\\\/])+framework$/m.exec(p) + ); + }); + subProjects.unshift(shared ? path.relative(projectPath, path.join(ROOT, 'framework')) : 'CordovaLib'); + data = data.replace(/^\s*android\.library\.reference\.\d+=.*\n/mg, ''); + if (!/\n$/.exec(data)) { + data += '\n'; + } + for (var i = 0; i < subProjects.length; ++i) { + data += 'android.library.reference.' + (i+1) + '=' + subProjects[i] + '\n'; + } + fs.writeFileSync(dstPath, data); } function copyBuildRules(projectPath) { @@ -251,7 +280,7 @@ exports.createProject = function(project_path, package_name, project_name, proje copyBuildRules(project_path); }); // Link it to local android install. - return runAndroidUpdate(project_path, target_api, use_shared_project); + writeProjectProperties(project_path, target_api); }).then(function() { console.log('Project successfully created.'); }); @@ -274,22 +303,20 @@ function extractProjectNameFromManifest(projectPath) { } // Returns a promise. -exports.updateProject = function(projectPath) { - var version = fs.readFileSync(path.join(ROOT, 'VERSION'), 'utf-8').trim(); +exports.updateProject = function(projectPath, shared) { + var newVersion = fs.readFileSync(path.join(ROOT, 'VERSION'), 'utf-8').trim(); // Check that requirements are met and proper targets are installed return check_reqs.run() .then(function() { var projectName = extractProjectNameFromManifest(projectPath); var target_api = check_reqs.get_target(); - copyJsAndLibrary(projectPath, false, projectName); + copyJsAndLibrary(projectPath, shared, projectName); copyScripts(projectPath); copyBuildRules(projectPath); removeDebuggableFromManifest(projectPath); - return runAndroidUpdate(projectPath, target_api, false) - .then(function() { - console.log('Android project is now at version ' + version); - console.log('If you updated from a pre-3.2.0 version and use an IDE, we now require that you import the "CordovaLib" library project.'); - }); + writeProjectProperties(projectPath, target_api, shared); + console.log('Android project is now at version ' + newVersion); + console.log('If you updated from a pre-3.2.0 version and use an IDE, we now require that you import the "CordovaLib" library project.'); }); }; diff --git a/bin/templates/cordova/lib/build.js b/bin/templates/cordova/lib/build.js index 071990f6..7830bd6e 100644 --- a/bin/templates/cordova/lib/build.js +++ b/bin/templates/cordova/lib/build.js @@ -27,9 +27,9 @@ var shell = require('shelljs'), ROOT = path.join(__dirname, '..', '..'); var check_reqs = require('./check_reqs'); -// Globals -var build_type, - build_method; +var LOCAL_PROPERTIES_TEMPLATE = + '# This file is automatically generated.\n' + + '# Do not modify this file -- YOUR CHANGES WILL BE ERASED!\n'; function find_files(directory, predicate) { if (fs.existsSync(directory)) { @@ -51,22 +51,25 @@ function hasCustomRules() { return fs.existsSync(path.join(ROOT, 'custom_rules.xml')); } -// Copy the gradle wrapper files on each build so that: -// A) We don't require the Android SDK at project creation time, and -// B) So that they are always up-to-date. -function copyGradleWrapper() { - var projectPath = ROOT; - // check_reqs ensures that this is set. - var sdkDir = process.env['ANDROID_HOME']; - var wrapperDir = path.join(sdkDir, 'tools', 'templates', 'gradle', 'wrapper'); - if (process.platform == 'win32') { - shell.cp('-f', path.join(wrapperDir, 'gradlew.bat'), projectPath); - } else { - shell.cp('-f', path.join(wrapperDir, 'gradlew'), projectPath); +function extractProjectNameFromManifest(projectPath) { + var manifestPath = path.join(projectPath, 'AndroidManifest.xml'); + var manifestData = fs.readFileSync(manifestPath, 'utf8'); + var m = /