How to Create Android App Bundle (.aab) for Cordova Apps Using Gradle
Currently, Google recommends Android app developers to use the Android App Bundle (.aab) instead of using APKs. In this tutorial, we will try to create an Android App Bundle from an application that was created using Cordova.
Introduction
Cordova is an open-source framework that can be used to build web-based applications using HTML5, CSS3 and JavaScript that can be compiled into Android and iOS-based applications. Therefore, Cordova is included in the multi-platform hybrid script.
To use this framework, we first have to install Node.Js and NPM as the basis for using the command-line. After we finish installing Node.Js and NPM, then we install Cordova with the command below :
npm install -g cordova
Don’t forget to check again whether Cordova is installed on our system or not with the command below :
cordova --version
Creating Android Apps Using Cordova
First of all, create a folder for the Android application.
cordova create AppAndroid
After the application folder is successfully created, go into the folder.
cd AppAndroid
In this folder you can start customizing the needs of the Android application you want. Don’t forget to put the Android platform into the folder, so that later you can compile the application into an Android App Bundle.
cordova platform add android
Set Environment
First make sure you have installed and registered the location of the Java Environment and Android SDK into the system Path.
export ANDROID_HOME={path_to}/Android/sdk
export PATH=${PATH}:${ANDROID_HOME}/tools
export PATH=${PATH}:${ANDROID_HOME}/platform-tools
export ANDROID_SDK_ROOT={path_to}/Android/sdk
We also need to download and install Gradle before build Android application.
Create Android App Bundle Using Gradle
After you do the environment settings, build your Android application with Cordova CLI.
cordova build android
Then open the platforms/android folder.
cd platforms/android
In this folder, you will find the gradlew file which we can then run to create the .aab file for your Android application using the following command :
gradlew bundle
Note: For those of you Linux or Mac users, you must run it with the following command :
./gradlew bundle
After that, wait a while until the process of creating the .aab file for your Android app is complete. You can view the .aab files for your Android apps in the platforms/android/app/build/outputs/bundle/release directory.
Create a signed Android App Bundle
Next we need to create a keystore for the Android App Bundle that we have created with the following steps.
keytool -genkey -v -keystore <keystoreName>.keystore -alias <Keystore AliasName> -keyalg <Key algorithm> -keysize <Key size> -validity <Key Validity in Days>
Then the keystore has been generated with name as <keystoreName>.keystore. Please save the keystore, alias name and password to sign your Android application.
To sign the unsigned Android App Bundle, run the jarsigner tool which is also included in the JDK :
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore <keystorename> <Unsigned AAB file> <Keystore Alias name>
To optimize your Android App Bundle file before distributing it to end-users you need to use zipalign to ensures that all uncompressed files in the archive are aligned relative to the start of the file. You will find zipAlign in the android build tools then use it like so :
'{path_to_android_build_tools}/zipalign' -v 4 <unzip_aab_file>.aab <new_zip_aab_file>.aab
Congratulation, your Android App Bundle is already to publish on PlayStore!
If you have any suggestions for the next article please leave a comment :)