Steve Kondik | ae76c84 | 2010-06-28 11:47:26 -0400 | [diff] [blame] | 1 | #!/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 | |
| 12 | OUT_TARGET_HOST=$(uname -a | grep Darwin) |
| 13 | if [ -z "$OUT_TARGET_HOST" ] |
| 14 | then |
| 15 | OUT_TARGET_HOST=linux-x86 |
| 16 | else |
| 17 | OUT_TARGET_HOST=darwin-x86 |
| 18 | fi |
| 19 | |
| 20 | set -e |
| 21 | |
| 22 | BASE=`pwd` |
| 23 | if [ "$APKCERTS" = "" ]; |
| 24 | then |
| 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 |
| 31 | fi |
| 32 | |
| 33 | if [ ! -f "$APKCERTS" ]; |
| 34 | then |
| 35 | echo "Invalid path to apkcerts.txt, set APKCERTS to the correct path." |
| 36 | fi |
| 37 | |
| 38 | if [ "`which pngcrush`" = "" ]; |
| 39 | then |
| 40 | echo "Please install pngcrush" |
| 41 | exit 1; |
| 42 | fi |
| 43 | |
| 44 | if [ "`which aapt`" = "" ]; |
| 45 | then |
| 46 | echo "Please ensure aapt is in your \$PATH" |
| 47 | exit 1; |
| 48 | fi |
| 49 | |
| 50 | if [ "`which zipalign`" = "" ]; |
| 51 | then |
| 52 | echo "Please ensure zipalign is in your \$PATH" |
| 53 | exit 1; |
| 54 | fi |
| 55 | |
| 56 | if [ -e "$1" ]; |
| 57 | then |
| 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 |
| 116 | else |
| 117 | echo "Usage: $0 [apk file]" |
| 118 | fi |
| 119 | |