blob: 1207b6bab1ac6e909d5aa687b2a5955c03ac9830 [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`
Ricardo Cerqueirae1f37382011-11-27 19:56:54 +000023BRUTECRUSH="-brute"
Steve Kondik47ce7ec2010-08-22 00:08:06 -040024TMPDIR=/tmp/opticharge-$$
25
Ricardo Cerqueirae1f37382011-11-27 19:56:54 +000026if [ -z "$BRUTE_PNGCRUSH" ]
27then
28 BRUTECRUSH=""
29fi
30
Steve Kondikae76c842010-06-28 11:47:26 -040031if [ "$APKCERTS" = "" ];
32then
Ricardo Cerqueirae1f37382011-11-27 19:56:54 +000033 if [ "$TARGET_BUILD_VARIANT" = "userdebug" ]; then
34 TARGET_BUILD_VARIANT="eng"
Erez A. Korn8e8a6782012-08-12 08:33:59 +030035 elif [ "$TARGET_BUILD_VARIANT" = "user" ]; then
36 TARGET_BUILD_VARIANT="eng"
Ricardo Cerqueirae1f37382011-11-27 19:56:54 +000037 fi
38
Ladios Jonquil1f765132011-09-25 02:11:08 +080039 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 -040040 if [ ! -f "$APKCERTS" ];
41 then
42 echo "Set APKCERTS to the path to your apkcerts.txt file"
43 exit 1;
44 fi
45fi
46
47if [ ! -f "$APKCERTS" ];
48then
49 echo "Invalid path to apkcerts.txt, set APKCERTS to the correct path."
50fi
51
Evan McClainf1994862011-10-28 04:34:29 -040052if [ "$(which pngcrush)" != "" ];
Steve Kondikae76c842010-06-28 11:47:26 -040053then
Evan McClainf1994862011-10-28 04:34:29 -040054 optimize_png () {
Ricardo Cerqueirae1f37382011-11-27 19:56:54 +000055 pngcrush -q ${BRUTECRUSH} $1 ${1}.out 1> /dev/null 2> /dev/null
Evan McClainf1994862011-10-28 04:34:29 -040056 mv ${1}.out ${1}
57 }
58elif [ "$(which optipng)" != "" ];
59then
60 optimize_png () {
61 optipng -o7 -quiet $1 1> /dev/null 2> /dev/null
62 }
63elif [ "$(which pngout-static)" != "" ];
64then
65 optimize_png () {
66 pngout-static $1
67 }
68elif [ "$(which pngout)" != "" ];
69then
70 optimize_png () {
71 pngout $1
72 }
73else
74 echo "Please install pngcrush, optipng, or pngout"
Steve Kondikae76c842010-06-28 11:47:26 -040075 exit 1;
76fi
77
78if [ "`which aapt`" = "" ];
79then
80 echo "Please ensure aapt is in your \$PATH"
81 exit 1;
82fi
83
84if [ "`which zipalign`" = "" ];
85then
86 echo "Please ensure zipalign is in your \$PATH"
87 exit 1;
88fi
89
90if [ -e "$1" ];
91then
92 NAME=`basename $1`;
93 echo "Optimizing $NAME...";
94
95 if [ "$2" != "" ];
96 then
97 CERT=build/target/product/security/$2.x509.pem
98 KEY=build/target/product/security/$2.pk8
99 if [ ! -f "$ANDROID_BUILD_TOP/$CERT" ];
100 then
101 echo "$CERT does not exist!";
102 exit 1;
103 fi
104 else
105 APKINFO=`grep "name=\"$NAME\"" $APKCERTS`;
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100106 [ $QUIET ] || echo "APKINFO: $APKINFO";
Steve Kondikae76c842010-06-28 11:47:26 -0400107 if [ "$APKINFO" = "" ];
108 then
109 echo "No apk info for $NAME";
110 exit 1;
111 fi
112 CERT=`echo $APKINFO | awk {'print $2'} | cut -f 2 -d "=" | tr -d "\""`;
113 KEY=`echo $APKINFO | awk {'print $3'} | cut -f 2 -d "=" | tr -d "\""`;
114 if [ "$CERT" = "" ];
115 then
116 echo "Unable to find certificate for $NAME"
117 exit 1;
118 fi
119 fi
120
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100121 [ $QUIET ] || echo "Certificate: $CERT";
Steve Kondikae76c842010-06-28 11:47:26 -0400122
Steve Kondik47ce7ec2010-08-22 00:08:06 -0400123 [ -d $TMPDIR/$NAME ] && rm -rf $TMPDIR/$NAME
124 mkdir -p $TMPDIR/$NAME
125 trap "rm -rf $TMPDIR; exit" INT TERM EXIT
126 cd $TMPDIR/$NAME
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100127 unzip -q $BASE/$1
Steve Kondikae76c842010-06-28 11:47:26 -0400128 for x in `find . -name "*.png" | grep -v "\.9.png$" | tr "\n" " "`
129 do
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100130 [ $QUIET ] || echo "Crushing $x"
Evan McClainf1994862011-10-28 04:34:29 -0400131 optimize_png $x
Steve Kondikae76c842010-06-28 11:47:26 -0400132 done
133 cp $BASE/$1 $BASE/$1.old
134
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100135 [ $QUIET ] || echo "Repacking apk.."
Christopher Laisc557d072011-01-05 15:10:17 -0600136 aapt p -0 .dat -0 .dict -F $NAME .
Steve Kondikae76c842010-06-28 11:47:26 -0400137
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100138 [ $QUIET ] || echo "Resigning with cert: `echo $CERT`"
Steve Kondikae76c842010-06-28 11:47:26 -0400139
Chirayu Desaic10f6d52012-08-11 16:34:32 +0530140 [ $QUIET ] || echo java -jar $OUT/../../../host/$OUT_TARGET_HOST/framework/signapk.jar $ANDROID_BUILD_TOP/$CERT $ANDROID_BUILD_TOP/$KEY $NAME signed_$NAME
141 java -jar $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 +0100142 [ $QUIET ] || echo "Zipalign.."
Steve Kondikae76c842010-06-28 11:47:26 -0400143 zipalign -f 4 signed_$NAME $BASE/$1
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100144 if [ ! $QUIET ]; then
145 ls -l $BASE/$1.old
146 ls -l $BASE/$1
147 fi
Steve Kondikae76c842010-06-28 11:47:26 -0400148 rm $BASE/$1.old
149else
150 echo "Usage: $0 [apk file]"
151fi
152