How to use the command 'bundletool' (with examples)

How to use the command 'bundletool' (with examples)

The ‘bundletool’ command-line tool is used to manipulate Android Application Bundles. It provides several subcommands for various actions related to Android Application Bundles, such as generating APKs, installing APKs to an emulator or device, estimating download size, generating device specification JSON files, and verifying bundles.

Use case 1: Display help for a subcommand

Code:

bundletool help subcommand

Motivation:
This use case is helpful when you want to get detailed information about a specific subcommand of ‘bundletool’. The ‘help’ subcommand displays the usage documentation for the specified subcommand, providing insights into its functionality and available options.

Explanation:

  • bundletool help: The main command to display help for a subcommand.
  • subcommand: The name of the subcommand for which you want to view the help information. Replace ‘subcommand’ with the actual name of the subcommand.

Example output:

$ bundletool help build-apks

Usage: bundletool build-apks [options]

Options:
  --bundle=<bundle.aab>    Path to the Android App Bundle to generate APKs for.
  --output=<file.apks>     Path to where the generated APKs should be saved.
  --ks=<key.keystore>      The path to the keystore file.
  --ks-key-alias=<alias>   The alias of the key in the keystore file.

Use case 2: Generate APKs from an application bundle (prompts for keystore password)

Code:

bundletool build-apks --bundle=path/to/bundle.aab --ks=path/to/key.keystore --ks-key-alias=key_alias --output=path/to/file.apks

Motivation:
Generating APKs from an application bundle is useful when you need individual APK files for various purposes like publishing to different app stores or distributing the APKs directly to users. This command allows you to provide the path to the application bundle file, the keystore file, and other necessary information to generate APKs.

Explanation:

  • bundletool build-apks: The main command to generate APKs from an application bundle.
  • --bundle=path/to/bundle.aab: Specifies the path to the Android App Bundle (.aab) file.
  • --ks=path/to/key.keystore: Specifies the path to the keystore file used for signing the APKs.
  • --ks-key-alias=key_alias: Specifies the alias of the key in the keystore file.
  • --output=path/to/file.apks: Specifies the path where the generated APKs should be saved.

Example output:

$ bundletool build-apks --bundle=app_bundle.aab --ks=keystore.jks --ks-key-alias=my_key --output=apks_folder/output.apks
Building APKs...
Completed successfully!
APKs generated at: apks_folder/output.apks

Use case 3: Generate APKs from an application bundle giving the keystore password

Code:

bundletool build-apks --bundle=path/to/bundle.aab --ks=path/to/key.keystore --ks-key-alias=key_alias –ks-pass=pass:the_password --output=path/to/file.apks

Motivation:
If you want to automate the APK generation process by providing the keystore password in the command itself, this use case is helpful. It eliminates the need for manual entry of the keystore password during the process.

Explanation:

  • --ks-pass=pass:the_password: Specifies the keystore password as the argument. Replace ’the_password’ with the actual password of the keystore.
  • The rest of the arguments and their meanings are the same as in Use case 2.

Example output:

$ bundletool build-apks --bundle=app_bundle.aab --ks=keystore.jks --ks-key-alias=my_key –ks-pass=pass:my_password --output=apks_folder/output.apks
Building APKs...
Completed successfully!
APKs generated at: apks_folder/output.apks

Use case 4: Generate APKs including only one single APK for universal usage

Code:

bundletool build-apks --bundle=path/to/bundle.aab --mode=universal --ks=path/to/key.keystore --ks-key-alias=key_alias --output=path/to/file.apks

Motivation:
Generating a universal APK that is compatible with all Android devices can be useful for certain use cases. This command enables you to generate a single APK file that can be installed on any Android device.

Explanation:

  • --mode=universal: Specifies the mode for generating the APKs as ‘universal’, ensuring that only one single APK is generated.
  • The rest of the arguments and their meanings are the same as in Use case 2.

Example output:

$ bundletool build-apks --bundle=app_bundle.aab --mode=universal --ks=keystore.jks --ks-key-alias=my_key --output=apks_folder/output.apks
Building APKs...
Completed successfully!
APKs generated at: apks_folder/output.apks

Use case 5: Install the right combination of APKs to an emulator or device

Code:

bundletool install-apks --apks=path/to/file.apks

Motivation:
When you have generated APKs using ‘bundletool’, you can use this command to seamlessly install the APKs to an emulator or physical Android device. It ensures that the right combination of APKs is installed to achieve the desired functionality.

Explanation:

  • bundletool install-apks: The main command to install APKs to an emulator or device.
  • --apks=path/to/file.apks: Specifies the path to the APKs file to be installed. Replace ‘path/to/file.apks’ with the actual path to the APKs file.

Example output:

$ bundletool install-apks --apks=apks_folder/output.apks
Installing APKs...
Successfully installed APKs from apks_folder/output.apks

Use case 6: Estimate the download size of an application

Code:

bundletool get-size total --apks=path/to/file.apks

Motivation:
Understanding the download size of an application is crucial, especially when optimizing the APK size for better user experience. With this command, you can estimate the total download size of the application based on the APKs generated by ‘bundletool’.

Explanation:

  • bundletool get-size total: The main command to estimate the download size of an application.
  • --apks=path/to/file.apks: Specifies the path to the APKs file generated by ‘bundletool’.

Example output:

$ bundletool get-size total --apks=apks_folder/output.apks
The estimated download size of the application is 50 MB.

Use case 7: Generate a device specification JSON file for an emulator or device

Code:

bundletool get-device-spec --output=path/to/file.json

Motivation:
When developing or testing an application, having a device specification file is essential to simulate specific device configurations. This command generates a JSON file containing device specifications for an emulator or physical device.

Explanation:

  • bundletool get-device-spec: The main command to generate a device specification JSON file.
  • --output=path/to/file.json: Specifies the path where the device specification JSON file should be saved.

Example output:

$ bundletool get-device-spec --output=specs_folder/spec.json
Generated device specification JSON file at specs_folder/spec.json

Use case 8: Verify a bundle and display detailed information about it

Code:

bundletool validate --bundle=path/to/bundle.aab

Motivation:
Validating the Android App Bundle file is essential to ensure its correctness and to identify any issues before using it for APK generation or distribution. This command allows you to verify a bundle and obtain detailed information about it.

Explanation:

  • bundletool validate: The main command to validate a bundle and display detailed information.
  • --bundle=path/to/bundle.aab: Specifies the path to the Android App Bundle (.aab) file to be validated.

Example output:

$ bundletool validate --bundle=app_bundle.aab
Validating the bundle file...
The bundle file is valid.

Metadata:
- Version: 2.0.0
- Number of modules: 3

...

Conclusion

The ‘bundletool’ command-line tool provides various use cases for manipulating Android Application Bundles. By following the examples provided in this article, you can generate APKs, install them to emulators or devices, estimate download sizes, generate device specification files, validate bundles, and more. Utilizing ‘bundletool’ can streamline your Android app development and distribution processes.

Related Posts

How to use the command 'yaa' (with examples)

How to use the command 'yaa' (with examples)

The command ‘yaa’ is a command line tool that allows users to create and manipulate YAA (Yet Another Archive) files.

Read More
How to use the command `hcloud` (with examples)

How to use the command `hcloud` (with examples)

The command hcloud is a CLI (Command Line Interface) tool for Hetzner Cloud.

Read More
How to use the command "pio test" (with examples)

How to use the command "pio test" (with examples)

This article will illustrate various use cases of the command “pio test” in PlatformIO.

Read More