-
Notifications
You must be signed in to change notification settings - Fork 8
/
patch-apk
executable file
·46 lines (45 loc) · 1.18 KB
/
patch-apk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#! /bin/sh
IFS=
for f in $ANDROID_SDK_ROOT/build-tools/* $SDK_TOOLS; do
test -d "$f" && sdk_tools=$f
done
: ${sdk_tools:?please set the ANDROID_SDK_ROOT or SDK_TOOLS envvar}
unset IFS
android(){ PATH=$sdk_tools:$PATH "$@"; }
tmpdir=$(mktemp -u)
keystore=$tmpdir/keystore
keyname=keyname
keypass=${KEYPASS:-qwertyuiop} # LOL
keep=0
apktool=apktool
for a; do
case $1 in
*=*) eval "$1" ;;
*) set -- "$@" "$1" ;;
esac
shift
done
set -e
old_apk=$1
new_apk=${2:-${1%.apk}-new.apk}
trap '[ "$keep" = 1 ] && echo "==> $tmpdir" || rm -fr "$tmpdir"' EXIT INT TERM
mkdir -p "$tmpdir"
printf '%s\n' "$tmpdir"
dir=$tmpdir/d
set -x
apktool d -o "$dir" "$old_apk"
(cd "$dir" && patch -sp1)
for f in "$dir/assets/"*.pem; do
openssl x509 -in "$f" -outform DER -out "${f%.pem}.crt"
done
apktool b -o "$new_apk" "$dir"
export KEYPASS=$keypass
if ! [ -r "$keystore" ]; then
keytool -genkey -v -keystore "$keystore" \
-alias "$keyname" -dname "CN=$keyname" \
-storepass:env KEYPASS \
-keyalg RSA -keysize 2048 -validity 10000
fi
android zipalign -f 4 "$new_apk" "$tmpdir/apk" && mv "$tmpdir/apk" "$new_apk"
android apksigner sign -ks "$keystore" --ks-pass=env:KEYPASS "$new_apk"
printf '\nnew apk in %s\n' "$new_apk"