Extracting a universal .apk from .aab file

Bundletool with Fastlane 🚀

Martin Gonzalez
3 min readDec 9, 2019

One build to rule them all.

I was doing a continuous integration task making a production pipeline that allows to make the release candidate to an internal application of the company and at the same time, after testing it, upload it to the Play Store, but a problem arose, you can not distribute .aab files.

Whats an .aab?

An AAB file is an Android App Bundle that developers use for uploading apps to Google Play. After upload, Google Play uses a process called Dynamic Delivery to deliver optimized versions of app packages (.APK files) to user devices so they contain only the specific portions of the app that each device needs to run. With Dynamic Delivery, app install sizes can be reduced significantly on user devices.

https://fileinfo.com/extension/aab

How is our pipeline?

We use Unity to create our games, and one of our main target platform is Android. Since Android changed the way to upload files to the Play Store Unity has a checkbox to export the build as an Android App Bundle, cool right?

We have a CI pipeline that run Unity tests, a Unity build and then distribute the binary to an internal application we have, similar to Apple Testflight app. The problem comes here, we cannot distribute AABs, because it’s something Google uses to deploy your app in several architectures, so the only way to share the binary is to make it an apk, and here there are 2 options, the easy one and the correct one.

The easy way

We can create a release_candidate branch that runs Unity tests and a Unity build exporting an apk instead of aab.

But this has a defect. If we want to create the productive binary it must be in another branch, like master or trunk but we will have to run the risk that our binary is not identical to the release_candidate branch binary because it’s a complete hole new build.

The correct way

We need to build once and be able to distribute wherever we want without losing that confidence in the binary we are testing, so in a way we need to transform that .aab file to an apk in the fly

Bundletool to the rescue

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. bundletool is also available to you as a command line tool, so you can recreate, inspect, and verify Google Play’s server-side build of your app’s APKs.

https://developer.android.com/studio/command-line/bundletool

With this tool we can extract a universal .apk file and also sign it for distribution.

Because we use fastlane as an automation library I created a fastlane plugin called fastlane-plugin-bundletool that allows you to download a specific version of bundletool and extract the apk. Feel free to use it.

Let’s see an example of our pipeline.

Capture of project Fastfile
Capture of CI Runner output

Summary

Now I can have a unique pipeline both to distribute the app internally and to upload it to the Play Store.

Thanks for reading! :)

🐦 Twitter 🍫 Github 🚀 Fastlane Json Plugin 🔗 LinkedIn
◼️ Medium

--

--

Martin Gonzalez

Software Developer working at Tactile Games Denmark, Unity developer for 10 years and I can not sit still. I love helping and learning from others.