Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
3
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

@cayozin

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

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 
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
3
Help us understand the problem. What are the problem?