Skip to content

Commit

Permalink
feat(android): added app-bundle support (#11407)
Browse files Browse the repository at this point in the history
Co-authored-by: Gary Mathews <contact@garymathews.com>
Co-authored-by: ssekhri <ssekhri@axway.com>

Fixes TIMOB-26434
  • Loading branch information
jquick-axway committed Jan 30, 2020
1 parent dbe3c58 commit 5d93fea
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
11 changes: 11 additions & 0 deletions android/cli/commands/_build.js
Expand Up @@ -3768,9 +3768,20 @@ AndroidBuilder.prototype.buildAppProject = async function buildAppProject() {
const gradlew = new GradleWrapper(this.buildDir);
gradlew.logger = this.logger;
if (this.allowDebugging) {
// Build a debug version of the APK. (Native code can be debugged via Android Studio.)
await gradlew.assembleDebug('app');
} else {
// Build a release version of the APK.
await gradlew.assembleRelease('app');

// Create an "*.aab" app-bundle file of the app.
// Note: This is a Google Play publishing format. App-bundles cannot be ran on Android devices.
// Google's server will generate multiple APKs from this split by architecture and image density.
await gradlew.bundleRelease('app');

// Set path to the app-bundle file that was built up above.
// Our "package.js" event hook will later copy it to the developer's chosen destination directory.
this.aabFile = path.join(this.buildDir, 'app', 'build', 'outputs', 'bundle', 'release', 'app.aab');
}
};

Expand Down
18 changes: 17 additions & 1 deletion android/cli/hooks/package.js
Expand Up @@ -19,29 +19,45 @@ exports.init = function (logger, config, cli) {
cli.on('build.post.compile', {
priority: 10000,
post: function (builder, finished) {
// Do not continue if this is not a "production" build.
if (builder.target !== 'dist-playstore') {
return finished();
}

// Fetch a path to the built APK file.
let sourceFilePath = builder.apkFile;
if (!sourceFilePath || !fs.existsSync(sourceFilePath)) {
logger.error(__('No APK file to deploy, skipping'));
return finished();
}

// Do not continue if developer did not provide a destination directory.
const outputDir = builder.outputDir;
if (!outputDir) {
logger.error(__('Packaging output directory path cannot be empty.'));
return finished();
}

// Copy built app file(s) to destination directory.
if (outputDir !== path.dirname(sourceFilePath)) {
// Create the destination directory.
fs.ensureDirSync(outputDir);
const outputFilePath = path.join(outputDir, builder.tiapp.name + '.apk');

// Copy built APK to destination.
let outputFilePath = path.join(outputDir, builder.tiapp.name + '.apk');
if (fs.existsSync(outputFilePath)) {
fs.unlinkSync(outputFilePath);
}
appc.fs.copyFileSync(sourceFilePath, outputFilePath, { logger: logger.debug });

// Copy built app-bundle to destination, if available.
if (builder.aabFile && fs.existsSync(builder.aabFile)) {
outputFilePath = path.join(outputDir, builder.tiapp.name + '.aab');
if (fs.existsSync(outputFilePath)) {
fs.unlinkSync(outputFilePath);
}
appc.fs.copyFileSync(builder.aabFile, outputFilePath, { logger: logger.debug });
}
}

logger.info(__('Packaging complete'));
Expand Down
15 changes: 15 additions & 0 deletions android/cli/lib/gradle-wrapper.js
Expand Up @@ -112,6 +112,21 @@ class GradleWrapper {
await this.run(`${subprojectName}assembleRelease --console plain --warning-mode all`);
}

/**
* Builds a release version of an application project to an "*.aab" app-bundle file.
* This is a Google Play publishing format, when uploaded, Google's servers will then generate multiple
* APK files splits by CPU architecture and image densities. (App-bundle files cannot be ran on a device.)
* @param {String} [subprojectName]
* Optional name of the gradle subproject to generate an AAB app-bundle for, such as 'app'.
* Will build the app and all of its dependency projects if not done already.
*
* Can be null/undefined, in which case all application projects will be built as app-bundles.
*/
async bundleRelease(subprojectName) {
subprojectName = isNonEmptyString(subprojectName) ? `:${subprojectName}:` : '';
await this.run(`${subprojectName}bundleRelease --console plain`);
}

/**
* Executes the gradle script's "publishing" task for the project.
*
Expand Down

0 comments on commit 5d93fea

Please sign in to comment.