Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify Android App Bundle (aab) Contents before deploying

We have a build and release pipeline (Azure Devops) that deploys our APK Android app to various appcenter.ms environments. During the release process we unpack the apk, modify the contents with environment specific configuration, and then re-pack the apk.

Now that we are trying to do this with an Android App Bundle (AAB), we can no longer use apktool. How can we achieve the same thing with AAB that we could with APK?

This is a snippet of our working apk version of the script

## NOTE STUFF IS TRUNCATED!!! THIS IS NOT A COMPLETE SCRIPT!!! DO NOT COPY

brew list apktool &>/dev/null || HOMEBREW_NO_AUTO_UPDATE=1 brew install apktool
brew list xmlstarlet &>/dev/null || HOMEBREW_NO_AUTO_UPDATE=1 brew install xmlstarlet

# ... truncated ...

echo "Decompiling $zipPath"
apktool d $zipPath -o "apk"

cd apk

# ... truncated / modify androidmanifest.xml ...

unalignedPath="$apkPath.unaligned"
unsignedPath="$apkPath.unsigned"

cd ..

echo "Repackage apk to $unsignedPath"
apktool b apk -o $unsignedPath

echo "Sign"
jarsigner -keystore $keystorePath -storepass $keystorePass -keypass $keystorePass -verbose -sigalg MD5withRSA -digestalg SHA1 -signedjar $unalignedPath $unsignedPath $keyAlias
jarsigner -verify -verbose -certs $unalignedPath

echo "Zipalign"
$ANDROID_HOME/build-tools/27.0.3/zipalign -f -v 4 $unalignedPath $apkPath

When we simply change the file extension from apk to aab and run the same apktool, our folder structure is kind of messed up. folder structure when extracting apk from aab file

Also, the resources and manifest are already converted to protobuf, and I don't think I can reverse engineer them.

like image 440
Chase Florell Avatar asked Apr 27 '20 15:04

Chase Florell


People also ask

Do I need to Zipalign AAB?

For AAB files, there is no official guide to zipalign, so it is not required.

How do I test an AAB bundle?

Building on Android Studio Step 1: Go to Android Studio -> Build ->Generate Signed Bundle. Select Android App Bundle on the pop screen and select Next. Step 2: Enter the KeyStore path where the KeyStore is stored or Create a new KeyStore as seen below. Step 4: Select the release mode from the list and select finish.

What is the difference between APK and AAB?

AAB is a container that hosts a base APK and multiple split-APKs. Basically, AAB is a publishing format that a developer submits to the Play Store while APK is the packaging format for Android apps that you install on your device.

Can we install AAB file in Android device?

Using App Bundle Installer Another app that you can use to install an AAB file is called the App Bundle Installer, which, again, is available for download on the Google Play Store. Note that this is a free, ad supported app that is still in development.


2 Answers

To edit the manifest of the AAB, you'll need to extract the file base/manifest/AndroidManifest.xml from the AAB, e.g.

unzip -p app.aab base/manifest/AndroidManifest.xml > AndroidManifest.pb

At this stage, in spite of its extension, the manifest is in a protocol buffer format (this is why I gave it the extension .pb above). You'll thus then need to find a protocol buffer parser/editor to make the changes you need.

To parse the proto, you'll need the definition of the protocol buffer, which you can find in this JAR: https://maven.google.com/com/android/tools/build/aapt2-proto/3.6.3-6040484/aapt2-proto-3.6.3-6040484.jar See message XmlNode in Resources.proto

Once you've made the changes on the parsed proto, re-serialize the proto and re-inject it at the same place with the same name in the AAB (it's just a zip file).

Finally, you don't need to zip-align the AAB, so remove this step.

Maybe in the future a tool will allow do you the conversion for you automatically, similarly to what apktool does. In the meantime, you can do it manually this way. Hope that helps.

Edit by Lionscribe

Simple step by step instructions.

  1. Extract the AndroidManifest.xml file from the AAB file as instructed above. For this use, keep the name as "AndroidManifest.xml".
  2. Download protoc binary from https://developers.google.com/protocol-buffers/docs/downloads and extract binary to path or working folder.
  3. Download above referenced jar file, open jar with any zip program, and copy the files "Resources.proto" & "Configuration.proto" from the jar root folder to your working folder.
  4. Run command protoc --decode=aapt.pb.XmlNode Resources.proto < AndroidManifest.xml > output.txt to decode the file.
  5. Edit "output.txt" per your needs.
  6. Run protoc --encode=aapt.pb.XmlNode Resources.proto < output.txt > AndroidManifest_new.xml to encode the new version.
like image 52
Pierre Avatar answered Oct 13 '22 00:10

Pierre


I recently figured out a way to modify the app bundle contents. I used APK and AAB both to modify the contents of AAB.

Step1: Extract the AndroidManifest.xml file from apk using the apktool.

Step2: Modify this extracted manifest file. You can modify other resources like icons, string resources as well in this step.

Step3: Now we need to convert these modified resources into protobuf format. Please follow the below link for the same.

https://developer.android.com/studio/build/building-cmdline#bundletool-build https://musteresel.github.io/posts/2019/07/build-android-app-bundle-on-command-line.html

Step4: Unzip the contents of the app bundle and replace the existing resources(like manifest, etc.) with these modified resources.

Step5: The final trick is to use --no-dir-entries flag while zipping the contents of extracted app bundle folder which contains the modified resources. Goto the extracted app bundle folder and execute the below command.

"zip -r --no-dir-entries ../modified_aab.zip *"

Step6: And lastly, just rename .zip to .aab using simple command: mv modified_aab.zip modified_aab.aab

like image 23
Kumar Gaurav Avatar answered Oct 12 '22 23:10

Kumar Gaurav