blob: ecea88b214438ce2e374dca75d3178641456136f [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
21
22BASE=`pwd`
23if [ "$APKCERTS" = "" ];
24then
25 APKCERTS=$OUT/obj/PACKAGING/target_files_intermediates/$TARGET_PRODUCT-target_files-$TARGET_BUILD_VARIANT.$LOGNAME/META/apkcerts.txt
26 if [ ! -f "$APKCERTS" ];
27 then
28 echo "Set APKCERTS to the path to your apkcerts.txt file"
29 exit 1;
30 fi
31fi
32
33if [ ! -f "$APKCERTS" ];
34then
35 echo "Invalid path to apkcerts.txt, set APKCERTS to the correct path."
36fi
37
38if [ "`which pngcrush`" = "" ];
39then
40 echo "Please install pngcrush"
41 exit 1;
42fi
43
44if [ "`which aapt`" = "" ];
45then
46 echo "Please ensure aapt is in your \$PATH"
47 exit 1;
48fi
49
50if [ "`which zipalign`" = "" ];
51then
52 echo "Please ensure zipalign is in your \$PATH"
53 exit 1;
54fi
55
56if [ -e "$1" ];
57then
58 NAME=`basename $1`;
59 echo "Optimizing $NAME...";
60
61 if [ "$2" != "" ];
62 then
63 CERT=build/target/product/security/$2.x509.pem
64 KEY=build/target/product/security/$2.pk8
65 if [ ! -f "$ANDROID_BUILD_TOP/$CERT" ];
66 then
67 echo "$CERT does not exist!";
68 exit 1;
69 fi
70 else
71 APKINFO=`grep "name=\"$NAME\"" $APKCERTS`;
72 echo "APKINFO: $APKINFO";
73 if [ "$APKINFO" = "" ];
74 then
75 echo "No apk info for $NAME";
76 exit 1;
77 fi
78 CERT=`echo $APKINFO | awk {'print $2'} | cut -f 2 -d "=" | tr -d "\""`;
79 KEY=`echo $APKINFO | awk {'print $3'} | cut -f 2 -d "=" | tr -d "\""`;
80 if [ "$CERT" = "" ];
81 then
82 echo "Unable to find certificate for $NAME"
83 exit 1;
84 fi
85 fi
86
87 echo "Certificate: $CERT";
88
89 rm -rf /tmp/$NAME
90 mkdir /tmp/$NAME
91 cd /tmp/$NAME
92 unzip $BASE/$1
93 for x in `find . -name "*.png" | grep -v "\.9.png$" | tr "\n" " "`
94 do
95 echo "Crushing $x"
96 pngcrush $x $x.crushed 1>/dev/null
97 if [ -e "$x.crushed" ];
98 then
99 mv $x.crushed $x
100 fi
101 done
102 cp $BASE/$1 $BASE/$1.old
103
104 echo "Repacking apk.."
105 aapt p -0 res/raw -0 res/raw-en -F $NAME .
106
107 echo "Resigning with cert: `echo $CERT`"
108
109 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
110 java -jar $ANDROID_BUILD_TOP/out/host/$OUT_TARGET_HOST/framework/signapk.jar $ANDROID_BUILD_TOP/$CERT $ANDROID_BUILD_TOP/$KEY $NAME signed_$NAME
111 echo "Zipalign.."
112 zipalign -f 4 signed_$NAME $BASE/$1
113 ls -l $BASE/$1.old
114 ls -l $BASE/$1
115 rm $BASE/$1.old
116else
117 echo "Usage: $0 [apk file]"
118fi
119