blob: b4345c4e391a40f8c7f900ac36464149e32f0acc [file] [log] [blame]
Steve Kondikae76c842010-06-28 11:47:26 -04001#!/bin/sh
2#
3# Super-mega opticharger of doom
Evan McClainf1994862011-10-28 04:34:29 -04004# Shrinks apks by running pngcrush or optipng or pngout on png images
Steve Kondikae76c842010-06-28 11:47:26 -04005#
6# Point APKCERTS at the full path to a generated apkcerts.txt file, such as:
7# /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
8#
9# cyanogen - shade@chemlab.org
10# ChrisSoyars - me@ctso.me
11
12OUT_TARGET_HOST=$(uname -a | grep Darwin)
13if [ -z "$OUT_TARGET_HOST" ]
14then
15 OUT_TARGET_HOST=linux-x86
16else
17 OUT_TARGET_HOST=darwin-x86
18fi
19
20set -e
Pat Thoyts0e7f64e2010-06-11 11:53:38 +010021QUIET=1
Steve Kondikae76c842010-06-28 11:47:26 -040022BASE=`pwd`
Steve Kondik47ce7ec2010-08-22 00:08:06 -040023TMPDIR=/tmp/opticharge-$$
24
Steve Kondikae76c842010-06-28 11:47:26 -040025if [ "$APKCERTS" = "" ];
26then
Ladios Jonquil1f765132011-09-25 02:11:08 +080027 APKCERTS=$OUT/obj/PACKAGING/target_files_intermediates/$TARGET_PRODUCT-target_files-$TARGET_BUILD_VARIANT.$USER/META/apkcerts.txt
Steve Kondikae76c842010-06-28 11:47:26 -040028 if [ ! -f "$APKCERTS" ];
29 then
30 echo "Set APKCERTS to the path to your apkcerts.txt file"
31 exit 1;
32 fi
33fi
34
35if [ ! -f "$APKCERTS" ];
36then
37 echo "Invalid path to apkcerts.txt, set APKCERTS to the correct path."
38fi
39
Evan McClainf1994862011-10-28 04:34:29 -040040if [ "$(which pngcrush)" != "" ];
Steve Kondikae76c842010-06-28 11:47:26 -040041then
Evan McClainf1994862011-10-28 04:34:29 -040042 optimize_png () {
43 pngcrush -q -brute $1 ${1}.out 1> /dev/null 2> /dev/null
44 mv ${1}.out ${1}
45 }
46elif [ "$(which optipng)" != "" ];
47then
48 optimize_png () {
49 optipng -o7 -quiet $1 1> /dev/null 2> /dev/null
50 }
51elif [ "$(which pngout-static)" != "" ];
52then
53 optimize_png () {
54 pngout-static $1
55 }
56elif [ "$(which pngout)" != "" ];
57then
58 optimize_png () {
59 pngout $1
60 }
61else
62 echo "Please install pngcrush, optipng, or pngout"
Steve Kondikae76c842010-06-28 11:47:26 -040063 exit 1;
64fi
65
66if [ "`which aapt`" = "" ];
67then
68 echo "Please ensure aapt is in your \$PATH"
69 exit 1;
70fi
71
72if [ "`which zipalign`" = "" ];
73then
74 echo "Please ensure zipalign is in your \$PATH"
75 exit 1;
76fi
77
78if [ -e "$1" ];
79then
80 NAME=`basename $1`;
81 echo "Optimizing $NAME...";
82
83 if [ "$2" != "" ];
84 then
85 CERT=build/target/product/security/$2.x509.pem
86 KEY=build/target/product/security/$2.pk8
87 if [ ! -f "$ANDROID_BUILD_TOP/$CERT" ];
88 then
89 echo "$CERT does not exist!";
90 exit 1;
91 fi
92 else
93 APKINFO=`grep "name=\"$NAME\"" $APKCERTS`;
Pat Thoyts0e7f64e2010-06-11 11:53:38 +010094 [ $QUIET ] || echo "APKINFO: $APKINFO";
Steve Kondikae76c842010-06-28 11:47:26 -040095 if [ "$APKINFO" = "" ];
96 then
97 echo "No apk info for $NAME";
98 exit 1;
99 fi
100 CERT=`echo $APKINFO | awk {'print $2'} | cut -f 2 -d "=" | tr -d "\""`;
101 KEY=`echo $APKINFO | awk {'print $3'} | cut -f 2 -d "=" | tr -d "\""`;
102 if [ "$CERT" = "" ];
103 then
104 echo "Unable to find certificate for $NAME"
105 exit 1;
106 fi
107 fi
108
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100109 [ $QUIET ] || echo "Certificate: $CERT";
Steve Kondikae76c842010-06-28 11:47:26 -0400110
Steve Kondik47ce7ec2010-08-22 00:08:06 -0400111 [ -d $TMPDIR/$NAME ] && rm -rf $TMPDIR/$NAME
112 mkdir -p $TMPDIR/$NAME
113 trap "rm -rf $TMPDIR; exit" INT TERM EXIT
114 cd $TMPDIR/$NAME
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100115 unzip -q $BASE/$1
Steve Kondikae76c842010-06-28 11:47:26 -0400116 for x in `find . -name "*.png" | grep -v "\.9.png$" | tr "\n" " "`
117 do
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100118 [ $QUIET ] || echo "Crushing $x"
Evan McClainf1994862011-10-28 04:34:29 -0400119 optimize_png $x
Steve Kondikae76c842010-06-28 11:47:26 -0400120 done
121 cp $BASE/$1 $BASE/$1.old
122
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100123 [ $QUIET ] || echo "Repacking apk.."
Christopher Laisc557d072011-01-05 15:10:17 -0600124 aapt p -0 .dat -0 .dict -F $NAME .
Steve Kondikae76c842010-06-28 11:47:26 -0400125
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100126 [ $QUIET ] || echo "Resigning with cert: `echo $CERT`"
Steve Kondikae76c842010-06-28 11:47:26 -0400127
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100128 [ $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
Steve Kondikae76c842010-06-28 11:47:26 -0400129 java -jar $ANDROID_BUILD_TOP/out/host/$OUT_TARGET_HOST/framework/signapk.jar $ANDROID_BUILD_TOP/$CERT $ANDROID_BUILD_TOP/$KEY $NAME signed_$NAME
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100130 [ $QUIET ] || echo "Zipalign.."
Steve Kondikae76c842010-06-28 11:47:26 -0400131 zipalign -f 4 signed_$NAME $BASE/$1
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100132 if [ ! $QUIET ]; then
133 ls -l $BASE/$1.old
134 ls -l $BASE/$1
135 fi
Steve Kondikae76c842010-06-28 11:47:26 -0400136 rm $BASE/$1.old
137else
138 echo "Usage: $0 [apk file]"
139fi
140