blob: 652e5c60b43d159f390031f08d94e3bf6bd558f5 [file] [log] [blame]
Chirayu Desai39cb8782012-10-13 16:10:45 +05301#!/bin/bash
Steve Kondikae76c842010-06-28 11:47:26 -04002#
3# Super-mega opticharger of doom
Evan McClainf1994862011-10-28 04:34:29 -04004# Shrinks apks by running pngcrush or optipng or pngout on png images
Steve Kondikae76c842010-06-28 11:47:26 -04005#
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 Kondikae76c842010-06-28 11:47:26 -040012set -e
Pat Thoyts0e7f64e2010-06-11 11:53:38 +010013QUIET=1
Steve Kondikae76c842010-06-28 11:47:26 -040014BASE=`pwd`
Ricardo Cerqueirae1f37382011-11-27 19:56:54 +000015BRUTECRUSH="-brute"
Steve Kondik47ce7ec2010-08-22 00:08:06 -040016TMPDIR=/tmp/opticharge-$$
17
Chirayu Desai39cb8782012-10-13 16:10:45 +053018. $ANDROID_BUILD_TOP/vendor/cm/tools/functions
19
Ricardo Cerqueirae1f37382011-11-27 19:56:54 +000020if [ -z "$BRUTE_PNGCRUSH" ]
21then
22 BRUTECRUSH=""
23fi
24
Steve Kondikae76c842010-06-28 11:47:26 -040025if [ "$APKCERTS" = "" ];
26then
Ricardo Cerqueirae1f37382011-11-27 19:56:54 +000027 if [ "$TARGET_BUILD_VARIANT" = "userdebug" ]; then
28 TARGET_BUILD_VARIANT="eng"
Erez A. Korn8e8a6782012-08-12 08:33:59 +030029 elif [ "$TARGET_BUILD_VARIANT" = "user" ]; then
30 TARGET_BUILD_VARIANT="eng"
Ricardo Cerqueirae1f37382011-11-27 19:56:54 +000031 fi
32
Ladios Jonquil1f765132011-09-25 02:11:08 +080033 APKCERTS=$OUT/obj/PACKAGING/target_files_intermediates/$TARGET_PRODUCT-target_files-$TARGET_BUILD_VARIANT.$USER/META/apkcerts.txt
Steve Kondikae76c842010-06-28 11:47:26 -040034 if [ ! -f "$APKCERTS" ];
35 then
Chirayu Desai39cb8782012-10-13 16:10:45 +053036 echo -e $CL_RED"Set APKCERTS to the path to your apkcerts.txt file"$CL_RST
Steve Kondikae76c842010-06-28 11:47:26 -040037 exit 1;
38 fi
39fi
40
41if [ ! -f "$APKCERTS" ];
42then
Chirayu Desai39cb8782012-10-13 16:10:45 +053043 echo -e $CL_RED"Invalid path to apkcerts.txt, set APKCERTS to the correct path."$CL_RST
Steve Kondikae76c842010-06-28 11:47:26 -040044fi
45
Evan McClainf1994862011-10-28 04:34:29 -040046if [ "$(which pngcrush)" != "" ];
Steve Kondikae76c842010-06-28 11:47:26 -040047then
Evan McClainf1994862011-10-28 04:34:29 -040048 optimize_png () {
Ricardo Cerqueirae1f37382011-11-27 19:56:54 +000049 pngcrush -q ${BRUTECRUSH} $1 ${1}.out 1> /dev/null 2> /dev/null
Reinhard Lenninger4576c752012-08-26 22:47:49 +020050 mv ${1}.out ${1} 2> /dev/null
Evan McClainf1994862011-10-28 04:34:29 -040051 }
52elif [ "$(which optipng)" != "" ];
53then
54 optimize_png () {
55 optipng -o7 -quiet $1 1> /dev/null 2> /dev/null
56 }
57elif [ "$(which pngout-static)" != "" ];
58then
59 optimize_png () {
60 pngout-static $1
61 }
62elif [ "$(which pngout)" != "" ];
63then
64 optimize_png () {
65 pngout $1
66 }
67else
Chirayu Desai39cb8782012-10-13 16:10:45 +053068 echo -e $CL_RED"Please install pngcrush, optipng, or pngout"$CL_RST
Steve Kondikae76c842010-06-28 11:47:26 -040069 exit 1;
70fi
71
72if [ "`which aapt`" = "" ];
73then
Chirayu Desai39cb8782012-10-13 16:10:45 +053074 echo -e $CL_RED"Please ensure aapt is in your \$PATH"$CL_RST
Steve Kondikae76c842010-06-28 11:47:26 -040075 exit 1;
76fi
77
78if [ "`which zipalign`" = "" ];
79then
Chirayu Desai39cb8782012-10-13 16:10:45 +053080 echo -e $CL_RED"Please ensure zipalign is in your \$PATH"$CL_RST
Steve Kondikae76c842010-06-28 11:47:26 -040081 exit 1;
82fi
83
84if [ -e "$1" ];
85then
86 NAME=`basename $1`;
Chirayu Desai39cb8782012-10-13 16:10:45 +053087 echo -e $CL_YLW"Optimize:"$CL_RST" $NAME..";
Steve Kondikae76c842010-06-28 11:47:26 -040088
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 Desai39cb8782012-10-13 16:10:45 +053095 echo -e $CL_RED"$CERT does not exist!"$CL_RST;
Steve Kondikae76c842010-06-28 11:47:26 -040096 exit 1;
97 fi
98 else
99 APKINFO=`grep "name=\"$NAME\"" $APKCERTS`;
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100100 [ $QUIET ] || echo "APKINFO: $APKINFO";
Steve Kondikae76c842010-06-28 11:47:26 -0400101 if [ "$APKINFO" = "" ];
102 then
Chirayu Desai39cb8782012-10-13 16:10:45 +0530103 echo -e $CL_RED"No apk info for $NAME"$CL_RST;
Steve Kondikae76c842010-06-28 11:47:26 -0400104 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 Desai39cb8782012-10-13 16:10:45 +0530110 echo -e $CL_RED"Unable to find certificate for $NAME"$CL_RST
Steve Kondikae76c842010-06-28 11:47:26 -0400111 exit 1;
112 fi
KhasMeka82b7b32012-08-16 13:16:52 -0400113 if [ "$CERT" = "PRESIGNED" ];
114 then
Chirayu Desai39cb8782012-10-13 16:10:45 +0530115 echo -e $CL_YLW"$NAME is presigned, skipping"$CL_RST
KhasMeka82b7b32012-08-16 13:16:52 -0400116 exit 1;
117 fi
Steve Kondikae76c842010-06-28 11:47:26 -0400118 fi
119
Chirayu Desai39cb8782012-10-13 16:10:45 +0530120 [ $QUIET ] || echo -e $CL_YLW"Certificate:"$CL_RST" $CERT";
Steve Kondikae76c842010-06-28 11:47:26 -0400121
Steve Kondik47ce7ec2010-08-22 00:08:06 -0400122 [ -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 Thoyts0e7f64e2010-06-11 11:53:38 +0100126 unzip -q $BASE/$1
Steve Kondikae76c842010-06-28 11:47:26 -0400127 for x in `find . -name "*.png" | grep -v "\.9.png$" | tr "\n" " "`
128 do
Chirayu Desai39cb8782012-10-13 16:10:45 +0530129 [ $QUIET ] || echo -e $CL_YLW"Crushing $x"$CL_RST
Evan McClainf1994862011-10-28 04:34:29 -0400130 optimize_png $x
Steve Kondikae76c842010-06-28 11:47:26 -0400131 done
132 cp $BASE/$1 $BASE/$1.old
133
Chirayu Desai39cb8782012-10-13 16:10:45 +0530134 [ $QUIET ] || echo -e $CL_YLW"Repacking apk.."$CL_RST
Pawit Pornkitprasanbb565312012-08-18 15:16:08 +0700135 aapt p -0 .dat -0 .dict -0 .arsc -F $NAME .
Steve Kondikae76c842010-06-28 11:47:26 -0400136
Chirayu Desai39cb8782012-10-13 16:10:45 +0530137 [ $QUIET ] || echo -e $CL_YLW"Resigning with cert: `echo $CERT`"$CL_RST
Steve Kondikae76c842010-06-28 11:47:26 -0400138
Chirayu Desaid1dbaf32012-08-15 16:30:23 +0530139 [ $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 Desai39cb8782012-10-13 16:10:45 +0530141 [ $QUIET ] || echo -e $CL_YLW"Zipaligning.."$CL_RST
Steve Kondikae76c842010-06-28 11:47:26 -0400142 zipalign -f 4 signed_$NAME $BASE/$1
Pat Thoyts0e7f64e2010-06-11 11:53:38 +0100143 if [ ! $QUIET ]; then
144 ls -l $BASE/$1.old
145 ls -l $BASE/$1
146 fi
Steve Kondikae76c842010-06-28 11:47:26 -0400147 rm $BASE/$1.old
148else
149 echo "Usage: $0 [apk file]"
150fi
151