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



This content originally appeared on DEV Community and was authored by Amit Kumar

🎯 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 and your .aab file.

  3. 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>

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

🌍 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

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

📂 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

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"
}

Now, just run:

npm run build-apks

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! 👨‍💻🎉


This content originally appeared on DEV Community and was authored by Amit Kumar