2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Bash script to convert Android bundle .aab to .apk, .apks, .zip and ADB device install

Posted at

In Android Studio, we prepare an App for public release by creating a signed .aab bundle file. The bash script below (tested in Unix/Mac OS) performs the following functions:

  • converts .aab bundle located in the "release" folder to an .apks file
  • extracts a universal .apk file, which can be run on any device (useful if you send it to multiple testers)
  • compresses all files in an encrypted folder using zip
  • installs the .apks on a connected device (optional)

The script accepts the following arguments when run in the terminal:

  1. the name of the files to be created
  2. (Optional) The address of a connected Android device to which the .apks file can be installed. The address can be obtained by running "adb devices" in the command line.

The script has the following placeholders, which should be replaced for your specific environment/application:

  • {PASSWORD}, {KEYSTORE_FILE}, {ALIAS} are the details used to sign the .aab in Android Studio
  • {PATH_TO_BUNDLE_TOOL} is the path of the bundletool e.g. ~/Downloads/bundletool-all-0.8.0.jar

The bundletool can be obtained from:
https://developer.android.com/studio/command-line/bundletool

#!/bin/sh

# convert default .aab file to other products
# - apks to install in device directly
# - universal apk to install in device

if [ "$#" -lt 1 ]; then
    echo "Illegal number of parameters - required"
    echo "1. name"
    echo "(optional) 2. device (from adb devices)"
    exit
fi

default_name=app
target_name=$1

folder=release
mode=universal
apk_container=apk_container

default_aab=$folder/$default_name.aab
default_apks=$folder/$default_name.apks
default_apk=$folder/$apk_container/$mode.apk

target_aab=$folder/$target_name.aab
target_apks=$folder/$target_name.apks
target_apk=$folder/$apk_container/$target_name.apk

password={PASSWORD}
keystore={KEYSTORE_FILE}
alias={ALIAS}


if [ -f $default_aab ]; then
	java -jar {PATH_TO_BUNDLE_TOOL} build-apks --overwrite --mode=$mode --bundle=$default_aab --output=$default_apks --ks=$keystore --ks-key-alias=$alias --ks-pass=pass:$password
else
	echo "cannot find $default_aab"
fi

if [ -f $default_apks ]; then
	unzip $default_apks -d $folder/$apk_container
else 
	echo "cannot find $default_apks"
fi

if [ ! -f $target_aab ] && [ -f $default_aab ]; then
	mv -f $default_aab $target_aab
else 
	echo "cannot find $default_aab or target $target_aab already exists"
fi

if [ ! -f $target_apks ] && [ -f $default_apks ]; then
	mv -f $default_apks $target_apks
fi

if [ ! -f $target_apk ] && [ -f $default_apk ]; then
	mv -f $default_apk $target_apk
fi

if [ "$#" -eq 2 ] && [ -f $target_apks ]; then
	device=$2
	java -jar {PATH_TO_BUNDLE_TOOL} install-apks --apks=$target_apks --device-id=$device
fi

zip -er $folder/$target_name.zip $folder/$apk_container 
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?