Cherry referring to my last name Forward and Reverse Engineering

Re-signing of APKs for Debugging in Android 10+

Because I keep forgetting this useful procedure: The following is a shell script relying on AndroidManifestEditor to enable the FLAG_DEBUGGABLE flag for an APK file to make it debuggable via jdwp and ptrace. I call this approach “minimal invasive” because it works without decoding more parts of the APK file than necessary. This produces a signature following APK Signature Scheme v2, to ensure compatibility with Android 10+.

# Optional: Generate new keystore for signing
keytool -genkey -v -keystore my.keystore -alias tempkey -keyalg RSA -keysize 2048 -validity 1000

# Unpack
unzip <your-apk>.apk -d tmp

# Enable debugging (don't use the -o option to set input = output)
java -jar ~/opt/ManifestEditor/ManifestEditor-1.0.2.jar tmp/AndroidManifest.xml -d 1 --force
mv tmp/AndroidManifest-new.xml tmp/AndroidManifest.xml

# Re-pack
cd tmp
zip -Z store -r app-debug.apk $(find . -maxdepth 1 -mindepth 1 | grep -v META-INF)
zipalign -p -f -v 4 app-debug.apk app-debug-aligned.apk

# Re-sign
apksigner sign --v2-signing-enabled --ks my.keystore app-debug-aligned.apk

Be aware, though, that the code inside the APK can detect the modification (for example by reading getContext().getApplicationInfo().flags) and react in malicious ways.