Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Achieving Smaller Apps and Simpler Releases with Android App Bundle

1 Oct 2018 1  
In this article I explain how Android App Bundles can help you, and demonstrate how to use them.

This article is in the Product Showcase section for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers.

For mobile phone users, getting an “insufficient storage” warning can be extremely annoying. Phone storage capacity is still a limited resource. Although phone storage capacity has grown over the years, so have all the things that fill up our phones: music, apps, games, photos, etc.

A traditional Android Package Kit (APK) contains a collection of code and resources (layout files, images, etc.) for all devices an app supports. So when you install an APK, you are installing resources that you probably will never use. Your screen size doesn’t change nor your CPU; you typically don’t speak all the languages an app supports. Clearly, an APK gives you more than you ask for and takes up more space than is truly needed.

If oversized APKs are the bane of your existence, however, you now have a solution. The Android App Bundle was introduced at this year’s Google I/O to help developers deliver great apps in smaller APK sizes. Below, I explain how Android App Bundles can help you, and demonstrate how to use them.

What Is an Android App Bundle?

The Android App Bundle is simply a publishing format—precisely, a zip archive with a .aab extension. It contains code and the resources for all devices an app supports, such as DEX files, native libraries, the manifest file(s), assets, etc. Once uploaded for publishing, Google Play handles APK signing and generation, a process termed Dynamic Delivery. Dynamic Delivery is used to generate an optimized APK for users based on their device configuration. How exactly is this done?

Source: https://goo.gl/BbLjWE (Android Developers Blog)

Split APKs (introduced in Lollipop) are generated from a given Android App Bundle and act like a single APK. A typical app gets a base APK and configuration APKs. And, if the app has a dynamic feature, users get the dynamic feature APK and its configuration APKs as well. The base APK holds files common to all device configurations, like the manifest file. Configuration APKs are generated for you, and each includes resources for a particular device configuration: language, CPU architecture, or screen density. So a user gets a standard base APK, like all other devices, and configuration APKs that contain only the relevant resources for the user’s device.

This means my Android One phone (Xiaomi Mi A1) gets a base APK, configuration APKs for English, arm64 CPU architecture, and xhdpi screen resolution. Even more interesting—When device configuration (like language) changes, Google Play detects it and downloads the configuration APK for the language. To further reduce APK size, plans to target devices based on texture compression format, graphics API, and new platform features are in the offing.

A dynamic feature APK has the code and resources for app features that are not needed when a user first installs the app. Developers can add features or functionalities to their apps as dynamic feature modules which Google Play serves on-demand, instead of at install time, further reducing app download size. It makes sense to carve out features that take up significant space and aren’t needed at install time and those that are rarely used into dynamic features modules.

Devices with Android versions that predate Lollipop also see some noticeable size reduction, but will have all languages included in their APKs.

By now, it’s evident that building a giant APK is ineffective and an outdated path to take. The Android App Bundle is the future of Android app delivery, and we’ll see later how to build one.

Android App Bundle != APK

An Android App Bundle shares some similarities with an APK, but they are distinct and serve different purposes. First, the app bundle is purely meant for upload. Users cannot install and use it directly. The app bundle is a ZIP archive file from which optimized APKs are generated and served to devices. The Android App Bundle contains metadata files meant to help tools generate APKs (which don’t end up in the APK). In addition, the app bundle has a strict validation standard.

Switching from APKs to an app bundle is a seamless process. Using Android Studio 3.2 and higher, you can generate a signed app bundle in just a few clicks and upload it to the Play Store for users to start reaping the benefits of a small-size APK. As there is no major change to your codebase, there is nothing to be concerned about. Early adopters of the Android App Bundle have already reported significant decreases in app size because of Dynamic Delivery. Some developers have even slashed their APK sizes in half.

Source: https://goo.gl/4dpWqD (Android App Bundle Page)

What Are the Benefits of Android App Bundles?

Android App Bundles bring several benefits to app developers as well as users. Here, we look at some of these benefits.

  • A smaller app size means more users can install your app without having to contemplate what to delete to make space for it.
  • A smaller app means it’s less likely users will uninstall your app to make space on their device.
  • There’s no need to build, sign, or manage multiple APKs as the Android App Bundle is a single artifact.
  • It’s possible to add dynamic features for specific use cases and audiences without increasing the size of your app at install time.

You can only expect an even longer list as many interesting features will soon be available to developers, such as app bundles soon supporting instant apps too.

Building the App Bundle

