Skip to content

Commit 7cfb33d

Browse files
committed
CB-7980 Add --minSdkVersion and --versionCode flags to cordova/build command
These are also exposed via environment variables: ANDROID_VERSION_CODE, ANDROID_MIN_SDK_VERSION This also fixes build.gradle modifying the value set by ANDROID_VERSION_CODE when multi-apk is enabled (override should never be modified)
1 parent 9224ab1 commit 7cfb33d

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

bin/templates/cordova/lib/build.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -306,15 +306,21 @@ function parseOpts(options, resolvedTarget) {
306306
// Iterate through command line options
307307
for (var i=0; options && (i < options.length); ++i) {
308308
if (/^--/.exec(options[i])) {
309-
var option = options[i].substring(2);
310-
switch(option) {
309+
var keyValue = options[i].substring(2).split('=');
310+
var flagName = keyValue[0];
311+
var flagValue = keyValue[1];
312+
if ((flagName == 'versionCode' || flagName == 'minSdkVersion') && !flagValue) {
313+
flagValue = options[i + 1];
314+
++i;
315+
}
316+
switch(flagName) {
311317
case 'debug':
312318
case 'release':
313-
ret.buildType = option;
319+
ret.buildType = flagName;
314320
break;
315321
case 'ant':
316322
case 'gradle':
317-
ret.buildMethod = option;
323+
ret.buildMethod = flagName;
318324
break;
319325
case 'device':
320326
case 'emulator':
@@ -324,8 +330,14 @@ function parseOpts(options, resolvedTarget) {
324330
case 'nobuild' :
325331
ret.buildMethod = 'none';
326332
break;
333+
case 'versionCode':
334+
process.env['ANDROID_VERSION_CODE'] = flagValue;
335+
break;
336+
case 'minSdkVersion':
337+
process.env['ANDROID_MIN_SDK_VERSION'] = flagValue;
338+
break;
327339
default :
328-
console.warn('Build option \'' + options[i] + '\' not recognized (ignoring).');
340+
console.warn('Build option --\'' + flagName + '\' not recognized (ignoring).');
329341
}
330342
} else {
331343
console.warn('Build option \'' + options[i] + '\' not recognized (ignoring).');
@@ -445,12 +457,14 @@ module.exports.findBestApkForArchitecture = function(buildResults, arch) {
445457
};
446458

447459
module.exports.help = function() {
448-
console.log('Usage: ' + path.relative(process.cwd(), path.join(ROOT, 'cordova', 'build')) + ' [build_type]');
449-
console.log('Build Types : ');
450-
console.log(' \'--debug\': Default build, will build project in debug mode');
460+
console.log('Usage: ' + path.relative(process.cwd(), path.join(ROOT, 'cordova', 'build')) + ' [flags]');
461+
console.log('Flags:');
462+
console.log(' \'--debug\': will build project in debug mode (default)');
451463
console.log(' \'--release\': will build project for release');
452-
console.log(' \'--ant\': Default build, will build project with ant');
464+
console.log(' \'--ant\': will build project with ant (default)');
453465
console.log(' \'--gradle\': will build project with gradle');
454-
console.log(' \'--nobuild\': will skip build process (can be used with run command)');
466+
console.log(' \'--nobuild\': will skip build process (useful when using run command)');
467+
console.log(' \'--versionCode=#\': Override versionCode for this build. Useful for uploading multiple APKs. Requires --gradle.');
468+
console.log(' \'--minSdkVersion=#\': Override minSdkVersion for this build. Useful for uploading multiple APKs. Requires --gradle.');
455469
process.exit(0);
456470
};

bin/templates/project/build.gradle

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ android {
7474

7575
defaultConfig {
7676
versionCode Integer.parseInt(System.env.ANDROID_VERSION_CODE ?: ("" + getIntFromManifest("versionCode") + "0"))
77+
if (System.env.ANDROID_MIN_SDK_VERSION) {
78+
minSdkVersion Integer.parseInt(System.env.ANDROID_MIN_SDK_VERSION)
79+
}
7780
}
7881

7982
compileSdkVersion cordova.cordovaSdkVersion
@@ -82,13 +85,13 @@ android {
8285
if (multiarch || System.env.BUILD_MULTIPLE_APKS) {
8386
productFlavors {
8487
armv7 {
85-
versionCode defaultConfig.versionCode + 2
88+
versionCode System.env.ANDROID_VERSION_CODE ?: defaultConfig.versionCode + 2
8689
ndk {
8790
abiFilters "armeabi-v7a", ""
8891
}
8992
}
9093
x86 {
91-
versionCode defaultConfig.versionCode + 4
94+
versionCode System.env.ANDROID_VERSION_CODE ?: defaultConfig.versionCode + 4
9295
ndk {
9396
abiFilters "x86", ""
9497
}
@@ -99,8 +102,16 @@ android {
99102
}
100103
}
101104
}
102-
} else if (getIntFromManifest("minSdkVersion") >= 20) {
105+
} else if (!System.env.ANDROID_VERSION_CODE) {
106+
def minSdkVersion = Integer.parseInt(System.env.ANDROID_MIN_SDK_VERSION ?: "" + getIntFromManifest("minSdkVersion"))
107+
// Vary versionCode by the two most common API levels:
108+
// 14 is ICS, which is the lowest API level for many apps.
109+
// 20 is Lollipop, which is the lowest API level for the updatable system webview.
110+
if (minSdkVersion >= 20) {
103111
defaultConfig.versionCode += 9
112+
} else if (minSdkVersion >= 14) {
113+
defaultConfig.versionCode += 8
114+
}
104115
}
105116

106117
compileOptions {

0 commit comments

Comments
 (0)