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