Modify Android App Bundle (aab) Contents before deploying
Asked Answered
T

4

10

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.

Thorne answered 27/4, 2020 at 15:49 Comment(1)
Did you make some progress with this ? I am also looking for something similar. @chase-florellCountdown
C
15

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.
Checkbook answered 27/4, 2020 at 17:20 Comment(18)
I'm trying to do your suggestion. Do you have any other links or guides to this? I'm having trouble finding a good resource for this.Trantrance
Hence the part of my answer: "You'll then need to find a protocol buffer parser/editor to make the changes you need."Checkbook
@Trantrance Did you make some progress ?Countdown
@Countdown I made some progress but still couldn't find a proto parser. I just stayed on APKs.Trantrance
@Checkbook why is it not possible to add this functionality to the bundle tool itself. it would help lot of people with their CI/ CD flow on the app release.Countdown
I would suggest you file a feature request in bundletool's GitHub project with what you need, why you need it, and why it can't be done otherwise.Checkbook
Could this be applied to remove an AAB's signature?Theology
@Checkbook The command you mentioned protoc --decode=aapt.pb.XmlNode Resources.proto < AndroidManifest.xml works perfectly for android manifest.xml but not with resources.pb any solution to this?Godforsaken
Sweet! The proto message for the resource table is different. From memory, it's probably something like aapt.pb.ResourceTable but I can check later if that doesn't workCheckbook
When encoding I get the message: input:164:13: Expected identifier, got: 6 Checking on that line I see: ref { id: 2131296256 name: "mipmap/ic_launcher" 6: 1 } Any way to sort this?Friable
Fixed it, to get latest aapt2-proto: mvnrepository.com/artifact/com.android.tools.build/aapt2-protoFriable
This is the most comprehensive way I find out. But got error, $ ./protoc --encode=aapt.pb.XmlNode Resources.proto < output.txt > AndroidManifest_new.xml input:2217:13: Expected identifier, got: 6 Failed to parse input. compiled_item { ref { id: 2131689472 name: "mipmap/ic_launcher" 6: 1 } }Morphine
Someone got error when encoding, see my post: https://mcmap.net/q/1051728/-modify-android-app-bundle-aab-contents-before-deployingRhythmics
no need resign the packageDealt
I follow this step by step, as close as possible. But if I try upload result aab, I got " File 'BundleConfig.pb' was not found.". I did reproduce locally the same error bundletool build-apks. I did try --no-dir-entries while zip aab after editing. No luck...Swagerty
I figured out difference: you actually have to zip from the inside of a folder like this $ zip -r --no-dir-entries ../modified_aab.zip *. if you call zip like this - $ zip -r --no-dir-entries ./modified_aab.zip ./aab_content/* - you will get "File 'BundleConfig.pb' was not found." error.Swagerty
Yes, it works but I got "invalid signature" while trying to upload to Google Play. How to fix this?Abloom
@Abloom see https://mcmap.net/q/36041/-your-android-app-bundle-is-signed-with-the-wrong-key-ensure-that-your-app-bundle-is-signed-with-the-correct-signing-key-and-try-againCheckbook
M
5

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

Maliamalice answered 11/7, 2021 at 8:14 Comment(3)
I want to make change my app name for that I need to modify my resources.pb file which is compiled and can be edited without doing Serialization do you have any way to edit it?Godforsaken
+1 for the --no-dir-entries flag, this was causing my aab's to not get recognised. As soon as that flag is set, everything works and I can use bundletool to create apk's which I can deploy :)Passepartout
It is critical to call zip form the inside of a folder. If you call it like this - $ zip -r --no-dir-entries ./modified_aab.zip ./aab_content/* - you will get error - "File 'BundleConfig.pb' was not found.". Just hope it might save other people some headache.Swagerty
R
3

@Pierre gave a good answer by this comment: Modify Android App Bundle (aab) Contents before deploying but someone got stuck when encoding output file to new AndroidManifest.xml file.

The reason is: using Out of Date proto files.

In order to get latest version or specific version you want:

  1. Go to https://maven.google.com/web/index.html#com.android.tools.build:aapt2-proto
  2. Navigate to the version you want (for example: https://maven.google.com/web/index.html#com.android.tools.build:aapt2-proto:8.1.2-10154469)
  3. In Artifact(s) section, click on "Jar" and download (for example: https://dl.google.com/android/maven2/com/android/tools/build/aapt2-proto/8.1.2-10154469/aapt2-proto-8.1.2-10154469.jar )
  4. Unzip the downloaded file, copy the proto files and follow the answer I mentioned above: Modify Android App Bundle (aab) Contents before deploying

enter image description here

Rhythmics answered 17/10, 2023 at 14:14 Comment(0)
S
0

To edit the rest of the files

protoc --decode=aapt.pb.ResourceTable Resources.proto < resources.pb > output.txt

and of course

protoc --encode=aapt.pb.ResourceTable Resources.proto < output.txt > resources.pb
Shitty answered 25/4, 2023 at 3:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.