Android App Bundle

Deepanshu
MindOrks
5 min readAug 20, 2018

--

In Google I/O 2018, a new publishing format has been introduced for Android applications called Android App Bundle. It is a new upload format that includes all your app’s compiled code and resources, but defers APK generation and signing to Google Play. Traditionally, Android apps are distributed using a special file called an Android Package(.apk).

Why should we use this new format?

  • Dynamic Delivery : Google Play’s new app serving model, called Dynamic Delivery, uses your app bundle to generate and serve optimized APKs for each user’s device configuration, so they download only the code and resources they need to run your app. For example, you don’t need other languages strings if you have set English as your default language.
  • No need to manually manage multiple APKs : You no longer have to build, sign, and manage multiple APKs to support different devices, and users get smaller, more optimized downloads. For example, now you don’t have to create multiple APKs for devices with different screen resolutions.
  • Dynamic Feature Module : These modules contain features and assets that you can decide not to include when users first download and install your app. Using the Play Core Library, your app can later request to download those modules as dynamic feature APKs. For example, video calling feature and camera filters can be downloaded later on demand.
  • Reduced APK size : Using Split APK mechanism(explained later), Google Play can break up a large app into smaller, discrete packages that are installed on a user’s device as required. On average, apps published with app bundles are 20% smaller in size.

How to build an app bundle?

You can easily build your app bundle using Android Studio(3.2 Canary 14+) or using command line interface. The generated app bundle will be stored at app/build/outputs/bundle/buildVariant/bundle.aab.

  1. Android Studio : Go to Build > Build Bundle(s)/APK(s) and select Build Bundle(s).
  2. Console : ./gradlew bundle

Dynamic Delivery with Split APKs

A fundamental component of Dynamic Delivery is the split APK mechanism available on Android 5.0 (API level 21) and higher. Split APKs are very similar to regular APKs — they include compiled DEX bytecode, resources, and an Android manifest. However, the Android platform is able to treat multiple installed split APKs as a single app.

On the left: a simple app that includes a base APK (B) and some configuration APKs ©. On the right: a more complex app that includes two dynamic feature APKs (D) and corresponding configuration APKs © for download on demand.

Different types of split APKs

  • Base APK : This APK contains code and resources that all other split APKs can access and provides the basic functionality for your app. When the user downloads the app, they always get this APK.
  • Configuration APKs : Each of these APKs includes only native libraries and resources for a given device configuration — screen density, language or CPU architecture. When a device downloads a base or dynamic feature APK, it downloads only the libraries and resources it needs. Add the following code in android {} block to enable language, density, and abi configuration splits
bundle {
language {
enableSplit = true
}
density {
enableSplit = true
}
abi {
enableSplit = true
}
}
  • Dynamic Feature APKs : Each of these APKs contain code and resources that are not required when your app is first installed, but may be downloaded and installed later.

For devices running Android 4.4 (API level 20) and lower, Google Play automatically serves a single APK that’s optimized for the device’s configuration.

Deploy your app from an app bundle

If you want Android Studio to build and deploy your app from an app bundle:

  1. Select Run > Edit Configurations from the menu bar.
  2. In the left pane of the Run/Debug Configurations dialog, select your desired configuration under the Android App node.
  3. In the dropdown menu next to Deploy, select APK from app bundle.
  4. Under Dynamic features to deploy, check the box next to each dynamic feature module, if any, you want to include when deploying your app.
  5. Click OK.

Testing your app bundle with Google Play Internal Test Track

Before you can upload your app bundle to the Play console you need to generate a signed app bundle. To generate a signed app bundle, proceed as follows:

  1. Select Build > Generate Signed Bundle/APK from the menu bar. In the Generate Signed Bundle or APK dialog, select Android App Bundle and click Next.
  2. In the Module dropdown menu, select the base module of the app you want to generate an app bundle for.
  3. Provide information for an existing key and keystore, or create a new one. This is the same type of key and keystore information you provide when building a signed APK.
  4. If you want Android Studio to also save your signing key as an encrypted file, check the box next to Export encrypted key. To be able to upload your app bundle and take advantage of Dynamic Delivery, you need to upload this encrypted file to the Play Console and enroll in app signing by Google Play.
  5. Click Next and provide a Destination Folder for your app bundle. Select the Build Type and flavors that you want to generate app bundles for.
  6. Click Finish.

Now that you’ve generated a signed bundle, you can upload your app bundle to the Play Console.

Testing your app bundle using BundleTool

Bundletool is the underlying tool that Gradle, Android Studio, and Google Play use to build an Android App Bundle or convert an app bundle into the various APKs that are deployed to devices.

To generate an APK set for all device configurations your app supports from your app bundle, use the bundletool build-apks command

$ bundletool build-apks --bundle=<path to .aab> --output=<out.apks>

Only two arguments are required, 1) the path the .aab file and, 2) the directory where the generated apks are stored. Now you can run build-apks.

WARNING: The APKs won't be signed and thus not installable unless you also pass a keystore via the flag --ks.$ bundletool build-apks --bundle=/MyApp/my_app.aab --    output=/MyApp/my_app.apks
--ks=/MyApp/keystore.jks
--ks-pass=file:/MyApp/keystore.pwd
--ks-key-alias=MyKeyAlias
--key-pass=file:/MyApp/key.pwd

To deploy your app from an APK set, use the install-apks command and specify the path of the APK set

bundletool install-apks --apks=/MyApp/my_app.apks

Thank You!!!

--

--