Most app projects won’t require much effort to build app bundles that support serving optimized APKs using Dynamic Delivery. In fact, with a few clicks in Android Studio, you can build your signed Android App Bundle ready for publishing if you already organize your apps code and resources according to established conventions. But first, we’ll need to get Android Studio Canary. Also worth noting that Android Studio 3.2 stable has recently been released.

With a project opened in Android Studio, go to the menu bar and select Build > Build Bundle(s)/APKs > Build Bundle(s). Wait for your app bundle to be generated. You can locate the .aab file or analyze the generated app bundle by selecting the options from the pop-up after the app bundle is generated.

This Android App Bundle cannot be uploaded to the Play Console because it’s not signed. To generate a signed version, select Build > Generate Signed Bundle/APK… > Android App Bundle > Select Base Module > Enter Signing Credentials & Check any other options (just as you do for an APK).

The same Android App Bundle can be generated via the command line with Gradle’s bundle task. To generate a signed app bundle, the signing information has to be configured in the module’s build.gradle file.

# building the app bundle 

./gradlew modulename:bundle
./gradlew modulename:bundleVariant

To gain finer control over how splits are generated from the Android App Bundle, you can do so from the bundle block.

android {
...
  bundle {
     density {
         enableSplit = <true or false>
     }

     language {
         enableSplit = <true or false>
     }

     abi {
         enableSplit = <true or false>
     }
  }
}

Bundletool Comes in Handy

Bundletool is a command-line tool for working with Android App Bundles. With bundletool, you can build an Android App Bundle, extract connected device specifications, generate the APK Set archive (.apks), extract/install the APK from an APK Set archive, and validate app bundles. Since the Android App Bundle is solely a publishing format, bundletool is what is used to generate APKs for testing. Google Play and the IDE use the same tool.

The tool is open source and can be found on GitHub.

Using bundletool

bundletool <command> ...

Here’s a list of all the commands bundletool accepts: build-bundle, build-apks, extract-apks, get-device-spec, install-apks, validate, and version. Use bundletool help <command> to obtain detailed information about a command such as flags and other options.

In the following example, we’ll find the specification of the connected device, build the APKs, and install an APK for the connected device.

# Get Device Spec
bundletool get-device-spec --adb='/usr/bin/adb' --output='specs.json'

# View Device Specification
cat specs.json 
{
  "supportedAbis": ["arm64-v8a", "armeabi-v7a", "armeabi"],
  "supportedLocales": ["en-US"],
  "screenDensity": 480,
  "sdkVersion": 27
}

# Build APKs
bundletool build-apks  --bundle='<path-to-.aab>' /
--device-spec='<path-to-specs.json>' --output='<path-to-output.apks>'


# Install the APK
bundletool install-apks --apks='<path-to-ouput.apks>'

Dynamic Feature Modules

Dynamic feature modules enable you to move certain features into a separate module. Dynamic delivery is used to serve these features on demand to users. Dynamic feature modules require some effort and careful consideration. Check out the documentation for more details on creating and configuring dynamic feature modules.

Uploading to the Play Store

Once you have a signed Android App Bundle, you can upload it to the Play Store and let Google Play dynamically deliver optimized APKs to your users. You also need to enroll in app signing by Google Play so you can upload the app bundle.

App signing by Google Play provides a secure way to manage your app signing keys. Google Play manages your app signing key and you only use your upload key, which is used to verify your identity. Managing app signing keys yourself is quite risky. Once you lose your keys, you’re unable to update your published apps; likewise, your keys are susceptible to theft. Delegating the signing process to Google Play ensures your keys are safer and you can request a reset of your upload key. So, you never have to get locked out of your own app. Once enrolled in app signing, Google Play automatically generates and signs the APKs it sends to users’ devices.

Once uploaded, click on the app bundle and select Explore App Bundle to see how much size was saved. From here, you can search and download device-specific APKs. For the listed configurations, you can select VIEW DEVICES to see a list of devices that will get that particular APK.

And the good news is that Android App Bundles can be published via the Publishing API.

Closing Thoughts

As we’ve seen, the Android App Bundle is the future of Android app delivery, rendering monolithic APKs obsolete. Not only do apps benefit from size reduction, but apps also get to deliver on-demand features that provide a unique experience to each user.

Even more interesting, developers enjoy the benefits with minimal effort. Already, some developers have seen a huge reduction in app size. Users also benefit greatly as their devices get more free space for other things like music, movies, and even apps. Using Android Vitals, developers can further improve the performance and stability of their apps on users’ devices.

Move to the app bundle format and find out how much you can reduce your app size (and by what factor you can increase the number of installs).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here