androidandroid-studioapkandroid-app-bundle

Generate an APK file from an AAB file (Android app bundle)


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


Solution

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