blob: f8a9d8af5bfe48517b567758a95cb2512eff04ae [file] [log] [blame]
Steve Kondikae76c842010-06-28 11:47:26 -04001#!/bin/sh
2#
3# Super-mega opticharger of doom
4# Shrinks apks by running pngcrush on png images
5#
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
22QFLAG=-q
Steve Kondikae76c842010-06-28 11:47:26 -040023BASE=`pwd`
Steve Kondik47ce7ec2010-08-22 00:08:06 -040024TMPDIR=/tmp/opticharge-$$
25
Steve Kondikae76c842010-06-28 11:47:26 -040026if [ "$APKCERTS" = "" ];
27then
Ladios Jonquil1f765132011-09-25 02:11:08 +080028 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 -040029 if [ ! -f "$APKCERTS" ];
30 then
31 echo "Set APKCERTS to the path to your apkcerts.txt file"
32 exit 1;
33 fi
34fi
35
36if [ ! -f "$APKCERTS" ];
37then
38 echo "Invalid path to apkcerts.txt, set APKCERTS to the correct path."
39fi
40
41if [ "`which pngcrush`" = "" ];
42then
43 echo "Please install pngcrush"
44 exit 1;
45fi
46
47if [ "`which aapt`" = "" ];
48then
49 echo "Please ensure aapt is in your \$PATH"
50 exit 1;
51fi
52
53if [ "`which zipalign`" = "" ];
54then
55 echo "Please ensure zipalign is in your \$PATH"
56 exit 1;
57fi
58
59if [ -e "$1" ];
60then
61 NAME=`basename $1`;
62 echo "Optimizing $NAME...";
63
64 if [ "$2" != "" ];
65 then
66 CERT=build/target/product/security/$2.x509.pem
67 KEY=build/target/product/security/$2.pk8
68 if [ ! -f "$ANDROID_BUILD_TOP/$CERT" ];
69 then
70 echo "$CERT does not exist!";
71 exit 1;
72 fi
73 else
74 APKINFO=`grep "name=\"$NAME\"" $APKCERTS`;
Pat Thoyts0e7f64e2010-06-11 11:53:38 +010075 [ $QUIET ] || echo "APKINFO: $APKINFO";
Steve Kondikae76c842010-06-28 11:47:26 -040076 if [ "$APKINFO" = "" ];
77 then
78 echo "No apk info for $NAME";
79 exit 1;
80 fi
81 CERT=`echo $APKINFO | awk {'print $2'} | cut -f 2 -d "=" | tr -d "\""`;
82 KEY=`echo $APKINFO | awk {'print $3'} | cut -f 2 -d "=" | tr -d "\""`;
83 if [ "$CERT" = "" ];
84 then
85 echo "Unable to find certificate for $NAME"
86 exit 1;
87 fi
88 fi
89
Pat Thoyts0e7f64e2010-06-11 11:53:38 +010090 [ $QUIET ] || echo "Certificate: $CERT";
Steve Kondikae76c842010-06-28 11:47:26 -040091
Steve Kondik47ce7ec2010-08-22 00:08:06 -040092 [ -d $TMPDIR/$NAME ] && rm -rf $TMPDIR/$NAME
93 mkdir -p $TMPDIR/$NAME
94 trap "rm -rf $TMPDIR; exit" INT TERM EXIT
95 cd $TMPDIR/$NAME
Pat Thoyts0e7f64e2010-06-11 11:53:38 +010096 unzip -q $BASE/$1
Steve Kondikae76c842010-06-28 11:47:26 -040097 for x in `find . -name "*.png" | grep -v "\.9.png$" | tr "\n" " "`
98 do
Pat Thoyts0e7f64e2010-06-11 11:53:38 +010099 [ $QUIET ] || echo "Crushing $x"
100 pngcrush $QFLAG $x $x.crushed 1>/dev/null
Steve Kondikae76c842010-06-28 11:47:26 -0400101 if [ -e "$x.crushed" ];
102 then
103 mv $x.crushed $x
104 fi
105 done
106 cp $BASE/$1 $BASE/$1.old
107
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100108 [ $QUIET ] || echo "Repacking apk.."
Christopher Laisc557d072011-01-05 15:10:17 -0600109 aapt p -0 .dat -0 .dict -F $NAME .
Steve Kondikae76c842010-06-28 11:47:26 -0400110
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100111 [ $QUIET ] || echo "Resigning with cert: `echo $CERT`"
Steve Kondikae76c842010-06-28 11:47:26 -0400112
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100113 [ $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 -0400114 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 +0100115 [ $QUIET ] || echo "Zipalign.."
Steve Kondikae76c842010-06-28 11:47:26 -0400116 zipalign -f 4 signed_$NAME $BASE/$1
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100117 if [ ! $QUIET ]; then
118 ls -l $BASE/$1.old
119 ls -l $BASE/$1
120 fi
Steve Kondikae76c842010-06-28 11:47:26 -0400121 rm $BASE/$1.old
122else
123 echo "Usage: $0 [apk file]"
124fi
125