-
Notifications
You must be signed in to change notification settings - Fork 9
/
apk-launcher-icon-extract
executable file
·135 lines (87 loc) · 2.45 KB
/
apk-launcher-icon-extract
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env bash
shopt -s nullglob
if [ -z "$1" ]; then
echo "usage: $(basename "$0") apk"
exit 1
fi
function require() { hash "$@" || exit 127; }
require unzip
require apktool
require xmllint
function match_vector() {
GLOB="$1"
GLOB="${GLOB/@/res\/}"
GLOB="${GLOB/mipmap/mipmap*}"
GLOB="${GLOB}*.xml"
MATCHES=($2/$GLOB)
echo "${MATCHES[0]}"
return
}
function match_raster() {
densities=(xxxhdpi xxhdpi xhdpi hdpi mdpi)
if [[ $1 =~ ^@drawable ]]; then
for d in "${densities[@]}"; do
GLOB="$1"
GLOB="${GLOB/@/res\/}"
GLOB="${GLOB/drawable/drawable*}"
GLOB="${GLOB}.png"
MATCHES=($2/$GLOB)
if [ ${#MATCHES[@]} -gt 0 ]; then
echo "${MATCHES[0]}"
return
fi
done
elif [[ $1 =~ ^@mipmap ]]; then
for d in "${densities[@]}"; do
GLOB="$1"
GLOB="${GLOB/@/res\/}"
GLOB="${GLOB/mipmap/mipmap-${d}*}"
GLOB="${GLOB}*.png"
MATCHES=($2/$GLOB)
if [ ${#MATCHES[@]} -gt 0 ]; then
echo "${MATCHES[0]}"
return
fi
done
fi
}
function extract_resource() {
DIR="$1"
REFERENCE="$2"
PREFIX="$3"
VECTOR_ICON=$(match_vector "$REFERENCE" "$DIR")
RASTER_ICON=$(match_raster "$REFERENCE" "$DIR")
if [ -n "$VECTOR_ICON" ]; then
FILENAME="$PREFIX.xml"
xmllint --format "$VECTOR_ICON" > "$FILENAME"
echo "Saved $FILENAME ($REFERENCE resolved to $VECTOR_ICON)"
elif [ -n "$RASTER_ICON" ]; then
FILENAME="$PREFIX.png"
cp "$RASTER_ICON" "$FILENAME"
echo "Saved $FILENAME ($REFERENCE resolved to $RASTER_ICON)"
fi
}
if [[ $1 == *.zip ]]; then
TMPDIR=$(mktemp -d)
trap 'rm -rf -- "$TMPDIR"' EXIT
unzip -q "$1" -d "$TMPDIR"
BASEAPK=$(directory-base-split "$TMPDIR")
if [ ! -f "${BASEAPK[0]}" ]; then
echo "error: *_base_split.apk not found in zip, aborting"
exit 1
fi
else
BASEAPK="$1"
fi
TMPDIR=$(mktemp -d)
apktool d "$BASEAPK" -f -s -o "$TMPDIR" > /dev/null 2>&1
extract_resource \
"$TMPDIR" \
"$(xpath -n -q -e 'string(/manifest/application/@android:icon)' < "$TMPDIR"/AndroidManifest.xml)" \
"$(basename "$1" .zip)-launcher-icon"
if [[ -n "$(xpath -n -q -e 'string(/manifest/application/@android:roundIcon)' < "$TMPDIR"/AndroidManifest.xml)" ]]; then
extract_resource \
"$TMPDIR" \
"$(xpath -n -q -e 'string(/manifest/application/@android:roundIcon)' < "$TMPDIR"/AndroidManifest.xml)" \
"$(basename "$1" .zip)-launcher-icon-round"
fi