CB-7114 Android: add support of min/max/target SDK to config.xml github: close #56
Project: http://git-wip-us.apache.org/repos/asf/cordova-lib/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-lib/commit/e86fab8a Tree: http://git-wip-us.apache.org/repos/asf/cordova-lib/tree/e86fab8a Diff: http://git-wip-us.apache.org/repos/asf/cordova-lib/diff/e86fab8a Branch: refs/heads/master Commit: e86fab8ad0934a77620888301801d1d415392812 Parents: 867a01e Author: sgrebnov <v-seg...@microsoft.com> Authored: Thu Jul 10 12:09:03 2014 +0400 Committer: Anis Kadri <a...@apache.org> Committed: Fri Sep 5 11:12:19 2014 -0700 ---------------------------------------------------------------------- cordova-lib/spec-cordova/ConfigParser.spec.js | 5 +++++ cordova-lib/spec-cordova/test-config.xml | 3 ++- cordova-lib/src/configparser/ConfigParser.js | 6 +++++- cordova-lib/src/cordova/metadata/android_parser.js | 15 +++++++++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/e86fab8a/cordova-lib/spec-cordova/ConfigParser.spec.js ---------------------------------------------------------------------- diff --git a/cordova-lib/spec-cordova/ConfigParser.spec.js b/cordova-lib/spec-cordova/ConfigParser.spec.js index 9c8c8e6..7028034 100644 --- a/cordova-lib/spec-cordova/ConfigParser.spec.js +++ b/cordova-lib/spec-cordova/ConfigParser.spec.js @@ -80,6 +80,11 @@ describe('config.xml parser', function () { expect(cfg.getPreference('zimzooo!')).toEqual(undefined); }); }); + describe('platform specific preference', function() { + it('should get value of existing platform specific preference', function() { + expect(cfg.getPreference('android-minSdkVersion', 'android')).toEqual('10'); + }); + }); describe('feature',function(){ it('should read feature id list', function() { var expectedList = [ http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/e86fab8a/cordova-lib/spec-cordova/test-config.xml ---------------------------------------------------------------------- diff --git a/cordova-lib/spec-cordova/test-config.xml b/cordova-lib/spec-cordova/test-config.xml index 3308ae0..27838b7 100644 --- a/cordova-lib/spec-cordova/test-config.xml +++ b/cordova-lib/spec-cordova/test-config.xml @@ -22,6 +22,7 @@ <icon id="logo" src="logo.png" width="255" height="255" /> <platform name="android"> <icon src="logo-android.png" width="255" height="255" density="mdpi" /> + <preference name="android-minSdkVersion" value="10" /> </platform> <!-- Features --> @@ -45,4 +46,4 @@ <feature name="A simple feature"> <param name="id" value="org.apache.cordova.justafeature" /> </feature> -</widget> \ No newline at end of file +</widget> http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/e86fab8a/cordova-lib/src/configparser/ConfigParser.js ---------------------------------------------------------------------- diff --git a/cordova-lib/src/configparser/ConfigParser.js b/cordova-lib/src/configparser/ConfigParser.js index 75f798b..da6376e 100644 --- a/cordova-lib/src/configparser/ConfigParser.js +++ b/cordova-lib/src/configparser/ConfigParser.js @@ -117,8 +117,12 @@ ConfigParser.prototype = { author: function() { return getNodeTextSafe(this.doc.find('author')); }, - getPreference: function(name) { + getPreference: function(name, platform) { var preferences = this.doc.findall('preference'); + if (platform) { // include platform specific preferences + preferences = preferences.concat( + this.doc.findall('platform[@name=\'' + platform + '\']/preference')); + } var ret = null; preferences.forEach(function (preference) { // Take the last one that matches. http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/e86fab8a/cordova-lib/src/cordova/metadata/android_parser.js ---------------------------------------------------------------------- diff --git a/cordova-lib/src/cordova/metadata/android_parser.js b/cordova-lib/src/cordova/metadata/android_parser.js index 12a6d1f..9f338dd 100644 --- a/cordova-lib/src/cordova/metadata/android_parser.js +++ b/cordova-lib/src/cordova/metadata/android_parser.js @@ -23,6 +23,7 @@ var fs = require('fs'), path = require('path'), + et = require('elementtree'), xml = require('../../util/xml-helpers'), util = require('../util'), events = require('../../events'), @@ -261,6 +262,20 @@ module.exports.prototype = { delete act.attrib["android:launchMode"]; // use Android default value (standard) } + // Set min/max/target SDK version + //<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="19" ... /> + var usesSdk = manifest.getroot().find('./uses-sdk'); + ['minSdkVersion', 'maxSdkVersion', 'targetSdkVersion'].forEach(function(sdkPrefName) { + var sdkPrefValue = config.getPreference('android-' + sdkPrefName, 'android'); + if (!sdkPrefValue) return; + + if (!usesSdk) { // if there is no required uses-sdk element, we should create it first + usesSdk = new et.Element('uses-sdk'); + manifest.getroot().append(usesSdk); + } + usesSdk.attrib['android:' + sdkPrefName] = sdkPrefValue; + }); + // Write out AndroidManifest.xml fs.writeFileSync(this.manifest, manifest.write({indent: 4}), 'utf-8');