To build the modified aapt2 binaries used by Apktool, you must first obtain the AOSP source and integrate the modified frameworks/base repository. The frameworks/base repository contains specific changes (like disabling optimizations) to ensure Apktool keeps APKs as close to the original as possible.
1. Get the modified frameworks/base repo
Use the platform_frameworks_base repository. Branches follow the Android version naming convention (e.g., apktool_7.1 for Android 7.1).
To include the modified package in your AOSP source:
cd frameworks/basegit remote add origin git@github.com:iBotPeaches/platform_frameworks_base.gitgit fetch origin -vgit checkout origin/apktool-{x} (replace {x} with the target version)
2. Prepare AOSP Source
Download the AOSP source (requires 150-250GB). To optimize the clone size, use:
# Partial clone
~/bin/repo init -u https://android.googlesource.com/platform/manifest -b android16-release --partial-clone
# Sync only the current branch
repo sync -c
3. Build the aapt2 binary
Steps vary by operating system. Ensure you have performed the initial AOSP build first.
Linux / Windows
source build/envsetup.sh
lunch aosp_cf_x86_64_only_phone-aosp_current-eng
m aapt2
strip out/host/linux-x86/bin/aapt2
strip out/host/linux-x86/bin/aapt2_64
strip out/host/windows-x86/bin/aapt2.exe
strip out/host/windows-x86/bin/aapt2_64.exe
Mac
Note: If building on a recent macOS, you may need to patch build/soong/cc/config/darwin_host.go by adding your current SDK version to the darwinSupportedSdkVersions array.
export ANDROID_JAVA_HOME=/Path/To/Jdk
source build/envsetup.sh
lunch aosp_cf_x86_64_only_phone-aosp_current-eng
m aapt2
strip out/host/darwin-x86/bin/aapt2_64
4. Verify Static Builds
To ensure dependencies like libc++ are built statically and not shared, verify the binaries using:
- Unix:
ldd <binary_path> - Mac:
otool -L <binary_path>
source build/envsetup.sh
lunch aosp_cf_x86_64_only_phone-aosp_current-eng
m aapt2
strip out/host/linux-x86/bin/aapt2