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