Skip to content

Instantly share code, notes, and snippets.

@vavrecan
Last active March 20, 2022 08:53
Show Gist options
  • Save vavrecan/8596303 to your computer and use it in GitHub Desktop.
Save vavrecan/8596303 to your computer and use it in GitHub Desktop.
Script that allows renaming namespace of Android APK so you can install same application multiple times
#!/bin/bash
# author Marek Vavrecan (vavrecan@gmail.com)
# show usage
[ $# -eq 0 ] && { echo "Usage: $0 [apk path] [source namespace] [target namespace]"; exit 1; }
APK_PATH="$1"
NAMESPACE_FROM="$2"
NAMESPACE_TO="$3"
# escape .
NAMESPACE_REPLACE=$(echo "s/$NAMESPACE_FROM/$NAMESPACE_TO/g" | sed -e 's/\./\\\./g')
# change to smalli support com.namespace to com/namespace
NAMESPACE_SLASHES_REPLACE=$(echo "s/$NAMESPACE_FROM/$NAMESPACE_TO/g" | sed -e 's/\./\\\//g')
if [ -f "apktool.jar" ];
then
else
echo "Please get apktool from http://code.google.com/p/android-apktool/ to continue"
exit 1;
fi
java -jar apktool.jar d -o out -f $APK_PATH
# replace tricks
find ./out -type f -exec sed -i $NAMESPACE_REPLACE {} \;
find ./out -type f -exec sed -i $NAMESPACE_SLASHES_REPLACE {} \;
find ./out -type f -exec sed -i 's/<string name="app_name">\(.*\)<\/string>/<string name="app_name">\1 Beta<\/string>/g' {} \;
mv out/smali/com/youcognize out/smali/com/youcognizebeta
# compile
java -jar apktool.jar b -o ./debug.apk -f ./out
# sign it again
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore key.keystore ./debug.apk "password" -storepass "password" -keypass "password"
# cleanup
rm -R ./out
# to get key
# keytool -genkey -v -keystore key.keystore -alias password -keyalg RSA -keysize 2048 -validity 10000
# this might be needed to fix align, but was not needed so far
# zipalign -v 4 debug.apk debug2.apk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment