blob: c8c86f5ae5e415ae658224a805e283a02a095b53 [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"
35 fi
36
Ladios Jonquil1f765132011-09-25 02:11:08 +080037 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 -040038 if [ ! -f "$APKCERTS" ];
39 then
40 echo "Set APKCERTS to the path to your apkcerts.txt file"
41 exit 1;
42 fi
43fi
44
45if [ ! -f "$APKCERTS" ];
46then
47 echo "Invalid path to apkcerts.txt, set APKCERTS to the correct path."
48fi
49
Evan McClainf1994862011-10-28 04:34:29 -040050if [ "$(which pngcrush)" != "" ];
Steve Kondikae76c842010-06-28 11:47:26 -040051then
Evan McClainf1994862011-10-28 04:34:29 -040052 optimize_png () {
Ricardo Cerqueirae1f37382011-11-27 19:56:54 +000053 pngcrush -q ${BRUTECRUSH} $1 ${1}.out 1> /dev/null 2> /dev/null
Evan McClainf1994862011-10-28 04:34:29 -040054 mv ${1}.out ${1}
55 }
56elif [ "$(which optipng)" != "" ];
57then
58 optimize_png () {
59 optipng -o7 -quiet $1 1> /dev/null 2> /dev/null
60 }
61elif [ "$(which pngout-static)" != "" ];
62then
63 optimize_png () {
64 pngout-static $1
65 }
66elif [ "$(which pngout)" != "" ];
67then
68 optimize_png () {
69 pngout $1
70 }
71else
72 echo "Please install pngcrush, optipng, or pngout"
Steve Kondikae76c842010-06-28 11:47:26 -040073 exit 1;
74fi
75
76if [ "`which aapt`" = "" ];
77then
78 echo "Please ensure aapt is in your \$PATH"
79 exit 1;
80fi
81
82if [ "`which zipalign`" = "" ];
83then
84 echo "Please ensure zipalign is in your \$PATH"
85 exit 1;
86fi
87
88if [ -e "$1" ];
89then
90 NAME=`basename $1`;
91 echo "Optimizing $NAME...";
92
93 if [ "$2" != "" ];
94 then
95 CERT=build/target/product/security/$2.x509.pem
96 KEY=build/target/product/security/$2.pk8
97 if [ ! -f "$ANDROID_BUILD_TOP/$CERT" ];
98 then
99 echo "$CERT does not exist!";
100 exit 1;
101 fi
102 else
103 APKINFO=`grep "name=\"$NAME\"" $APKCERTS`;
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100104 [ $QUIET ] || echo "APKINFO: $APKINFO";
Steve Kondikae76c842010-06-28 11:47:26 -0400105 if [ "$APKINFO" = "" ];
106 then
107 echo "No apk info for $NAME";
108 exit 1;
109 fi
110 CERT=`echo $APKINFO | awk {'print $2'} | cut -f 2 -d "=" | tr -d "\""`;
111 KEY=`echo $APKINFO | awk {'print $3'} | cut -f 2 -d "=" | tr -d "\""`;
112 if [ "$CERT" = "" ];
113 then
114 echo "Unable to find certificate for $NAME"
115 exit 1;
116 fi
117 fi
118
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100119 [ $QUIET ] || echo "Certificate: $CERT";
Steve Kondikae76c842010-06-28 11:47:26 -0400120
Steve Kondik47ce7ec2010-08-22 00:08:06 -0400121 [ -d $TMPDIR/$NAME ] && rm -rf $TMPDIR/$NAME
122 mkdir -p $TMPDIR/$NAME
123 trap "rm -rf $TMPDIR; exit" INT TERM EXIT
124 cd $TMPDIR/$NAME
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100125 unzip -q $BASE/$1
Steve Kondikae76c842010-06-28 11:47:26 -0400126 for x in `find . -name "*.png" | grep -v "\.9.png$" | tr "\n" " "`
127 do
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100128 [ $QUIET ] || echo "Crushing $x"
Evan McClainf1994862011-10-28 04:34:29 -0400129 optimize_png $x
Steve Kondikae76c842010-06-28 11:47:26 -0400130 done
131 cp $BASE/$1 $BASE/$1.old
132
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100133 [ $QUIET ] || echo "Repacking apk.."
Christopher Laisc557d072011-01-05 15:10:17 -0600134 aapt p -0 .dat -0 .dict -F $NAME .
Steve Kondikae76c842010-06-28 11:47:26 -0400135
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100136 [ $QUIET ] || echo "Resigning with cert: `echo $CERT`"
Steve Kondikae76c842010-06-28 11:47:26 -0400137
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100138 [ $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 -0400139 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 +0100140 [ $QUIET ] || echo "Zipalign.."
Steve Kondikae76c842010-06-28 11:47:26 -0400141 zipalign -f 4 signed_$NAME $BASE/$1
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100142 if [ ! $QUIET ]; then
143 ls -l $BASE/$1.old
144 ls -l $BASE/$1
145 fi
Steve Kondikae76c842010-06-28 11:47:26 -0400146 rm $BASE/$1.old
147else
148 echo "Usage: $0 [apk file]"
149fi
150