blob: 79a5da6124486dd6816f6dc93080f09cb64cdc18 [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
Steve Kondikae76c842010-06-28 11:47:26 -040012set -e
Pat Thoyts0e7f64e2010-06-11 11:53:38 +010013QUIET=1
Steve Kondikae76c842010-06-28 11:47:26 -040014BASE=`pwd`
Ricardo Cerqueirae1f37382011-11-27 19:56:54 +000015BRUTECRUSH="-brute"
Steve Kondik47ce7ec2010-08-22 00:08:06 -040016TMPDIR=/tmp/opticharge-$$
17
Ricardo Cerqueirae1f37382011-11-27 19:56:54 +000018if [ -z "$BRUTE_PNGCRUSH" ]
19then
20 BRUTECRUSH=""
21fi
22
Steve Kondikae76c842010-06-28 11:47:26 -040023if [ "$APKCERTS" = "" ];
24then
Ricardo Cerqueirae1f37382011-11-27 19:56:54 +000025 if [ "$TARGET_BUILD_VARIANT" = "userdebug" ]; then
26 TARGET_BUILD_VARIANT="eng"
Erez A. Korn8e8a6782012-08-12 08:33:59 +030027 elif [ "$TARGET_BUILD_VARIANT" = "user" ]; then
28 TARGET_BUILD_VARIANT="eng"
Ricardo Cerqueirae1f37382011-11-27 19:56:54 +000029 fi
30
Ladios Jonquil1f765132011-09-25 02:11:08 +080031 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 -040032 if [ ! -f "$APKCERTS" ];
33 then
34 echo "Set APKCERTS to the path to your apkcerts.txt file"
35 exit 1;
36 fi
37fi
38
39if [ ! -f "$APKCERTS" ];
40then
41 echo "Invalid path to apkcerts.txt, set APKCERTS to the correct path."
42fi
43
Evan McClainf1994862011-10-28 04:34:29 -040044if [ "$(which pngcrush)" != "" ];
Steve Kondikae76c842010-06-28 11:47:26 -040045then
Evan McClainf1994862011-10-28 04:34:29 -040046 optimize_png () {
Ricardo Cerqueirae1f37382011-11-27 19:56:54 +000047 pngcrush -q ${BRUTECRUSH} $1 ${1}.out 1> /dev/null 2> /dev/null
Evan McClainf1994862011-10-28 04:34:29 -040048 mv ${1}.out ${1}
49 }
50elif [ "$(which optipng)" != "" ];
51then
52 optimize_png () {
53 optipng -o7 -quiet $1 1> /dev/null 2> /dev/null
54 }
55elif [ "$(which pngout-static)" != "" ];
56then
57 optimize_png () {
58 pngout-static $1
59 }
60elif [ "$(which pngout)" != "" ];
61then
62 optimize_png () {
63 pngout $1
64 }
65else
66 echo "Please install pngcrush, optipng, or pngout"
Steve Kondikae76c842010-06-28 11:47:26 -040067 exit 1;
68fi
69
70if [ "`which aapt`" = "" ];
71then
72 echo "Please ensure aapt is in your \$PATH"
73 exit 1;
74fi
75
76if [ "`which zipalign`" = "" ];
77then
78 echo "Please ensure zipalign is in your \$PATH"
79 exit 1;
80fi
81
82if [ -e "$1" ];
83then
84 NAME=`basename $1`;
85 echo "Optimizing $NAME...";
86
87 if [ "$2" != "" ];
88 then
89 CERT=build/target/product/security/$2.x509.pem
90 KEY=build/target/product/security/$2.pk8
91 if [ ! -f "$ANDROID_BUILD_TOP/$CERT" ];
92 then
93 echo "$CERT does not exist!";
94 exit 1;
95 fi
96 else
97 APKINFO=`grep "name=\"$NAME\"" $APKCERTS`;
Pat Thoyts0e7f64e2010-06-11 11:53:38 +010098 [ $QUIET ] || echo "APKINFO: $APKINFO";
Steve Kondikae76c842010-06-28 11:47:26 -040099 if [ "$APKINFO" = "" ];
100 then
101 echo "No apk info for $NAME";
102 exit 1;
103 fi
104 CERT=`echo $APKINFO | awk {'print $2'} | cut -f 2 -d "=" | tr -d "\""`;
105 KEY=`echo $APKINFO | awk {'print $3'} | cut -f 2 -d "=" | tr -d "\""`;
106 if [ "$CERT" = "" ];
107 then
108 echo "Unable to find certificate for $NAME"
109 exit 1;
110 fi
111 fi
112
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100113 [ $QUIET ] || echo "Certificate: $CERT";
Steve Kondikae76c842010-06-28 11:47:26 -0400114
Steve Kondik47ce7ec2010-08-22 00:08:06 -0400115 [ -d $TMPDIR/$NAME ] && rm -rf $TMPDIR/$NAME
116 mkdir -p $TMPDIR/$NAME
117 trap "rm -rf $TMPDIR; exit" INT TERM EXIT
118 cd $TMPDIR/$NAME
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100119 unzip -q $BASE/$1
Steve Kondikae76c842010-06-28 11:47:26 -0400120 for x in `find . -name "*.png" | grep -v "\.9.png$" | tr "\n" " "`
121 do
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100122 [ $QUIET ] || echo "Crushing $x"
Evan McClainf1994862011-10-28 04:34:29 -0400123 optimize_png $x
Steve Kondikae76c842010-06-28 11:47:26 -0400124 done
125 cp $BASE/$1 $BASE/$1.old
126
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100127 [ $QUIET ] || echo "Repacking apk.."
Christopher Laisc557d072011-01-05 15:10:17 -0600128 aapt p -0 .dat -0 .dict -F $NAME .
Steve Kondikae76c842010-06-28 11:47:26 -0400129
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100130 [ $QUIET ] || echo "Resigning with cert: `echo $CERT`"
Steve Kondikae76c842010-06-28 11:47:26 -0400131
Chirayu Desaid1dbaf32012-08-15 16:30:23 +0530132 [ $QUIET ] || echo java -jar $ANDROID_HOST_OUT/framework/signapk.jar $ANDROID_BUILD_TOP/$CERT $ANDROID_BUILD_TOP/$KEY $NAME signed_$NAME
133 java -jar $ANDROID_HOST_OUT/framework/signapk.jar $ANDROID_BUILD_TOP/$CERT $ANDROID_BUILD_TOP/$KEY $NAME signed_$NAME
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100134 [ $QUIET ] || echo "Zipalign.."
Steve Kondikae76c842010-06-28 11:47:26 -0400135 zipalign -f 4 signed_$NAME $BASE/$1
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100136 if [ ! $QUIET ]; then
137 ls -l $BASE/$1.old
138 ls -l $BASE/$1
139 fi
Steve Kondikae76c842010-06-28 11:47:26 -0400140 rm $BASE/$1.old
141else
142 echo "Usage: $0 [apk file]"
143fi
144