Android App Bundles & Local Testing

Mithat Sinan Sarı
Hipo
Published in
5 min readJan 9, 2020

--

Apps binaries that we deliver to the Play Store contain tons of libraries and resources. We can reduce the amount of unnecessary data to an extent with proguard but still we couldn’t generate device configuration specific APKs. You might think this has minimal impact, but there are lots of configurations:

  • Processor types (arm based armeabi-v7a, arm64-v8a or intel based x86, x86_64…)
  • Sreen resolutions (hdpi, xhdpi, xxhdpi…)

To be able to support all of these combinations, we used to include everything in the same package and deliver that to all users. This is where the new app bundles come in!

With app bundles, users get only what they need based on the device they are installing the app on, instead of having all possible combinations. If the device has a 32-bit processor, app bundle takes compatible APK from the package. Then looks at the screen size of the same device and takes the matching resources for that specific screen resolution. It does the same thing for locale and SDK version, prepares a new APK and makes it available for the user. This results in dramatically reduced download sizes!

As an example, we’ve recently generated an AAB for a project we have been working on, and there were 95 little APKs in it with a total size of 38 MB. My device needs only four of them, which is a total of 27 MB. The difference is huge: 11 MB! If we consider that my device’s screen resolution is xxhdpi, difference will be even more when target device’s resolution is lower than xxhdpi.

Where to start?

Assuming you are now sold on the benefits of app bundles, let’s see how we can switch to using them! Generating an app bundle is very similar to how you used to generate APKs:
Build -> Generate Signed Bundle/APK… And this screen comes up:

Choose App Bundle. And click next, fill the required fields:

You may want to check “Remember passwords”.

There is another checkbox about encrypted key. It creates .pepk file for Play Store. If you decide to use App Signing, you are going to need that .pepk file.

For extra information about App Signing, you can check here.

As last step, we will choose destination folder and build variants, then click finish.

Now you have a signed app bundle ready for Play Store! 🎉

In the destination folder, there should be an .aab file. It is your bundle file. Now you can upload and publish your bundle file on Play Store.

How to test?

Another thing you might want to do is to be able to locally test the app bundle and its outputs. Here is what you will need to do be able to do this:

  • An android test device
  • Bundletool
  • Unzipping tools like Winrar or The Unarchiver (optional)

Let’s start!

First we need to download Bundletool. You can find the latest version on Google Bundletool Github repo.

After downloading bundletool, we are going to use the terminal. Hacker time!

Since I don’t use any helper tools like Homebrew, I will explain everything on a vanilla terminal setup.

First, we need to build an .apks file. .apks file is an APK set archive that contains all the APKs generated by bundletool. To build that file, we are going to use the build-apks command and some parameters.

These are the parameters we are going to use:

If you dont give keystore parameters like keystore file or pass, APKs will be signed with the debug keystore. Let’s give keystore parameters to generate signed APKs:

Lets combine all of them:

java -jar ~/Downloads/bundletool-all-0.12.0.jar build-apks --bundle=~/StudioProjects/MyApp/app/app-prod-release.aab --output=out_bundle_archive_set.apks 
--ks=~/StudioProjects/MyApp/keystore.jks
--ks-pass=pass:mypassword123
--ks-key-alias=MyKeyAlias
--key-pass=pass:mypassword123

This single command will create an .apks file in our root folder.

There was an issue about installing device specific APKs with — connected-device command. It gives UnknownFlagsException. I believe they will fix this soon. If you don’t use — connected-device flag, bundletool generates APKs for all device configurations your app supports. This is basically like the legacy APK.

That’s why we are going to generate our device specific APKs and install them manually.

Generate device specific APKs

First we need to create json file that contains our device’s configs:

This is my device’s config json file. You should create your own test device’s config json. After creating json file, we are able to create device specific APKs. Now let’s call command below:

java -jar ~/Downloads/bundletool-all-0.12.0.jar extract-apks --apks=~/out_bundle_archive_set.apks 
--output-dir=~/apks/huawei
--device-spec=~/huawei_spec.json

Now we have device specific APKs. Don’t try to install them individually, doesn’t work (I’ve tried). To install that APK we need to call:

./adb install-multiple 
~apks/huawei/base-tr.apk
~apks/huawei/base-arm64_v8a_2.apk
~apks/huawei/base-master_2.apk
~apks/huawei/base-xxhdpi.apk

Those are my device specific APKs and the folder is where I extracted my APKs. Yours will be different, so don’t forget to change them!

If you get adb command not found error, make sure that you are in platform-tools folder.
It is usually in ~/Libraries/Android/sdk/platform-tools/adb (Mac)

That’s it! Now you can test your app on your device!

Optional part

To see all the APKs that bundle generates, go to the folder where you extract your .apks file and extract it with unzipping tool. You will see splits folder and toc.pb file. splits folder contains all the APKs. You can try different packages, like re-install your app by choosing different language package etc.

Keep an eye on posts from Hipo by subscribing to our newsletter and following us on Twitter.

--

--