Skip to content

Instantly share code, notes, and snippets.

@seza443
Forked from PofMagicfingers/apkdebug.sh
Last active December 1, 2018 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seza443/f3f4ce91344ba26654f8518951f27d08 to your computer and use it in GitHub Desktop.
Save seza443/f3f4ce91344ba26654f8518951f27d08 to your computer and use it in GitHub Desktop.
Enable debugging flag on an APK using apktool. Can be useful to debug cordova, etc on already compiled apps
#!/bin/sh
command -v apktool >/dev/null 2>&1 || { echo >&2 "I require apktool but it's not installed. Aborting."; exit 1; }
command -v keytool >/dev/null 2>&1 || { echo >&2 "I require keytool but it's not installed. Aborting."; exit 1; }
command -v jarsigner >/dev/null 2>&1 || { echo >&2 "I require jarsigner but it's not installed. Aborting."; exit 1; }
TMPDIR=`mktemp -d 2>/dev/null || mktemp -d -t 'apkdebug'`
APK=$1
DEBUG_APK="${APK%.*}.debug.apk"
if [ -f $APK ]; then
(echo "=> Unpacking APK..." &&
apktool -q d $APK -o $TMPDIR/app &&
echo "=> Adding debug flag..." &&
sed -i -e "s/android:debuggable=\"[^\"]*\" *//;s/<application /<application android:debuggable=\"true\" /" $TMPDIR/app/AndroidManifest.xml &&
echo "=> Repacking APK..." &&
apktool -q b $TMPDIR/app -o $DEBUG_APK &&
echo "=> Signing APK..." &&
keytool -genkey -noprompt \
-alias alias1 \
-dname "CN=Unknown, OU=Unknown, O=Unknown, L=Unknown, S=Unknown, C=Unknown" \
-keystore $TMPDIR/keystore \
-storepass password \
-keypass password \
-keysize 1024 &&
echo "=> Signing APK(2)..." &&
echo $DEBUG_APK &&
echo $TMPDIR &&
jarsigner -keystore $TMPDIR/keystore -storepass password -keypass password $DEBUG_APK alias1 > /dev/null 2>&1 &&
echo "=> Checking your debug APK..." &&
(jarsigner -verify $DEBUG_APK > /dev/null 2>&1 &&
echo "\n======" &&
echo "Success!"
echo "======\n" &&
echo "(deleting temporary directory...)\n" &&
echo "Your debug APK : $DEBUG_APK" &&
rm -rf $TMPDIR)) || (echo "=====" && echo "Something failed :'(" && echo "Leaving temporary dir $TMPDIR if you want to inspect what went wrong.")
else
echo "File not found: $APK"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment