bundletool is the underlying tool that Android Studio, the Android Gradle
plugin, and Google Play use to build an Android App Bundle. bundletool
can convert an app bundle into the various APKs that are deployed to devices.
Android SDK Bundles (ASBs) and their APKs are built with bundletool. It is
also available as a command-line tool, so you can build app bundles and
SDK bundles yourself and re-create Google Play's server-side build of your app's
APKs or your
runtime-enabled SDK's APKs.
Download bundletool
If you haven't already, download bundletool from the
GitHub repository.
Build and test an app bundle
You can use Android Studio or the bundletool command-line tool to build your
Android App Bundle and then test generating APKs from this app bundle.
Build an app bundle
Use Android Studio and the Android Gradle plugin to
build and sign an Android App Bundle.
However, if using the IDE is not an option—for example, because you are using
a continuous build server—you can also
build your app bundle from the command line
and sign it using
jarsigner.
For more information about building app bundles with bundletool,
see
Build an app bundle using bundletool.
Generate a set of APKs from your app bundle
After you build your Android App Bundle, test how Google Play uses it to generate APKs and how those APKs behave when deployed to a device.
There are two ways you can test your app bundle:
- Use the
bundletoolcommand-line tool locally. - Upload your bundle to the Play Console through Google Play by using a test track.
This section explains how to use bundletool to test your app bundle locally.
When bundletool generates APKs from your app bundle, it includes the generated
APKs 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:
bundletool build-apks --bundle=/MyApp/my_app.aab --output=/MyApp/my_app.apks
If you want to deploy the APKs to a device, you also need to include your app's
signing information, as shown in the following command. If you don't specify
signing information, bundletool attempts to sign your APKs with a debug key
for you.
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
The following table describes the various flags and options you can set when
using the bundletool build-apks command in greater detail:
Table 1. Options for the
bundletool build-apks command
Deploy APKs to a connected device
After you generate a set of APKs, bundletool can deploy the right
combination of APKs from that set to a connected device.
For example, if you have a connected device running Android 5.0 (API level 21)
or higher, bundletool pushes the base APK, feature module APKs, and
configuration APKs required to run your app on that device. Alternatively, if
your connected device is running Android 4.4 (API level 20) or lower,
bundletool searches for a compatible multi-APK to deploy to your device.
To deploy your app from an APK set, use the install-apks command and specify
the path of the APK set using the
--apks=/path/to/apks flag, as
shown in the following command. If you have multiple devices connected,
specify a target device by adding the
--device-id=serial-id flag.
bundletool install-apks --apks=/MyApp/my_app.apks
Generate a device-specific set of APKs
If you don't want to build a set of APKs for all device configurations your app
supports, you can build APKs that target only the configuration of a connected
device using the --connected-device option, as shown in the following command.
If you have multiple devices connected, specify a target device by including the
--device-id=serial-id flag.
bundletool build-apks --connected-device --bundle=/MyApp/my_app.aab --output=/MyApp/my_app.apks
Generate and use device specification JSON files
bundletool can generate an APK set that targets a device
configuration specified by a JSON file. To first generate a JSON file for a
connected device, run the following command:
bundletool get-device-spec --output=/tmp/device-spec.json
bundletool creates a JSON file for your device in the tool's directory. You
can then pass the file to bundletool to generate a set of APKs that
target only the configuration described in that JSON file, as follows:
bundletool build-apks --device-spec=/MyApp/pixel2.json --bundle=/MyApp/my_app.aab --output=/MyApp/my_app.apks
Manually create a device specification JSON
If you don't have access to the device for which you want to build a targeted APK set—for example, if you want to try your app with a device you don't have on hand—you can manually create a JSON file using the following format:
{
"supportedAbis": ["arm64-v8a", "armeabi-v7a"],
"supportedLocales": ["en", "fr"],
"screenDensity": 640,
"sdkVersion": 27
}
You can then pass this JSON to the bundle extract-apks command, as described
in the previous section.
Extract device-specific APKs from an existing APK set
If you have an existing APK set and you want to extract from it a subset of APKs
that target a specific device configuration, you can use the extract-apks
command and specify a device specification JSON, as follows:
bundletool extract-apks --apks=/MyApp/my_existing_APK_set.apks --output-dir=/MyApp/my_pixel2_APK_set.apks --device-spec=/MyApp/bundletool/pixel2.json
Measure the estimated download sizes of APKs in an APK set
To measure the estimated download sizes of APKs in an APK set as they would
be served compressed over the wire, use the get-size total command:
bundletool get-size total --apks=/MyApp/my_app.apks
You can modify the behavior of the get-size total command using the
following flags:
Table 2. Options for the
get-size total command
Additional resources
To learn more about using bundletool, watch
App Bundles: Testing bundles with bundletool and the Play Console.
