DEV Community

Cover image for πŸ“± Generate and Extract APKs from AAB Using BundleTool
Amit Kumar
Amit Kumar

Posted on

4 1 1 1 1

πŸ“± Generate and Extract APKs from AAB Using BundleTool

🎯 Introduction

When preparing for a production release, we often generate an Android App Bundle (AAB) for the Play Store while also needing an APK for the QA team to test. Manually creating both separately can be time-consuming. To streamline this process, we can generate the AAB once and extract the APK from it, making the workflow faster and more efficient. This guide will walk you through the steps to achieve that seamlessly!πŸš€


πŸ›  Step 1: Download BundleTool

Before you begin, download the latest version of BundleTool from the official GitHub repository. Once downloaded, follow these steps:

  1. Copy the bundletool-all-x.x.x.jar file and paste it into your workplace/project directory.

  2. Copy your release.keystore file into the same folder where you placed the bundletool-all-x.x.x.jar file.

  3. Copy your your.aab file into the same folder where you placed the bundletool-all-x.x.x.jar and release.keystore file

  4. Ensure that the bundletool-all-x.x.x.jar file, .aab file, and keystore file are all in the same directory before running the command.


πŸ”§ Step 2: Generate APKs from AAB for All Devices

Use the following command to generate multiple APKs for different device architectures:

java -jar /Users/amitkumar/Desktop/bundletool-all-1.18.1.jar build-apks \
  --bundle=/Users/amitkumar/Desktop/yourproject.aab \
  --output=./yourproject.apks \
  --ks=/Users/amitkumar/Desktop/your-release.keystore \
  --ks-key-alias=your-keystore-alias \
  --ks-pass=pass:<your-keystore-password> \
  --key-pass=pass:<your-key-password>

Enter fullscreen mode Exit fullscreen mode

This command will generate multiple APKs based on different device configurations. πŸ“‚

Then run this command in sane directory in order to unzip it

unzip yourproject.apks -d yourproject_extracted

Enter fullscreen mode Exit fullscreen mode

🌍 Step 3: Generate a Universal APK (Works on All Devices)

If you need a single APK that works on all devices, use:

java -jar bundletool-all-1.18.1.jar build-apks \
  --bundle=/Users/amitkumar/Desktop/yourproject.aab \
  --output=my_universal.apks \
  --mode=universal \
  --ks=/Users/amitkumar/Desktop/your-release.keystore \
  --ks-key-alias=your-keystore-alias \
  --ks-pass=pass:your-keystore-password \
  --key-pass=pass:your-key-password

Enter fullscreen mode Exit fullscreen mode

This will generate a single APK that runs on all Android devices. πŸ“±βœ…

Then run this command in same directory in order to unzip it

unzip my_universal.apks -d my_universal_extracted

Enter fullscreen mode Exit fullscreen mode

πŸ“‚ Step 4: Automatically Unzip Extracted APKs

Once you have the .apks file, you need to unzip it to access the actual APK files. You can do it in a single command:

java -jar bundletool-all-1.18.1.jar build-apks \
  --bundle=/Users/amitkumar/Desktop/yourproject.aab \
  --output=my_universal.apks \
  --mode=universal \
  --ks=/Users/amitkumar/Desktop/your-release.keystore \
  --ks-key-alias=your-keystore-alias \
  --ks-pass=pass:your-keystore-password \
  --key-pass=pass:your-key-password
  && unzip my_universal.apks -d my_universal_extracted

Enter fullscreen mode Exit fullscreen mode

This command will:

  1. Generate a universal APK.
  2. Automatically unzip it into my_universal_extracted folder. πŸŽ‰

⚑ Step 5: Automate the Process with package.json

If you are running this frequently, add the command to your package.json scripts:

"scripts": {
  "build-apks": "java -jar bundletool-all-1.18.1.jar build-apks --bundle=android/app/yourproject.aab --output=android/app/my_universal.apks --mode=universal --ks=android/app/your-release.keystore --ks-key-alias=YOUR_ALIAS --ks-pass=pass:YOUR_KEY_STORE_PASSWORD--key-pass=pass:YOUR_KEY_PASSWORD && unzip android/app/my_universal.apks -d android/app/my_universal_extracted"
}
Enter fullscreen mode Exit fullscreen mode

Now, just run:

npm run build-apks
Enter fullscreen mode Exit fullscreen mode

This simplifies the process and avoids running long commands manually. πŸ”₯


πŸš€ Conclusion

With these commands, you can easily generate and extract APKs from an AAB file, whether for different architectures or a universal APK. Automating the process using package.json makes it even more efficient!

Happy coding! πŸ‘¨β€πŸ’»πŸŽ‰

Postmark Image

20% off for developers shipping features, not fixing email

Build your product without worrying about email infrastructure. Our reliable delivery, detailed analytics, and developer-friendly API let you focus on shipping features that matter.

Start free

Top comments (0)

Image of Datadog

Get the real story behind DevSecOps

Explore data from thousands of apps to uncover how container image size, deployment frequency, and runtime context affect real-world security. Discover seven key insights that can help you build and ship more secure software.

Read the Report

πŸ‘‹ Kindness is contagious

Value this insightful article and join the thriving DEV Community. Developers of every skill level are encouraged to contribute and expand our collective knowledge.

A simple β€œthank you” can uplift someone’s spirits. Leave your appreciation in the comments!

On DEV, exchanging expertise lightens our path and reinforces our bonds. Enjoyed the read? A quick note of thanks to the author means a lot.

Okay