2010-06-28 15:47:26 +00:00
|
|
|
#!/bin/sh
|
|
|
|
#
|
|
|
|
# Super-mega opticharger of doom
|
2011-10-28 08:34:29 +00:00
|
|
|
# Shrinks apks by running pngcrush or optipng or pngout on png images
|
2010-06-28 15:47:26 +00:00
|
|
|
#
|
|
|
|
# Point APKCERTS at the full path to a generated apkcerts.txt file, such as:
|
|
|
|
# /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
|
|
|
|
#
|
|
|
|
# cyanogen - shade@chemlab.org
|
|
|
|
# ChrisSoyars - me@ctso.me
|
|
|
|
|
|
|
|
OUT_TARGET_HOST=$(uname -a | grep Darwin)
|
|
|
|
if [ -z "$OUT_TARGET_HOST" ]
|
|
|
|
then
|
|
|
|
OUT_TARGET_HOST=linux-x86
|
|
|
|
else
|
|
|
|
OUT_TARGET_HOST=darwin-x86
|
|
|
|
fi
|
|
|
|
|
|
|
|
set -e
|
2010-06-11 10:53:38 +00:00
|
|
|
QUIET=1
|
2010-06-28 15:47:26 +00:00
|
|
|
BASE=`pwd`
|
2011-11-27 19:56:54 +00:00
|
|
|
BRUTECRUSH="-brute"
|
2010-08-22 04:08:06 +00:00
|
|
|
TMPDIR=/tmp/opticharge-$$
|
|
|
|
|
2011-11-27 19:56:54 +00:00
|
|
|
if [ -z "$BRUTE_PNGCRUSH" ]
|
|
|
|
then
|
|
|
|
BRUTECRUSH=""
|
|
|
|
fi
|
|
|
|
|
2010-06-28 15:47:26 +00:00
|
|
|
if [ "$APKCERTS" = "" ];
|
|
|
|
then
|
2011-11-27 19:56:54 +00:00
|
|
|
if [ "$TARGET_BUILD_VARIANT" = "userdebug" ]; then
|
|
|
|
TARGET_BUILD_VARIANT="eng"
|
|
|
|
fi
|
|
|
|
|
2011-09-24 18:11:08 +00:00
|
|
|
APKCERTS=$OUT/obj/PACKAGING/target_files_intermediates/$TARGET_PRODUCT-target_files-$TARGET_BUILD_VARIANT.$USER/META/apkcerts.txt
|
2010-06-28 15:47:26 +00:00
|
|
|
if [ ! -f "$APKCERTS" ];
|
|
|
|
then
|
|
|
|
echo "Set APKCERTS to the path to your apkcerts.txt file"
|
|
|
|
exit 1;
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ! -f "$APKCERTS" ];
|
|
|
|
then
|
|
|
|
echo "Invalid path to apkcerts.txt, set APKCERTS to the correct path."
|
|
|
|
fi
|
|
|
|
|
2011-10-28 08:34:29 +00:00
|
|
|
if [ "$(which pngcrush)" != "" ];
|
2010-06-28 15:47:26 +00:00
|
|
|
then
|
2011-10-28 08:34:29 +00:00
|
|
|
optimize_png () {
|
2011-11-27 19:56:54 +00:00
|
|
|
pngcrush -q ${BRUTECRUSH} $1 ${1}.out 1> /dev/null 2> /dev/null
|
2011-10-28 08:34:29 +00:00
|
|
|
mv ${1}.out ${1}
|
|
|
|
}
|
|
|
|
elif [ "$(which optipng)" != "" ];
|
|
|
|
then
|
|
|
|
optimize_png () {
|
|
|
|
optipng -o7 -quiet $1 1> /dev/null 2> /dev/null
|
|
|
|
}
|
|
|
|
elif [ "$(which pngout-static)" != "" ];
|
|
|
|
then
|
|
|
|
optimize_png () {
|
|
|
|
pngout-static $1
|
|
|
|
}
|
|
|
|
elif [ "$(which pngout)" != "" ];
|
|
|
|
then
|
|
|
|
optimize_png () {
|
|
|
|
pngout $1
|
|
|
|
}
|
|
|
|
else
|
|
|
|
echo "Please install pngcrush, optipng, or pngout"
|
2010-06-28 15:47:26 +00:00
|
|
|
exit 1;
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "`which aapt`" = "" ];
|
|
|
|
then
|
|
|
|
echo "Please ensure aapt is in your \$PATH"
|
|
|
|
exit 1;
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "`which zipalign`" = "" ];
|
|
|
|
then
|
|
|
|
echo "Please ensure zipalign is in your \$PATH"
|
|
|
|
exit 1;
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -e "$1" ];
|
|
|
|
then
|
|
|
|
NAME=`basename $1`;
|
|
|
|
echo "Optimizing $NAME...";
|
|
|
|
|
|
|
|
if [ "$2" != "" ];
|
|
|
|
then
|
|
|
|
CERT=build/target/product/security/$2.x509.pem
|
|
|
|
KEY=build/target/product/security/$2.pk8
|
|
|
|
if [ ! -f "$ANDROID_BUILD_TOP/$CERT" ];
|
|
|
|
then
|
|
|
|
echo "$CERT does not exist!";
|
|
|
|
exit 1;
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
APKINFO=`grep "name=\"$NAME\"" $APKCERTS`;
|
2010-06-11 10:53:38 +00:00
|
|
|
[ $QUIET ] || echo "APKINFO: $APKINFO";
|
2010-06-28 15:47:26 +00:00
|
|
|
if [ "$APKINFO" = "" ];
|
|
|
|
then
|
|
|
|
echo "No apk info for $NAME";
|
|
|
|
exit 1;
|
|
|
|
fi
|
|
|
|
CERT=`echo $APKINFO | awk {'print $2'} | cut -f 2 -d "=" | tr -d "\""`;
|
|
|
|
KEY=`echo $APKINFO | awk {'print $3'} | cut -f 2 -d "=" | tr -d "\""`;
|
|
|
|
if [ "$CERT" = "" ];
|
|
|
|
then
|
|
|
|
echo "Unable to find certificate for $NAME"
|
|
|
|
exit 1;
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2010-06-11 10:53:38 +00:00
|
|
|
[ $QUIET ] || echo "Certificate: $CERT";
|
2010-06-28 15:47:26 +00:00
|
|
|
|
2010-08-22 04:08:06 +00:00
|
|
|
[ -d $TMPDIR/$NAME ] && rm -rf $TMPDIR/$NAME
|
|
|
|
mkdir -p $TMPDIR/$NAME
|
|
|
|
trap "rm -rf $TMPDIR; exit" INT TERM EXIT
|
|
|
|
cd $TMPDIR/$NAME
|
2010-06-11 10:53:38 +00:00
|
|
|
unzip -q $BASE/$1
|
2010-06-28 15:47:26 +00:00
|
|
|
for x in `find . -name "*.png" | grep -v "\.9.png$" | tr "\n" " "`
|
|
|
|
do
|
2010-06-11 10:53:38 +00:00
|
|
|
[ $QUIET ] || echo "Crushing $x"
|
2011-10-28 08:34:29 +00:00
|
|
|
optimize_png $x
|
2010-06-28 15:47:26 +00:00
|
|
|
done
|
|
|
|
cp $BASE/$1 $BASE/$1.old
|
|
|
|
|
2010-06-11 10:53:38 +00:00
|
|
|
[ $QUIET ] || echo "Repacking apk.."
|
2011-01-05 21:10:17 +00:00
|
|
|
aapt p -0 .dat -0 .dict -F $NAME .
|
2010-06-28 15:47:26 +00:00
|
|
|
|
2010-06-11 10:53:38 +00:00
|
|
|
[ $QUIET ] || echo "Resigning with cert: `echo $CERT`"
|
2010-06-28 15:47:26 +00:00
|
|
|
|
2012-08-11 11:04:32 +00:00
|
|
|
[ $QUIET ] || echo java -jar $OUT/../../../host/$OUT_TARGET_HOST/framework/signapk.jar $ANDROID_BUILD_TOP/$CERT $ANDROID_BUILD_TOP/$KEY $NAME signed_$NAME
|
|
|
|
java -jar $OUT/../../../host/$OUT_TARGET_HOST/framework/signapk.jar $ANDROID_BUILD_TOP/$CERT $ANDROID_BUILD_TOP/$KEY $NAME signed_$NAME
|
2010-06-11 10:53:38 +00:00
|
|
|
[ $QUIET ] || echo "Zipalign.."
|
2010-06-28 15:47:26 +00:00
|
|
|
zipalign -f 4 signed_$NAME $BASE/$1
|
2010-06-11 10:53:38 +00:00
|
|
|
if [ ! $QUIET ]; then
|
|
|
|
ls -l $BASE/$1.old
|
|
|
|
ls -l $BASE/$1
|
|
|
|
fi
|
2010-06-28 15:47:26 +00:00
|
|
|
rm $BASE/$1.old
|
|
|
|
else
|
|
|
|
echo "Usage: $0 [apk file]"
|
|
|
|
fi
|
|
|
|
|