Overview

Android App Bundle is a new format for you to build and package your app. A build tool packages a traditional app into an app bundle based on the ABI architecture, screen density, and language. When a user downloads your app, HUAWEI AppGallery provides a sub app bundle that adapts only to the user's device type, thereby reducing network data and device storage space, with which the same service functions can be provided.

You only need to make minor modifications to the project to convert a traditional app to an app bundle.

What You Will Learn

Hardware Requirements

Software Requirements

Creating an Android Project

  1. Open Android Studio and go to File > New > New Project.
  2. On the Phone and Tablet tab page, click Empty Activity and Next.
  3. Configure project attributes, such as the project name, package name, and API level.
  4. Click Finish.
    In this example, the default project configuration is used. The bundle development feature can be demonstrated without any code modification.

Building Your App

In this section, you need to build a complete APK, app bundle, and custom app bundle.

Building a Complete APK

This is a traditional build mode. A complete APK contains all resources and code of your app.

Building a Bundle

Open Android Studio and go to Build > Build Bunlde(s) / APK(s) > Build Bundle(s).

The app-debug.aab file is generated in build > outputs > bundle > debug.

Building a Custom App Bundle

By default, Android Studio automatically splits your app based on the ABI architecture, screen density, and language. If you want to customize the splitting dimension, add a control switch to android {} in the app/build.gradle file.

bundle { language { enableSplit = true } density { enableSplit = true } abi { enableSplit = true } }

If enableSplit under language is set to false, Android Studio packages all language resources to base.apk during building.

You cannot install and test the app bundles locally through the .aab files generated, but you need to use the Bundletool to convert the .aab file into an APK set. The BundleTool is an open-source command line tool of Google. You can click the button below to download the BundleTool. You can upload the .aab file to HUAWEI AppGallery, and HUAWEI AppGallery will automatically call the BundleTool to convert the file. This section describes how to convert the .aab file locally using the BundleTool.

Download BundleTool

Converting an .aab File to APKs

java -jar bundletool-all-0.10.2.jar build-apks --bundle=app-debug.aab --output=aab.apks

In the command, bundle indicates the path of the .aab file and output indicates the path of the generated .apks file.
Change the file name extension .apks to .zip and decompress the .zip file to obtain the .apk files.

Installing a Bundle

Find the master package and the screen density package, language package, and ABI architecture package that adapt to your mobile phone, and install and test the bundle. In this example, the following packages are used: base-master.apk (master package), base-xxxhdpi.apk (screen density package), and base-zh.apk (language package), but no SO file (ABI architecture package) is available. Install the packages on the mobile phone to verify the functions.

adb install-multiple .\outputs\bundle\debug\splits\base-master.apk .\outputs\bundle\debug\splits\base-xxxhdpi.apk .\outputs\bundle\debug\splits\base-zh.apk

Converting an .aab File to a Complete APK

Android versions earlier than 5.0 do not support app bundles. In some scenarios, a complete APK is required so that you need to call the BundleTool to convert the .aab file to a complete APK. The method is as follows:

java -jar bundletool-all-0.10.2.jar build-apks --bundle= app-debug.aab --output=aab-un.apks --mode=universal

Change the file name extension .aab to .zip and then decompress the .zip package. The .apk file is generated as follows.

Installing a Complete APK

adb install universal.apk

Congratulations! You have completed the first project of App Bundle and learned how to:

Download the demo source code used in this codelab from the following address:

Download source code

Code copied