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