Steve Kondik | ae76c84 | 2010-06-28 11:47:26 -0400 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # Super-mega opticharger of doom |
Evan McClain | f199486 | 2011-10-28 04:34:29 -0400 | [diff] [blame] | 4 | # Shrinks apks by running pngcrush or optipng or pngout on png images |
Steve Kondik | ae76c84 | 2010-06-28 11:47:26 -0400 | [diff] [blame] | 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 |
Steve Kondik | ae76c84 | 2010-06-28 11:47:26 -0400 | [diff] [blame] | 22 | BASE=`pwd` |
Ricardo Cerqueira | e1f3738 | 2011-11-27 19:56:54 +0000 | [diff] [blame] | 23 | BRUTECRUSH="-brute" |
Steve Kondik | 47ce7ec | 2010-08-22 00:08:06 -0400 | [diff] [blame] | 24 | TMPDIR=/tmp/opticharge-$$ |
| 25 | |
Ricardo Cerqueira | e1f3738 | 2011-11-27 19:56:54 +0000 | [diff] [blame] | 26 | if [ -z "$BRUTE_PNGCRUSH" ] |
| 27 | then |
| 28 | BRUTECRUSH="" |
| 29 | fi |
| 30 | |
Steve Kondik | ae76c84 | 2010-06-28 11:47:26 -0400 | [diff] [blame] | 31 | if [ "$APKCERTS" = "" ]; |
| 32 | then |
Ricardo Cerqueira | e1f3738 | 2011-11-27 19:56:54 +0000 | [diff] [blame] | 33 | if [ "$TARGET_BUILD_VARIANT" = "userdebug" ]; then |
| 34 | TARGET_BUILD_VARIANT="eng" |
| 35 | fi |
| 36 | |
Ladios Jonquil | 1f76513 | 2011-09-25 02:11:08 +0800 | [diff] [blame] | 37 | APKCERTS=$OUT/obj/PACKAGING/target_files_intermediates/$TARGET_PRODUCT-target_files-$TARGET_BUILD_VARIANT.$USER/META/apkcerts.txt |
Steve Kondik | ae76c84 | 2010-06-28 11:47:26 -0400 | [diff] [blame] | 38 | if [ ! -f "$APKCERTS" ]; |
| 39 | then |
| 40 | echo "Set APKCERTS to the path to your apkcerts.txt file" |
| 41 | exit 1; |
| 42 | fi |
| 43 | fi |
| 44 | |
| 45 | if [ ! -f "$APKCERTS" ]; |
| 46 | then |
| 47 | echo "Invalid path to apkcerts.txt, set APKCERTS to the correct path." |
| 48 | fi |
| 49 | |
Evan McClain | f199486 | 2011-10-28 04:34:29 -0400 | [diff] [blame] | 50 | if [ "$(which pngcrush)" != "" ]; |
Steve Kondik | ae76c84 | 2010-06-28 11:47:26 -0400 | [diff] [blame] | 51 | then |
Evan McClain | f199486 | 2011-10-28 04:34:29 -0400 | [diff] [blame] | 52 | optimize_png () { |
Ricardo Cerqueira | e1f3738 | 2011-11-27 19:56:54 +0000 | [diff] [blame] | 53 | pngcrush -q ${BRUTECRUSH} $1 ${1}.out 1> /dev/null 2> /dev/null |
Evan McClain | f199486 | 2011-10-28 04:34:29 -0400 | [diff] [blame] | 54 | mv ${1}.out ${1} |
| 55 | } |
| 56 | elif [ "$(which optipng)" != "" ]; |
| 57 | then |
| 58 | optimize_png () { |
| 59 | optipng -o7 -quiet $1 1> /dev/null 2> /dev/null |
| 60 | } |
| 61 | elif [ "$(which pngout-static)" != "" ]; |
| 62 | then |
| 63 | optimize_png () { |
| 64 | pngout-static $1 |
| 65 | } |
| 66 | elif [ "$(which pngout)" != "" ]; |
| 67 | then |
| 68 | optimize_png () { |
| 69 | pngout $1 |
| 70 | } |
| 71 | else |
| 72 | echo "Please install pngcrush, optipng, or pngout" |
Steve Kondik | ae76c84 | 2010-06-28 11:47:26 -0400 | [diff] [blame] | 73 | exit 1; |
| 74 | fi |
| 75 | |
| 76 | if [ "`which aapt`" = "" ]; |
| 77 | then |
| 78 | echo "Please ensure aapt is in your \$PATH" |
| 79 | exit 1; |
| 80 | fi |
| 81 | |
| 82 | if [ "`which zipalign`" = "" ]; |
| 83 | then |
| 84 | echo "Please ensure zipalign is in your \$PATH" |
| 85 | exit 1; |
| 86 | fi |
| 87 | |
| 88 | if [ -e "$1" ]; |
| 89 | then |
| 90 | NAME=`basename $1`; |
| 91 | echo "Optimizing $NAME..."; |
| 92 | |
| 93 | if [ "$2" != "" ]; |
| 94 | then |
| 95 | CERT=build/target/product/security/$2.x509.pem |
| 96 | KEY=build/target/product/security/$2.pk8 |
| 97 | if [ ! -f "$ANDROID_BUILD_TOP/$CERT" ]; |
| 98 | then |
| 99 | echo "$CERT does not exist!"; |
| 100 | exit 1; |
| 101 | fi |
| 102 | else |
| 103 | APKINFO=`grep "name=\"$NAME\"" $APKCERTS`; |
Pat Thoyts | 0e7f64e | 2010-06-11 11:53:38 +0100 | [diff] [blame] | 104 | [ $QUIET ] || echo "APKINFO: $APKINFO"; |
Steve Kondik | ae76c84 | 2010-06-28 11:47:26 -0400 | [diff] [blame] | 105 | if [ "$APKINFO" = "" ]; |
| 106 | then |
| 107 | echo "No apk info for $NAME"; |
| 108 | exit 1; |
| 109 | fi |
| 110 | CERT=`echo $APKINFO | awk {'print $2'} | cut -f 2 -d "=" | tr -d "\""`; |
| 111 | KEY=`echo $APKINFO | awk {'print $3'} | cut -f 2 -d "=" | tr -d "\""`; |
| 112 | if [ "$CERT" = "" ]; |
| 113 | then |
| 114 | echo "Unable to find certificate for $NAME" |
| 115 | exit 1; |
| 116 | fi |
| 117 | fi |
| 118 | |
Pat Thoyts | 0e7f64e | 2010-06-11 11:53:38 +0100 | [diff] [blame] | 119 | [ $QUIET ] || echo "Certificate: $CERT"; |
Steve Kondik | ae76c84 | 2010-06-28 11:47:26 -0400 | [diff] [blame] | 120 | |
Steve Kondik | 47ce7ec | 2010-08-22 00:08:06 -0400 | [diff] [blame] | 121 | [ -d $TMPDIR/$NAME ] && rm -rf $TMPDIR/$NAME |
| 122 | mkdir -p $TMPDIR/$NAME |
| 123 | trap "rm -rf $TMPDIR; exit" INT TERM EXIT |
| 124 | cd $TMPDIR/$NAME |
Pat Thoyts | 0e7f64e | 2010-06-11 11:53:38 +0100 | [diff] [blame] | 125 | unzip -q $BASE/$1 |
Steve Kondik | ae76c84 | 2010-06-28 11:47:26 -0400 | [diff] [blame] | 126 | for x in `find . -name "*.png" | grep -v "\.9.png$" | tr "\n" " "` |
| 127 | do |
Pat Thoyts | 0e7f64e | 2010-06-11 11:53:38 +0100 | [diff] [blame] | 128 | [ $QUIET ] || echo "Crushing $x" |
Evan McClain | f199486 | 2011-10-28 04:34:29 -0400 | [diff] [blame] | 129 | optimize_png $x |
Steve Kondik | ae76c84 | 2010-06-28 11:47:26 -0400 | [diff] [blame] | 130 | done |
| 131 | cp $BASE/$1 $BASE/$1.old |
| 132 | |
Pat Thoyts | 0e7f64e | 2010-06-11 11:53:38 +0100 | [diff] [blame] | 133 | [ $QUIET ] || echo "Repacking apk.." |
Christopher Lais | c557d07 | 2011-01-05 15:10:17 -0600 | [diff] [blame] | 134 | aapt p -0 .dat -0 .dict -F $NAME . |
Steve Kondik | ae76c84 | 2010-06-28 11:47:26 -0400 | [diff] [blame] | 135 | |
Pat Thoyts | 0e7f64e | 2010-06-11 11:53:38 +0100 | [diff] [blame] | 136 | [ $QUIET ] || echo "Resigning with cert: `echo $CERT`" |
Steve Kondik | ae76c84 | 2010-06-28 11:47:26 -0400 | [diff] [blame] | 137 | |
Pat Thoyts | 0e7f64e | 2010-06-11 11:53:38 +0100 | [diff] [blame] | 138 | [ $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] | 139 | 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] | 140 | [ $QUIET ] || echo "Zipalign.." |
Steve Kondik | ae76c84 | 2010-06-28 11:47:26 -0400 | [diff] [blame] | 141 | zipalign -f 4 signed_$NAME $BASE/$1 |
Pat Thoyts | 0e7f64e | 2010-06-11 11:53:38 +0100 | [diff] [blame] | 142 | if [ ! $QUIET ]; then |
| 143 | ls -l $BASE/$1.old |
| 144 | ls -l $BASE/$1 |
| 145 | fi |
Steve Kondik | ae76c84 | 2010-06-28 11:47:26 -0400 | [diff] [blame] | 146 | rm $BASE/$1.old |
| 147 | else |
| 148 | echo "Usage: $0 [apk file]" |
| 149 | fi |
| 150 | |