blob: 9e1af13dd06f533807da0e35342b5bf2fcd483be (
plain)
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
|
#!/bin/sh
#
# Super-mega opticharger of doom
# Shrinks apks by running pngcrush on png images
#
# Point APKCERTS at the full path to a generated apkcerts.txt file, such as:
# /home/shade/dev/sources/android-cm-eclair/out/target/product/dream_sapphire/obj/PACKAGING/target_files_intermediates/cyanogen_dream_sapphire-target_files-eng.shade/META/apkcerts.txt
#
# cyanogen - shade@chemlab.org
# ChrisSoyars - me@ctso.me
OUT_TARGET_HOST=$(uname -a | grep Darwin)
if [ -z "$OUT_TARGET_HOST" ]
then
OUT_TARGET_HOST=linux-x86
else
OUT_TARGET_HOST=darwin-x86
fi
set -e
QUIET=1
QFLAG=-q
BASE=`pwd`
TMPDIR=/tmp/opticharge-$$
if [ "$APKCERTS" = "" ];
then
APKCERTS=$OUT/obj/PACKAGING/target_files_intermediates/$TARGET_PRODUCT-target_files-$TARGET_BUILD_VARIANT.$LOGNAME/META/apkcerts.txt
if [ ! -f "$APKCERTS" ];
then
echo "Set APKCERTS to the path to your apkcerts.txt file"
exit 1;
fi
fi
if [ ! -f "$APKCERTS" ];
then
echo "Invalid path to apkcerts.txt, set APKCERTS to the correct path."
fi
if [ "`which pngcrush`" = "" ];
then
echo "Please install pngcrush"
exit 1;
fi
if [ "`which aapt`" = "" ];
then
echo "Please ensure aapt is in your \$PATH"
exit 1;
fi
if [ "`which zipalign`" = "" ];
then
echo "Please ensure zipalign is in your \$PATH"
exit 1;
fi
if [ -e "$1" ];
then
NAME=`basename $1`;
echo "Optimizing $NAME...";
if [ "$2" != "" ];
then
CERT=build/target/product/security/$2.x509.pem
KEY=build/target/product/security/$2.pk8
if [ ! -f "$ANDROID_BUILD_TOP/$CERT" ];
then
echo "$CERT does not exist!";
exit 1;
fi
else
APKINFO=`grep "name=\"$NAME\"" $APKCERTS`;
[ $QUIET ] || echo "APKINFO: $APKINFO";
if [ "$APKINFO" = "" ];
then
echo "No apk info for $NAME";
exit 1;
fi
CERT=`echo $APKINFO | awk {'print $2'} | cut -f 2 -d "=" | tr -d "\""`;
KEY=`echo $APKINFO | awk {'print $3'} | cut -f 2 -d "=" | tr -d "\""`;
if [ "$CERT" = "" ];
then
echo "Unable to find certificate for $NAME"
exit 1;
fi
fi
[ $QUIET ] || echo "Certificate: $CERT";
[ -d $TMPDIR/$NAME ] && rm -rf $TMPDIR/$NAME
mkdir -p $TMPDIR/$NAME
trap "rm -rf $TMPDIR; exit" INT TERM EXIT
cd $TMPDIR/$NAME
unzip -q $BASE/$1
for x in `find . -name "*.png" | grep -v "\.9.png$" | tr "\n" " "`
do
[ $QUIET ] || echo "Crushing $x"
pngcrush $QFLAG $x $x.crushed 1>/dev/null
if [ -e "$x.crushed" ];
then
mv $x.crushed $x
fi
done
cp $BASE/$1 $BASE/$1.old
[ $QUIET ] || echo "Repacking apk.."
aapt p -0 res/raw -0 res/raw-en -F $NAME .
[ $QUIET ] || echo "Resigning with cert: `echo $CERT`"
[ $QUIET ] || echo java -jar $ANDROID_BUILD_TOP/out/host/$OUT_TARGET_HOST/framework/signapk.jar $ANDROID_BUILD_TOP/$CERT $ANDROID_BUILD_TOP/$KEY $NAME signed_$NAME
java -jar $ANDROID_BUILD_TOP/out/host/$OUT_TARGET_HOST/framework/signapk.jar $ANDROID_BUILD_TOP/$CERT $ANDROID_BUILD_TOP/$KEY $NAME signed_$NAME
[ $QUIET ] || echo "Zipalign.."
zipalign -f 4 signed_$NAME $BASE/$1
if [ ! $QUIET ]; then
ls -l $BASE/$1.old
ls -l $BASE/$1
fi
rm $BASE/$1.old
else
echo "Usage: $0 [apk file]"
fi
|