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` |
Steve Kondik | 47ce7ec | 2010-08-22 00:08:06 -0400 | [diff] [blame] | 24 | TMPDIR=/tmp/opticharge-$$ |
| 25 | |
Steve Kondik | ae76c84 | 2010-06-28 11:47:26 -0400 | [diff] [blame] | 26 | if [ "$APKCERTS" = "" ]; |
| 27 | then |
| 28 | APKCERTS=$OUT/obj/PACKAGING/target_files_intermediates/$TARGET_PRODUCT-target_files-$TARGET_BUILD_VARIANT.$LOGNAME/META/apkcerts.txt |
| 29 | if [ ! -f "$APKCERTS" ]; |
| 30 | then |
| 31 | echo "Set APKCERTS to the path to your apkcerts.txt file" |
| 32 | exit 1; |
| 33 | fi |
| 34 | fi |
| 35 | |
| 36 | if [ ! -f "$APKCERTS" ]; |
| 37 | then |
| 38 | echo "Invalid path to apkcerts.txt, set APKCERTS to the correct path." |
| 39 | fi |
| 40 | |
| 41 | if [ "`which pngcrush`" = "" ]; |
| 42 | then |
| 43 | echo "Please install pngcrush" |
| 44 | exit 1; |
| 45 | fi |
| 46 | |
| 47 | if [ "`which aapt`" = "" ]; |
| 48 | then |
| 49 | echo "Please ensure aapt is in your \$PATH" |
| 50 | exit 1; |
| 51 | fi |
| 52 | |
| 53 | if [ "`which zipalign`" = "" ]; |
| 54 | then |
| 55 | echo "Please ensure zipalign is in your \$PATH" |
| 56 | exit 1; |
| 57 | fi |
| 58 | |
| 59 | if [ -e "$1" ]; |
| 60 | then |
| 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 Thoyts | 0e7f64e | 2010-06-11 11:53:38 +0100 | [diff] [blame] | 75 | [ $QUIET ] || echo "APKINFO: $APKINFO"; |
Steve Kondik | ae76c84 | 2010-06-28 11:47:26 -0400 | [diff] [blame] | 76 | 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 Thoyts | 0e7f64e | 2010-06-11 11:53:38 +0100 | [diff] [blame] | 90 | [ $QUIET ] || echo "Certificate: $CERT"; |
Steve Kondik | ae76c84 | 2010-06-28 11:47:26 -0400 | [diff] [blame] | 91 | |
Steve Kondik | 47ce7ec | 2010-08-22 00:08:06 -0400 | [diff] [blame] | 92 | [ -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 Thoyts | 0e7f64e | 2010-06-11 11:53:38 +0100 | [diff] [blame] | 96 | unzip -q $BASE/$1 |
Steve Kondik | ae76c84 | 2010-06-28 11:47:26 -0400 | [diff] [blame] | 97 | for x in `find . -name "*.png" | grep -v "\.9.png$" | tr "\n" " "` |
| 98 | do |
Pat Thoyts | 0e7f64e | 2010-06-11 11:53:38 +0100 | [diff] [blame] | 99 | [ $QUIET ] || echo "Crushing $x" |
| 100 | pngcrush $QFLAG $x $x.crushed 1>/dev/null |
Steve Kondik | ae76c84 | 2010-06-28 11:47:26 -0400 | [diff] [blame] | 101 | if [ -e "$x.crushed" ]; |
| 102 | then |
| 103 | mv $x.crushed $x |
| 104 | fi |
| 105 | done |
| 106 | cp $BASE/$1 $BASE/$1.old |
| 107 | |
Pat Thoyts | 0e7f64e | 2010-06-11 11:53:38 +0100 | [diff] [blame] | 108 | [ $QUIET ] || echo "Repacking apk.." |
Christopher Lais | c557d07 | 2011-01-05 15:10:17 -0600 | [diff] [blame] | 109 | aapt p -0 .dat -0 .dict -F $NAME . |
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 "Resigning with cert: `echo $CERT`" |
Steve Kondik | ae76c84 | 2010-06-28 11:47:26 -0400 | [diff] [blame] | 112 | |
Pat Thoyts | 0e7f64e | 2010-06-11 11:53:38 +0100 | [diff] [blame] | 113 | [ $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] | 114 | 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] | 115 | [ $QUIET ] || echo "Zipalign.." |
Steve Kondik | ae76c84 | 2010-06-28 11:47:26 -0400 | [diff] [blame] | 116 | zipalign -f 4 signed_$NAME $BASE/$1 |
Pat Thoyts | 0e7f64e | 2010-06-11 11:53:38 +0100 | [diff] [blame] | 117 | if [ ! $QUIET ]; then |
| 118 | ls -l $BASE/$1.old |
| 119 | ls -l $BASE/$1 |
| 120 | fi |
Steve Kondik | ae76c84 | 2010-06-28 11:47:26 -0400 | [diff] [blame] | 121 | rm $BASE/$1.old |
| 122 | else |
| 123 | echo "Usage: $0 [apk file]" |
| 124 | fi |
| 125 | |