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