Generate an APK file from an AAB file (Android app bundle)
Asked Answered
D

11

171

Is there a way to generate an APK file from an Android Application Bundle (AAB) via Terminal or using Android Studio?

Doorsill answered 29/10, 2018 at 6:41 Comment(2)
The supplied answers below only talk about how to generate an APKS file, which is straight out of the bundletool documentation. Has anyone found a way to get an actual APK (not APKS) file out of a bundle?Rerun
@Rerun my answer does exactly that! https://mcmap.net/q/101137/-generate-an-apk-file-from-an-aab-file-android-app-bundleBrambly
S
169

By default, the IDE does not use app bundles to deploy your app to a local device for testing

Refer to the bundletool command.

For the debug APK command,

bundletool build-apks --bundle=/MyApp/my_app.aab --output=/MyApp/my_app.apks

For the release APK command,

bundletool build-apks --bundle=/MyApp/my_app.aab --output=/MyApp/my_app.apks
    --ks=/MyApp/keystore.jks
    --ks-pass=file:/MyApp/keystore.pwd
    --ks-key-alias=MyKeyAlias
    --key-pass=file:/MyApp/key.pwd

I have been using the following commands while testing my release build for AAB:

  1. Download the bundletool JAR file from the GitHub repository (Latest release* → Assetsbundletool-all-version.jar file). Rename that file to bundletool.jar

  2. Generate your AAB file from Android Studio, e.g.: myapp-release.aab

  3. Run the following command:

    java -jar "path/to/bundletool.jar" build-apks --bundle=myapp-release.aab --output=myapp.apks --ks="/path/to/myapp-release.keystore" --ks-pass=pass:myapp-keystore-pass --ks-key-alias=myapp-alias --key-pass=pass:myapp-alias-pass
    
  4. myapp.apks file will be generated

  5. Make sure your device is connected to your machine

  6. Now run the following command to install it on your device:

    java -jar "path/to/bundletool.jar" install-apks --apks=myapp.apks
    

If you need to extract a single .apk file from the .aab file, you can add a extra parameter, --mode=universal to the bundletool command:

bundletool build-apks --bundle=/MyApp/my_app.aab --output=/MyApp/my_app.apks \
    --mode=universal \
    --ks=/MyApp/keystore.jks \
    --ks-pass=file:/MyApp/keystore.pwd \
    --ks-key-alias=MyKeyAlias \
    --key-pass=file:/MyApp/key.pwd

And execute

unzip -p /MyApp/my_app.apks universal.apk > /MyApp/my_app.apk

This will generate a single /MyApp/my_app.apk file that can be shared an installed by any device app installer.

Serles answered 29/10, 2018 at 6:46 Comment(8)
"By default", Studio doesn't, but it can be done easily. See my answer below.Extensive
To nit, this doesn't actually answer the question. This generates an APKS file, not an APK file. An APKS file cannot be as easily installed to a device and requires the receiver of the APK to use the bundletool to install it, or go through AS, or something else that only a developer probably would know how to do.Rerun
@Rerun did you find any solution?Donovan
This command will just generate the APK Archive File (.apks) and we need to convert it to .zip format and extract it only to find plenty of APK files. The correct way is using a --mode:universal flag. I submitted an edit couple of times but unfortunately, they were rejected for a weird reason - "edit deviates from the original intent of the post". Common reviewers, I'm trying to complete the incomplete accepted answer.Flossieflossy
Can you also add to your answer the possibility to do this in Android Studio with one click? That's much simpler than running these commands.Extensive
getting SEVERE: Input Future failed with Error java.lang.OutOfMemoryError: Java heap space please helpGuillemot
Can this tool also be used within an Android app? Can it be used to just install the aab file directly, without extracting to multiple apk files first?Coopery
use --mode=universal in bundletool command to create only one apk in the endCalie
B
185

So far nobody has provided the solution to get the APK from an AAB.

