bundletool is the underlying tool that Android Studio, the Android Gradle plugin, and Google Play use to build an Android App Bundle, and convert an app bundle into the various APKs that are deployed to devices. Android SDK Bundles and their APKs are built with bundletool as well. bundletool is also available to you as a command line tool, so you can build app bundles and SDK bundles yourself, and recreate Google Play's server-side build of your app's APKs or your runtime-enabled SDK's APKs. Show
Download bundletoolIf you haven't already done so, download bundletool from the GitHub repository. Build and test an app bundleYou can use Android Studio or the bundletool command line tool to build your Android App Bundle and test generating APKs from this app bundle. Build an app bundleYou should use Android Studio and the Android plugin for Gradle to build and sign an Android App Bundle. However, if using the IDE is not an option (for example, because you're 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 bundleAfter you build your Android App Bundle, you should test how Google Play uses it to generate APKs and how those APKs behave when deployed to a device. There are two ways you should consider testing your app bundle: locally using the bundletool command line tool and through Google Play by uploading your bundle to the Play Console and 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 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.apksIf you want to deploy the APKs to a device, you need to also include your app's signing information, as shown in the command below. If you do not 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.pwdThe table below describes the various flags and options you can set when using the bundletool build-apks command in greater detail.
Deploy APKs to a connected deviceAfter 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 looks for a compatible multi-APK and deploys it 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 below. (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.apksGenerate a device-specific set of APKsIf you'd rather not 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 below. (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.apksGenerate and use device specification JSON filesbundletool is capable of generating 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.jsonbundletool creates a JSON file for your device in the directory the tool is located. You can then pass it 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.apksManually create a device specification JSONIf you don't have access to the device for which you want to build a targeted APK set (for example, a friend wants 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. 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.jsonMeasure the estimated download sizes of APKs in an APK setTo 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.apksYou can modify the behavior of the get-size total command using the following flags:
Build and test an app bundle with an SDK bundle dependency (experimental)You can use bundletool to build an Android App Bundle with an Android SDK Bundle dependency and test generating APKs from it. Build an app bundle with an SDK bundle dependencyYou can build your Android App Bundle with an Android SDK Bundle dependency from the command line and sign it using jarsigner. Each app bundle module includes a Module Protocol Buffer (.pb) file: runtime_enabled_sdk_config.pb. This file contains the list of SDKs that an app bundle module depends on. For the full definition of this file, see the runtime_enabled_sdk_config.proto file. To build an app bundle with an SDK bundle dependency, follow the same guide to build an app bundle using bundletool, but this time ensure that you add a runtime_enabled_sdk_config.pb file to each app module's zip file with compiled code and resources. Some notable fields in the runtime_enabled_sdk_config.pb file:
An SDK can appear only in one module; if multiple modules depend on the same SDK, this dependency should be deduplicated and moved to the base module. Different modules can't depend on different versions of the SDK. Generate APKs from an app bundle with an SDK bundle dependencyTo generate APKs from your app bundle, you can follow the same steps as in Generate a set of APKs from your app bundle or Generate a device-specific set of APKs, but this time you should provide the bundletool build-apks command with the SDKs the app depends on. These SDKs can be provided in SDK bundle format or SDK Archive format. You can provide the SDKs as SDK bundles by adding the --sdk-bundles flag, as follows: bundletool build-apks --bundle=app.aab --sdk-bundles=sdk1.asb,sdk2.asb \ --output=app.apksYou can provide the SDKs as SDK archives by adding the --sdk-archives flag, as follows: bundletool build-apks --bundle=app.aab --sdk-archives=sdk1.asar,sdk2.asar \ --output=app.apksBuild and test an SDK bundle (experimental)You can use bundletool to build an Android SDK Bundle and test generating the files needed for installation and distribution. Build an SDK bundleYou can build your Android SDK Bundle (ASB) from the command line and sign it using jarsigner. To build an SDK bundle, follow these steps:
The table below describes the various flags and options you can set when using the bundletool build-sdk-bundle command in greater detail.
Generate APKs from an SDK bundleAfter you build your Android SDK Bundle, you can test an SDK bundle locally by generating its APKs using the bundletool build-sdk-apks command, as shown in the following code. When bundletool generates APKs from your SDK bundle, it includes them in a container called an APK set archive, which uses the .apks file extension. bundletool generates a single standalone APK from the SDK bundle that targets all device configurations. bundletool build-sdk-apks --sdk-bundle=sdk.asb --output=sdk.apksIf you want to deploy the ASB to a device, you need to also 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-sdk-apks --sdk-bundle=sdk.asb --output=sdk.apks \ --ks=keystore.jks \ --ks-pass=file:/keystore.pwd \ --ks-key-alias=KeyAlias \ --key-pass=file:/key.pwdThe table below describes the various flags and options you can set when using the bundletool build-sdk-apks command in greater detail.
Generate an SDK Archive from an SDK bundleAfter you upload your Android SDK Bundle to your distribution channel, for example Google Play, the Android SDK bundle is transformed into an Android SDK Archive (.asar) for distribution to app developers through Maven. For more details about the format, see the SDK Archive format specification. After you build your Android SDK Bundle, you can test the generation of an Android SDK Archive locally using the bundletool build-sdk-asar command, as shown in the following code. bundletool build-sdk-asar --sdk-bundle=sdk.asb --output=sdk.asar \ --apk-signing-key-certificate=keycert.txtThe table below describes the various flags and options you can set when using the bundletool build-sdk-asar command in greater detail.
Runtime-enabled SDK formats (experimental)Runtime-enabled SDKs introduce two Android file formats: the Android SDK Bundle (.asb), used to publish the runtime-enabled SDK to app stores, and the Android SDK Archive (.asar), used to distribute the runtime-enabled SDK on Maven. The Android SDK Bundle formatAn SDK Bundle is a publishing format for runtime-enabled SDKs. It contains all the SDK code and resources, including the code from any libraries the SDK depends on. It doesn't include the code and resources of other runtime-enabled SDKs that the SDK depends on. An Android SDK Bundle is a signed zip file that contains the extension .asb. The SDK code and resources are organized similarly to what you would find in an APK. An ASB also contains several configuration files that are used to aid the generation of the installable APKs. Figure 1. The contents of an Android SDK Bundle.The following list describes some of the Android SDK Bundle files in more detail:
The Android SDK Archive formatAn Android SDK Archive is the distribution format of a runtime-enabled SDK on Maven. It's a zip file that contains the file extension .asar. It contains all information that is needed by the app build tools to generate an Android App Bundle that depends on your runtime-enabled SDK. Figure 2. The contents of an Android SDK Archive Bundle.The following list describes some of the Android SDK Bundle files in more detail:
Additional resourcesTo learn more about using bundletool, try the following resource. Codelabs
|