Skip to content

Commit

Permalink
fix(android): can't set versionCode in manifest as of 9.0.0 (#11416)
Browse files Browse the repository at this point in the history
- Regression caused by changing build system from ant to gradle.
- Now reads versionCode from "AndroidManifest.xml" and writes it to generated "build.gradle" file.

Co-authored-by: Lokesh Choudhary <lchoudhary@axway.com>
  • Loading branch information
jquick-axway and lokeshchdhry committed Jan 17, 2020
1 parent 70c16f7 commit a69f6b6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
18 changes: 17 additions & 1 deletion android/cli/commands/_build.js
Original file line number Diff line number Diff line change
Expand Up @@ -2455,6 +2455,21 @@ AndroidBuilder.prototype.generateAppProject = async function generateAppProject(
dexerHook('', [ '', '' ], {}, resolve);
});

// Acquire the app's version integer code and name to be written to "build.gradle" later.
let versionCode = '1';
let versionName = this.tiapp.version ? this.tiapp.version : '1';
if (this.customAndroidManifest) {
const versionInfo = this.customAndroidManifest.getAppVersionInfo();
if (versionInfo) {
if (versionInfo.versionCode) {
versionCode = versionInfo.versionCode;
}
if (versionInfo.versionName) {
versionName = versionInfo.versionName;
}
}
}

// Generate a "build.gradle" file for this project from the SDK's "app.build.gradle" EJS template.
// Note: Google does not support setting "maxSdkVersion" via gradle script.
let buildGradleContent = await fs.readFile(path.join(this.templatesDir, 'app.build.gradle'));
Expand All @@ -2463,7 +2478,8 @@ AndroidBuilder.prototype.generateAppProject = async function generateAppProject(
compileSdkVersion: this.compileSdkVersion,
minSdkVersion: this.minSDK,
targetSdkVersion: this.targetSDK,
versionName: this.tiapp.version ? this.tiapp.version : '1',
versionCode: versionCode,
versionName: versionName,
libFilePaths: this.libFilePaths,
libProjectNames: this.libProjectNames,
libDependencyStrings: this.libDependencyStrings,
Expand Down
21 changes: 21 additions & 0 deletions android/cli/lib/android-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,27 @@ class AndroidManifest {
manifestElement.setAttribute('package', name);
}

/**
* Fetches the "versionCode" and "versionName" attributes from the <manifest/> element.
* @return {{versionCode: {String}, versionName: {String}}}
* Returns a dictionary providing the "versionCode" and "versionName" attributes read from <manifest/> element.
*
* Returns null if no XML content has been loaded;
*/
getAppVersionInfo() {
let versionInfo = null;
if (!this.isEmpty()) {
const element = this._xmlDomDocument.documentElement;
if (element.tagName === 'manifest') {
versionInfo = {
versionCode: element.getAttribute('android:versionCode'),
versionName: element.getAttribute('android:versionName')
};
}
}
return versionInfo;
}

/**
* Fetches the manifest's <uses-sdk/> element's information.
* @return {{minSdkVersion: {String}, maxSdkVersion: {String}, targetSdkVersion: {String}}}
Expand Down
1 change: 1 addition & 0 deletions android/templates/build/app.build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ android {
applicationId '<%- applicationId %>'
minSdkVersion <%- minSdkVersion %>
targetSdkVersion <%- targetSdkVersion %>
versionCode <%- versionCode %>
versionName '<%- versionName %>'
manifestPlaceholders = project.ext.tiManifestPlaceholders
manifestPlaceholders.put('localApplicationId', applicationId) // Legacy placeholder old AARs sometimes use.
Expand Down

0 comments on commit a69f6b6

Please sign in to comment.