This solution will generate a universal binary as an apk.

  1. Add --mode=universal to your bundletool command (if you need a signed app, use the --ks parameters as required).

     bundletool build-apks --bundle=/MyApp/my_app.aab --output=/MyApp/my_app.apks --mode=universal
    
  2. MAIN STEP: Change the output file name from .apks to .zip

  3. Unzip and explore

  4. The file universal.apk is your app

This universal binary will likely be quite big but is a great solution for sending to the QA department or distributing the App anywhere other than the Google Play store.

Brambly answered 22/7, 2019 at 15:38 Comment(4)
What if I want to generate the APK file within Android itself? Is it possible?Coopery
Can .apk be used to edit the app (or at least review the code) and then reupload to Google Play?Complexioned
You can also directly build the APK corresponding to your connected device, by using build-apks with --connected-device, and then bundletool install-apksSubastral
I've made an app, it runs perfect local. So I build the apks file and use the bundletool as mentioned above. The app doesn't run. So there is something wrong with this method.Lynellelynett
S
169

By default, the IDE does not use app bundles to deploy your app to a local device for testing

Refer to the bundletool command.

For the debug APK command,

bundletool build-apks --bundle=/MyApp/my_app.aab --output=/MyApp/my_app.apks

For the release APK command,

bundletool build-apks --bundle=/MyApp/my_app.aab --output=/MyApp/my_app.apks
    --ks=/MyApp/keystore.jks
    --ks-pass=file:/MyApp/keystore.pwd
    --ks-key-alias=MyKeyAlias
    --key-pass=file:/MyApp/key.pwd

I have been using the following commands while testing my release build for AAB:

  1. Download the bundletool JAR file from the GitHub repository (Latest release* → Assetsbundletool-all-version.jar file). Rename that file to bundletool.jar

  2. Generate your AAB file from Android Studio, e.g.: myapp-release.aab

  3. Run the following command:

    java -jar "path/to/bundletool.jar" build-apks --bundle=myapp-release.aab --output=myapp.apks --ks="/path/to/myapp-release.keystore" --ks-pass=pass:myapp-keystore-pass --ks-key-alias=myapp-alias --key-pass=pass:myapp-alias-pass
    
  4. myapp.apks file will be generated

  5. Make sure your device is connected to your machine

  6. Now run the following command to install it on your device:

    java -jar "path/to/bundletool.jar" install-apks --apks=myapp.apks
    

If you need to extract a single .apk file from the .aab file, you can add a extra parameter, --mode=universal to the bundletool command:

bundletool build-apks --bundle=/MyApp/my_app.aab --output=/MyApp/my_app.apks \
    --mode=universal \
    --ks=/MyApp/keystore.jks \
    --ks-pass=file:/MyApp/keystore.pwd \
    --ks-key-alias=MyKeyAlias \
    --key-pass=file:/MyApp/key.pwd

And execute

unzip -p /MyApp/my_app.apks universal.apk > /MyApp/my_app.apk

This will generate a single /MyApp/my_app.apk file that can be shared an installed by any device app installer.

Serles answered 29/10, 2018 at 6:46 Comment(8)
"By default", Studio doesn't, but it can be done easily. See my answer below.Extensive
To nit, this doesn't actually answer the question. This generates an APKS file, not an APK file. An APKS file cannot be as easily installed to a device and requires the receiver of the APK to use the bundletool to install it, or go through AS, or something else that only a developer probably would know how to do.Rerun
@Rerun did you find any solution?Donovan
This command will just generate the APK Archive File (.apks) and we need to convert it to .zip format and extract it only to find plenty of APK files. The correct way is using a --mode:universal flag. I submitted an edit couple of times but unfortunately, they were rejected for a weird reason - "edit deviates from the original intent of the post". Common reviewers, I'm trying to complete the incomplete accepted answer.Flossieflossy
Can you also add to your answer the possibility to do this in Android Studio with one click? That's much simpler than running these commands.Extensive
getting SEVERE: Input Future failed with Error java.lang.OutOfMemoryError: Java heap space please helpGuillemot
Can this tool also be used within an Android app? Can it be used to just install the aab file directly, without extracting to multiple apk files first?Coopery
use --mode=universal in bundletool command to create only one apk in the endCalie
E
75

