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:
aapt2andbundletool.aapt2is the second version of the Android Asset Packaging Tool. It’s available (I used manual download from Maven) here. The downloaded jar file contained theaapt2executable.bundletoolis 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_resourcesThis fills the
compiled_resourcesdirectory with files such aslayout_activity_main.xml.flat.“Link” the resources into a temporary APK, generating the
R.javafile 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 genThis creates the
temporary.apkwhich 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.jarfor the target platform (part of the Android SDK).Compile the Java source files. Since
R.javais 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 \ gen/**/*.java project/app/src/main/java/**/*.javaThis creates
.classfiles in theclassesdirectory. Depending on dependencies the classpath needs to include other paths/jars.Extract the previously generated temporary APK:
unzip temporary.apk -d stagingAn APK is nothing but a fancy zip file, so this puts all contents of the
temporary.apkinto thestagingdirectory.Prepare files to be bundled as the base module. The end result is a
stagingdirectory which can be zipped as a base module. First step is to move thestaging/AndroidManifest.xmlintostaging/manifest/(a directory to be created). Next I createstating/dex/and usedxto convert the Java bytecode (in the.classfiles) 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.aabSign 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