Build an Android App Bundle (*.aab) from the command line
2019-07-11
Here’s how I’m building an Android App Bundle (*.aab) file from the command line, without Gradle:
Get the necessary tools:
aapt2
andbundletool
.aapt2
is the second version of the Android Asset Packaging Tool. It’s available (I used manual download from Maven) here. The downloaded jar file contained theaapt2
executable.bundletool
is used to work with App bundles, available from it’s GitHub repository. I downloadedbundletool-all-0.10.0.jar
, which can be run withjava -jar path/to/above/bundletool.jar
. Apart from these two the following steps needjavac
(the Java compiler),jarsigner
(part of the JDK for me),dx
(part of the Android SDK build tools; used to convert Java bytecode to Dalvik bytecode) andzip
/unzip
.“Compile” all resources using
aapt2
: Every resource file of the base module needs to be compiled:aapt2 compile project/app/src/main/res/**/* -o compiled_resources
This fills the
compiled_resources
directory with files such aslayout_activity_main.xml.flat
.“Link” the resources into a temporary APK, generating the
R.java
file along the way and converting the resources into protobuf format:aapt2 link --proto-format -o temporary.apk \ -I android_sdk/platforms/android-NN/android.jar \ --manifest project/app/src/main/AndroidManifest.xml \ -R compiled_resources/*.flat \ --auto-add-overlay --java gen
This creates the
temporary.apk
which contains the resources and the manifest in protobuf (Google Protocol Buffers) format and also generatesR.java
(undergen/my/package/R.java
) which is used by Java code to reference resources. It needs to “include” (it doesn’t really contain it, just cross-checks references) theandroid.jar
for the target platform (part of the Android SDK).Compile the Java source files. Since
R.java
is now generated, I can compile the Java sources:javac -source 1.7 -target 1.7 \ -bootclasspath $JAVA_HOME/jre/lib/rt.jar \ -classpath android_sdk/platforms/android-NN/android.jar \ -d classes \ **/*.java project/app/src/main/java/**/*.java gen/
This creates
.class
files in theclasses
directory. Depending on dependencies the classpath needs to include other paths/jars.Extract the previously generated temporary APK:
unzip temporary.apk -d staging
An APK is nothing but a fancy zip file, so this puts all contents of the
temporary.apk
into thestaging
directory.Prepare files to be bundled as the base module. The end result is a
staging
directory which can be zipped as a base module. First step is to move thestaging/AndroidManifest.xml
intostaging/manifest/
(a directory to be created). Next I createstating/dex/
and usedx
to convert the Java bytecode (in the.class
files) to Dalvik bytecode (suitable for running on Android):dx --dex --output=staging/dex/classes.dex classes/
Create a zip file of the contents of the base module:
(cd staging; zip -r ../base.zip *)
Build the bundle:
bundletool build-bundle --modules=base.zip --output=bundle.aab
Sign it:
jarsigner -keystore mykeystore.jks bundle.aab my-id
Done :)
Notes:
Versions I used:
aapt2-3.4.1-5326820-linux.jar
,bundletool-all-0.10.0.jar
, build-tools28.0.3
, platform version28
,javac 1.8.0_212
(openjdk)References: Android APK from command line, Build bundle with bundletool