Ok here is the complete way I had to do:

  1. Download bundletool-all-0.10.3.jar from this link, download the latest version available

  2. Create an app bundle using android studio and locate its path:

    In my case its E:\Projects\Android\Temp\app\build\outputs\bundle\debug\app.aab

  3. Copy the bundletools jar to some location and get its path

In my case its E:\Temp\bundletool-all-0.6.0.jar

Use this command:

java -jar "BUNDLE_TOOL_JAR_PATH" build-apks --bundle="BUNDLE_PATH" --output=YOUR_OUTPUT_NAME.apks

In my case it will be

    java -jar "E:\Temp\bundletool-all-0.6.0.jar" build-apks \
        --bundle="E:\Projects\Android\Temp\app\build\outputs\bundle\debug\app.aab" \
        --output=out_bundle_archive_set.apks
  1. This will create a file out_bundle_archive_set.apks , rename it to .zip out_bundle_archive_set.zip , extract this file and done You will have multiple apk files

To install directly on external device use :

java -jar "E:\Temp\bundletool-all-0.6.0.jar" install-apks --apks=out_bundle_archive_set.apks

Check this blog post for more info . also check out official site

Euphorbia answered 29/10, 2018 at 7:35 Comment(3)
Renaming .apks to .zip and unzipping it were the parts I was looking for. Thank you!Proximate
SEVERE: Input Future failed with Error java.lang.OutOfMemoryError: Java heap spaceGuillemot
Thanks for the solution. However, a few more flags will be required. --ks and --ks-key-alias with values --ks="path_to_your_keystore" and --ks-key-alias="key_alias_from_android_studio"Sericin
E
21

People have already explained on how to do this with the command-line. For completion, I thought I'd also show the way to do it via the UI in Android Studio.

When you open your "Run/Debug Configurations", you can select "APK from app bundle" (instead of "Default APK").

See screenshot:

enter image description here

Extensive answered 29/10, 2018 at 12:43 Comment(3)
Not able to build APK form aab , getting the following error "Install failed. Installation failed <a href='rerun'>Rerun</a>"Des
After Shift + F10 it will install an application to a device, not generate an apk.Melbourne
How to set the location of the aab?Kidney
F
18

Refer Geek Dashboard for more info.

For those who are looking to generate a single universal APK file from your Android App Bundle, you must use --universal flag while running the build-apks command.

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

Where bundletool.jar is the bundletool jar file you downloaded here

your_app.aab is the Android App Bundle of your App

your_app.apks is the output APKs Archive File that will be generated once you run the command.

While running the above command make sure you place bundletool.jar and your AAB file in the same folder.

Now, you need to change the your_app.apks file format to your_app.zip and extract it to find the universal.apk file

enter image description here

Note: Use –overwrite flag to overwrite the APKs file if there is already one with the same name. Otherwise, bundletool command will throw you a fatal error.

Flossieflossy answered 29/8, 2019 at 7:52 Comment(1)
getting SEVERE: Input Future failed with Error java.lang.OutOfMemoryError: Java heap space please helpGuillemot
S
17

on mac it can be easily done using homebrew

brew install bundletool

you can use the command below to generate apks

bundletool build-apks --bundle=aab_path.aab --output=release.apks

above command generates apks file which can later be extracted to give various apk files. To see all generated files change the extension from .apks to .zip and just extract the files.

then you can install apk using this command on connected device

bundletool install-apks --apks=release.apks
Stale answered 6/11, 2019 at 13:20 Comment(0)
P
13

