iceandfire | e8be7a5 | 2013-01-27 23:10:05 +0530 | [diff] [blame] | 1 | #!/bin/bash |
iceandfire | 779f0e2 | 2012-11-23 17:47:18 +0530 | [diff] [blame] | 2 | # |
| 3 | # Super-mega opticharger of doom |
| 4 | # Shrinks apks by running pngcrush or optipng or pngout 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 | |
iceandfire | e8be7a5 | 2013-01-27 23:10:05 +0530 | [diff] [blame] | 20 | . $ANDROID_BUILD_TOP/vendor/xenonhd/tools/colors |
| 21 | |
iceandfire | 779f0e2 | 2012-11-23 17:47:18 +0530 | [diff] [blame] | 22 | set -e |
| 23 | QUIET=1 |
| 24 | BASE=`pwd` |
| 25 | BRUTECRUSH="-brute" |
| 26 | TMPDIR=/tmp/opticharge-$$ |
| 27 | |
| 28 | if [ -z "$BRUTE_PNGCRUSH" ] |
| 29 | then |
| 30 | BRUTECRUSH="" |
| 31 | fi |
| 32 | |
| 33 | if [ "$APKCERTS" = "" ]; |
| 34 | then |
| 35 | if [ "$TARGET_BUILD_VARIANT" = "userdebug" ]; then |
| 36 | TARGET_BUILD_VARIANT="eng" |
| 37 | fi |
| 38 | |
| 39 | APKCERTS=$OUT/obj/PACKAGING/target_files_intermediates/$TARGET_PRODUCT-target_files-$TARGET_BUILD_VARIANT.$USER/META/apkcerts.txt |
| 40 | if [ ! -f "$APKCERTS" ]; |
| 41 | then |
iceandfire | e8be7a5 | 2013-01-27 23:10:05 +0530 | [diff] [blame] | 42 | echo -e $CL_RED"Set APKCERTS to the path to your apkcerts.txt file"$CL_RST |
iceandfire | 779f0e2 | 2012-11-23 17:47:18 +0530 | [diff] [blame] | 43 | exit 1; |
| 44 | fi |
| 45 | fi |
| 46 | |
| 47 | if [ ! -f "$APKCERTS" ]; |
| 48 | then |
iceandfire | e8be7a5 | 2013-01-27 23:10:05 +0530 | [diff] [blame] | 49 | echo -e $CL_RED"Invalid path to apkcerts.txt, set APKCERTS to the correct path."$CL_RST |
iceandfire | 779f0e2 | 2012-11-23 17:47:18 +0530 | [diff] [blame] | 50 | fi |
| 51 | |
| 52 | if [ "$(which pngcrush)" != "" ]; |
| 53 | then |
| 54 | optimize_png () { |
| 55 | pngcrush -q ${BRUTECRUSH} $1 ${1}.out 1> /dev/null 2> /dev/null |
| 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 |
iceandfire | e8be7a5 | 2013-01-27 23:10:05 +0530 | [diff] [blame] | 74 | echo -e $CL_RED"Please install pngcrush, optipng, or pngout"$CL_RST |
iceandfire | 779f0e2 | 2012-11-23 17:47:18 +0530 | [diff] [blame] | 75 | exit 1; |
| 76 | fi |
| 77 | |
| 78 | if [ "`which aapt`" = "" ]; |
| 79 | then |
iceandfire | e8be7a5 | 2013-01-27 23:10:05 +0530 | [diff] [blame] | 80 | echo -e $CL_RED"Please ensure aapt is in your \$PATH"$CL_RST |
iceandfire | 779f0e2 | 2012-11-23 17:47:18 +0530 | [diff] [blame] | 81 | exit 1; |
| 82 | fi |
| 83 | |
| 84 | if [ "`which zipalign`" = "" ]; |
| 85 | then |
iceandfire | e8be7a5 | 2013-01-27 23:10:05 +0530 | [diff] [blame] | 86 | echo -e $CL_RED"Please ensure zipalign is in your \$PATH"$CL_RST |
iceandfire | 779f0e2 | 2012-11-23 17:47:18 +0530 | [diff] [blame] | 87 | exit 1; |
| 88 | fi |
| 89 | |
| 90 | if [ -e "$1" ]; |
| 91 | then |
| 92 | NAME=`basename $1`; |
iceandfire | e8be7a5 | 2013-01-27 23:10:05 +0530 | [diff] [blame] | 93 | echo -e $CL_CYN"Optimizing"$CL_RST" $NAME..."; |
iceandfire | 779f0e2 | 2012-11-23 17:47:18 +0530 | [diff] [blame] | 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 |
iceandfire | e8be7a5 | 2013-01-27 23:10:05 +0530 | [diff] [blame] | 101 | echo -e $CL_RED"$CERT does not exist!";$CL_RST |
iceandfire | 779f0e2 | 2012-11-23 17:47:18 +0530 | [diff] [blame] | 102 | exit 1; |
| 103 | fi |
| 104 | else |
| 105 | APKINFO=`grep "name=\"$NAME\"" $APKCERTS`; |
iceandfire | e8be7a5 | 2013-01-27 23:10:05 +0530 | [diff] [blame] | 106 | [ $QUIET ] || echo -e "APKINFO: $APKINFO"; |
iceandfire | 779f0e2 | 2012-11-23 17:47:18 +0530 | [diff] [blame] | 107 | if [ "$APKINFO" = "" ]; |
| 108 | then |
iceandfire | e8be7a5 | 2013-01-27 23:10:05 +0530 | [diff] [blame] | 109 | echo -e $CL_RED"No apk info for $NAME";$CL_RST |
iceandfire | 779f0e2 | 2012-11-23 17:47:18 +0530 | [diff] [blame] | 110 | exit 1; |
| 111 | fi |
iceandfire | e8be7a5 | 2013-01-27 23:10:05 +0530 | [diff] [blame] | 112 | CERT=`echo -e $APKINFO | awk {'print $2'} | cut -f 2 -d "=" | tr -d "\""`; |
| 113 | KEY=`echo -e $APKINFO | awk {'print $3'} | cut -f 2 -d "=" | tr -d "\""`; |
iceandfire | 779f0e2 | 2012-11-23 17:47:18 +0530 | [diff] [blame] | 114 | if [ "$CERT" = "" ]; |
| 115 | then |
iceandfire | e8be7a5 | 2013-01-27 23:10:05 +0530 | [diff] [blame] | 116 | echo -e $CL_RED"Unable to find certificate for $NAME"$CL_RST |
| 117 | exit 1; |
| 118 | fi |
| 119 | if [ "$CERT" = "PRESIGNED" ]; |
| 120 | then |
| 121 | echo -e $CL_GRN"$NAME is presigned, skipping"$CL_RST |
iceandfire | 779f0e2 | 2012-11-23 17:47:18 +0530 | [diff] [blame] | 122 | exit 1; |
| 123 | fi |
| 124 | fi |
| 125 | |
iceandfire | e8be7a5 | 2013-01-27 23:10:05 +0530 | [diff] [blame] | 126 | [ $QUIET ] || echo -e $CL_YLW"Certificate:"$CL_RST" $CERT"; |
iceandfire | 779f0e2 | 2012-11-23 17:47:18 +0530 | [diff] [blame] | 127 | |
| 128 | [ -d $TMPDIR/$NAME ] && rm -rf $TMPDIR/$NAME |
| 129 | mkdir -p $TMPDIR/$NAME |
| 130 | trap "rm -rf $TMPDIR; exit" INT TERM EXIT |
| 131 | cd $TMPDIR/$NAME |
| 132 | unzip -q $BASE/$1 |
| 133 | for x in `find . -name "*.png" | grep -v "\.9.png$" | tr "\n" " "` |
| 134 | do |
iceandfire | e8be7a5 | 2013-01-27 23:10:05 +0530 | [diff] [blame] | 135 | [ $QUIET ] || echo -e $CL_GRN"Crushing $x"$CL_RST |
iceandfire | 779f0e2 | 2012-11-23 17:47:18 +0530 | [diff] [blame] | 136 | optimize_png $x |
| 137 | done |
| 138 | cp $BASE/$1 $BASE/$1.old |
| 139 | |
iceandfire | e8be7a5 | 2013-01-27 23:10:05 +0530 | [diff] [blame] | 140 | [ $QUIET ] || echo -e $CL_GRN"Repacking apk.."$CL_RST |
| 141 | aapt p -0 .dat -0 .dict -0 .arsc -F $NAME . |
iceandfire | 779f0e2 | 2012-11-23 17:47:18 +0530 | [diff] [blame] | 142 | |
iceandfire | e8be7a5 | 2013-01-27 23:10:05 +0530 | [diff] [blame] | 143 | [ $QUIET ] || echo -e $CL_YLW"Resigning with cert: `echo -e $CERT`"$CL_RST |
| 144 | |
| 145 | [ $QUIET ] || echo -e java -jar $ANDROID_BUILD_TOP/out/host/$OUT_TARGET_HOST/framework/signapk.jar $ANDROID_BUILD_TOP/$CERT $ANDROID_BUILD_TOP/$KEY $NAME signed_$NAME |
iceandfire | 779f0e2 | 2012-11-23 17:47:18 +0530 | [diff] [blame] | 146 | java -jar $ANDROID_BUILD_TOP/out/host/$OUT_TARGET_HOST/framework/signapk.jar $ANDROID_BUILD_TOP/$CERT $ANDROID_BUILD_TOP/$KEY $NAME signed_$NAME |
iceandfire | e8be7a5 | 2013-01-27 23:10:05 +0530 | [diff] [blame] | 147 | [ $QUIET ] || echo -e $CL_GRN"Zipalign.."$CL_RST |
iceandfire | 779f0e2 | 2012-11-23 17:47:18 +0530 | [diff] [blame] | 148 | zipalign -f 4 signed_$NAME $BASE/$1 |
| 149 | if [ ! $QUIET ]; then |
| 150 | ls -l $BASE/$1.old |
| 151 | ls -l $BASE/$1 |
| 152 | fi |
| 153 | rm $BASE/$1.old |
| 154 | else |
iceandfire | e8be7a5 | 2013-01-27 23:10:05 +0530 | [diff] [blame] | 155 | echo -e "Usage: $0 [apk file]" |
iceandfire | 779f0e2 | 2012-11-23 17:47:18 +0530 | [diff] [blame] | 156 | fi |