Extracting an APK file from Android Studio

In Google I/O 2018, Android App Bundle, a new file format for distributing Android apps via Google Play, was launched. Later in 2019, Google began encouraging that developers publish new apps/updates in .aab format rather than the traditional.apk format. The primary goal of the Android Program Bundle is to reduce the size of the app you download from the Play Store. Technically, it is a collection of several APKs that are dynamically created for various device setups. If you wish to install a .aab file outside of the Play Store, you must first extract the APK files from the AAB and manually install them on your Android device.

Unlike traditional.apk files, the .aab file cannot be shared or sideloaded for testing. When you publish the .aab file to Google Play, an optimized.apk file will be produced dynamically based on the user’s device information.

For instance, if an Android app has many “strings.xml” files for different languages and your device’s default language is English. To decrease the.apk size, a.apk file will be produced dynamically for your device, omitting all other language files.

This feature is convenient for users, but it adds additional work for developers and testers. Because .aab files cannot be installed like APKs, you must either post them to the Play Store for testing or manually build APK files from Android App Bundle (.aab). After extracting numerous APKs or a universal APK file from .aab, you may install the program on various device settings. Meanwhile, you can also run or open an apk with an online emulator.

Converting Android App Bundle (.aab) to APK File

After creating the Android App Bundle in Android Studio or your preferred IDE, you should first verify how the produced APKs function when deployed on local mobile devices. The simplest approach to accomplish this is to convert the .aab file to APK and install it in a regular manner.

We’re using bundletool, an open-source command-line tool accessible on GitHub, to extract APK files from an Android App Bundle. This program is used to manipulate the Android App Bundle in order to build an APK set archive (.apks) file. After creating the “.apks” file using bundletool, we can extract the APK files by converting the file to.zip format.

Here’s how to use bundletool to extract.apk files from an Android App Bundle and generate .apks archive files:

Step 1: Get the bundletool command-line tool

First and foremost, get the most recent version of the bundletool jar file from GitHub and store it to your Desktop in a folder. At the time of writing, the most recent version of bundletool is 0.10.2, therefore the file name should be changed from bundletool-all-0.10.2.jar to bundletool.jar for simplicity.

Step 2: Generate APK Set Archive (.apks) file from .aab

Copy your Android App Bundle (.aab) file to the same location where you obtained the bundletool.jar earlier. This is critical, and you must ensure that both the bundletool.jar and the .aab files are in the same folder every time you execute the bundletool command.

The name of my Android App Bundle is nhl.aab, as seen in the screenshot, and both “bundletool.jar” and “nhl.aab” files are in the same folder. We’re going to use the AAB file from our Notification History application for this lesson.

Now, open the Command Prompt (Windows) or Terminal (Mac) and use the CD command to change the directory to your Desktop folder. Run the build-apks command to produce an APK set archive (.apks) file containing various APK files for every device settings your program supports.

java -jar bundletool.jar build-apks –bundle=nhl.aab –output=nhl.apks

  • bundletool.jar – The name of .jar file you downloaded from GitHub
  • nhl.aab – Your Android App Bundle file name
  • nhl.apks – The name of the output file. It is a .apks file. Don’t confuse it with the traditional .apk file.

Following the execution of the above command, a new file with the name “nhl.apks” will be produced in the same folder. This is an APK archive file, and unpacking it will give you many APK files for various device setups.

Step 3: Extract APK Files

It is quite simple to extract APK files from the “nhl.apks” file after it has been produced. Simply rename the “nhl.apks” file to “nhl.zip” and extract it normally using a ZIP file extractor. There are now several APK files in split and independent directories.

You have successfully converted the .aab file to.apk files, which you can now install on your device. This is how you can technically install an.aab file on an Android smartphone.

But wait, it’s not done yet. Did you have any issues with the preceding command? Yes, it is creating a large number of Debug APK files, and you are unsure which one to install. What if you want Android App Bundle’s signed APKs or a universal signed APK? Fortunately, bundletool has many options for extracting the needed APK from the .aab file.

Generating Signed APKs using your Keystore

Run the following command to produce signed APK files with your own keystore file.

java -jar bundletool.jar build-apks –bundle=nhl.aab –output=nhl.apks –ks=keystore.jks –ks-pass=pass:your_keystore_password –ks-key-alias=your_key_alias –key-pass=pass:your_key_password

This will create a “nhl.apks” file, which you must rename to “nhl.zip” and unpack to view signed apks in both split and standalone directories.

Generating a Universal APK from Android App Bundle

Run the following command if you just want to build one APK from your .aab file.

java -jar bundletool.jar build-apks –bundle=nhl.aab –output=nhl.apks –mode=universal

This program will create a nhl.apks file, which you must rename to nhl.zip and unpack to obtain a universnal.apk file.

Overwriting existing .apks file

To overwrite the old.apks file with the new one, simply add the -overwrite flag to your command, as shown below.

java -jar bundletool.jar build-apks –bundle=nhl.aab –output=nhl.apks –overwrite

Extracting Signed Universal APK file from Android App Bundle

Now, let’s merge all of the aforementioned flags to create a single universal APK file and overwrite any existing APK archives.

java -jar bundletool.jar build-apks –bundle=nhl.aab –output=nhl.apks –overwrite –mode=universal –ks=keystore.jks –ks-pass=pass:your_keystore_password –ks-key-alias=your_key_alias –key-pass=pass:your_key_password

You must change the created “nhl.apks” file to “nhl.zip” and unpack it to locate the signed “universal.apk” file after running this command.

There are a few more complex flags to produce APK and install Android App Bundle on connected or particular device settings, such as “—device-spec, —connected-device, —device-id.” In addition to these commands, there are “extract-apks” and “get-size total” commands for other use cases.