There's a tool called bundletool, which can create APK files out of your AAB file:

Find details about this tool on bundletool (official documentation).

But here some highlights taken from that site:

Building APK files

When bundletool generates APK files from your app bundle, it includes them in a container called an APK set archive, which uses the .apks file extension. To generate an APK set for all device configurations your app supports from your app bundle, use the bundletool build-apks command, as shown below:

bundletool build-apks --bundle=/MyApp/my_app.aab --output=/MyApp/my_app.apks

Note that the command above creates an APK set of unsigned APK files. If you want to deploy the APK files to a device, you need to also include your app’s signing information, as shown in the command below.

bundletool build-apks --bundle=/MyApp/my_app.aab --output=/MyApp/my_app.apks
      --ks=/MyApp/keystore.jks
      --ks-pass=file:/MyApp/keystore.pwd
      --ks-key-alias=MyKeyAlias
      --key-pass=file:/MyApp/key.pwd

Installing APKs

bundletool install-apks --apks=/MyApp/my_app.apks

Generate a device-specific set of APKs

bundletool build-apks --connected-device --bundle=/MyApp/my_app.aab --output=/MyApp/my_app.apks
Proparoxytone answered 29/10, 2018 at 6:49 Comment(0)
E
8

I have developed a windows tool for converting .aab files to .apk in Python.

It supports creating both debug and signed apk which can be directly installed to default android device connected through USB.

It uses google’s bundle tool in the backend.

https://aabtoapkconverter.com/

Edit: The Source code is now available now on github.

This is first tool that I have developed and shared with the world. Hope it is useful. I am open to suggestions and bug reports.

enter image description here

Entelechy answered 16/3, 2022 at 14:40 Comment(6)
Is it opensource?Teflon
I will be making it's source code available on github in next 1-2 daysEntelechy
Source code is now available on github. Link is added in the answer.Entelechy
Does this make universal APKs? Don't see any option if one wants to get separate APKs or a universal oneCam
Nice little tool :)Egor
Thank you. Terminal commands were throwing annoying errors that I didn't know how to resolve. This worked just fine. @GeorgeBirbilis - You probably figured it out by now but it seems this makes a single universal .apk (Indeed, in the github code, you can see the --mode=universal flag.)Graft
W
3

Here is what i did. First thing is, i am on a Mac. So in this official guide https://reactnative.dev/docs/signed-apk-android, followed below steps.

  1. Run this command sudo keytool -genkey -v -keystore my-upload-key.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000 you can change the name of my-upload-key to your choice
  2. Setup the gradle variables as per the guide
  3. Also do the same as per the guide for point "Adding signing config to your app's Gradle config"
  4. Now rather than continuing (which will generate an AAB file) what you can do is below.

4.1 I open the same project(android folder) in Android Studio

4.2 From the Menu Options > Build > Clean Project

4.3 Click on the Make Project button and let it complete (if you can run using npm run android, then this should finish without any issues) Make Project

4.4 Once done, click on Menu options > Build > Generate Signed Bundle/APK... Generate Signed Bundle/APK...

4.5 In the next Screen Choose APK radio button and Click Next Choose APK option

4.6 In the next screen, Browse to the keystore file which you generated as the first step, its password, alias name and its password, click Next. Final Step

Wait for it to complete. You may see some errors but more importantly in the end you should get a popup in the right bottom corner saying "Locate". Click on that and you will find your app-release.apk.

Wende answered 3/6, 2021 at 9:38 Comment(0)
R
0

In Ionic Project .aab file convert to .apk by below commend

ionic cordova build android --release -- -- --packageType=apk
Responsive answered 28/8, 2023 at 19:26 Comment(0)
T
-9

I think this method much more efficient

enter image description here

Titrant answered 1/9, 2020 at 14:38 Comment(1)
Consider adding a more descriptive reply to go along with the screenshot.Snooperscope

© 2022 - 2025 — McMap. All rights reserved.