[G1 Android Development] farmatito:
Proposal for improved size optimization of the apk files.

I've developed a script to recompress all the apk's in /system/app.

I gain about 4M of space and so far everything seems to work.

I've noticed only a little slowdown at startup for bigger apk files.

Free space increase is:

Before: /dev/block/mtdblock3 67.5M 62.7M 4.8M 93% /system
After (a reboot): /dev/block/mtdblock3 67.5M 58.6M 8.9M 87% /system

To run the script:

1) unpack the update-*.zip package

2) cd to system/app

3) copy the script there and make it executable

4) run it

#!/bin/sh
# Run it in /system/app dir
# create temporary and final output directory
mkdir -p tmp
mkdir -p out
# Loop through the apk files
for i in `ls *.apk`
do
# Print original size
size=`du -h $i`
echo -n "Processing $size"
name=`basename $i .apk`
# Clean up stale files from previous killed runs
rm -fr tmp/*
cp $i tmp
cd tmp
unzip -q $i
cp ../$i $name.zip
# Generate list of the files to be compressed
list=`find | cut -b 3- | grep -v apk |grep -v zip`
for a in $list
do
# Do not process directories
if [ ! -d $a ] ; then
# Try to optimize png files.
# Needs optipng
infile=`ls $a | grep '\.png$'`
if [ -n "$infile" ] ; then
optipng -q -o 7 $a
fi
# Try to optimize a ogg files.
# Needs sox
infile=`ls $a | grep '\.ogg$'`
if [ -n "$infile" ] ; then
sox $a -C 0 temp.ogg
if [ -e temp.ogg ] ; then
rm $a
mv temp.ogg $a
fi
fi
#Try to optimize a gif files.
# Needs ImageMagick.
infile=`ls $a | grep '\.gif$'`
if [ -n "$infile" ] ; then
convert $a -fuzz 5% -layers Optimize temp.gif
if [ -e temp.gif ] ; then
rm $a
mv temp.gif $a
fi
fi
fi
done
# Recompress
# Needs p7zip-full package
7za a -tzip -mx=9 $name.zip $list 2>&1> /dev/null
# If you want to resign the apk packages
# but it seems not to be needed.
# Needs Signapk package
#java -jar ../signapk.jar ../testkey.x509.pem ../testkey.pk8 $name.zip $name.apk
mv $name.zip ../out/$name.apk
# Clean up
rm -R *
# Print new size
size=`du -h ../out/$name.apk`
echo " ==> $size";
cd ..
done

You need a linux box to run it with optipng, ImageMagick, sox and p7zip-full installed.

./recompress.sh
Processing 236K AlarmClock.apk ==> 176K ../out/AlarmClock.apk
Processing 1.2M Browser.apk ==> 964K ../out/Browser.apk
Processing 24K BugReport.apk ==> 20K ../out/BugReport.apk
Processing 76K Calculator.apk ==> 68K ../out/Calculator.apk
Processing 300K Calendar.apk ==> 192K ../out/Calendar.apk
Processing 132K CalendarProvider.apk ==> 120K ../out/CalendarProvider.apk
Processing 828K Camera.apk ==> 512K ../out/Camera.apk
Processing 32K checkin.apk ==> 28K ../out/checkin.apk
Processing 224K Clicker.apk ==> 176K ../out/Clicker.apk
Processing 272K com.amazon.mp3.apk ==> 236K ../out/com.amazon.mp3.apk
Processing 32K com.android.term.apk ==> 28K ../out/com.android.term.apk
Processing 428K Contacts.apk ==> 284K ../out/Contacts.apk
Processing 76K DownloadProvider.apk ==> 48K ../out/DownloadProvider.apk
Processing 16K DrmProvider.apk ==> 12K ../out/DrmProvider.apk
Processing 364K Email.apk ==> 316K ../out/Email.apk
Processing 88K FilePicker.apk ==> 72K ../out/FilePicker.apk
Processing 48K Ftp.apk ==> 44K ../out/Ftp.apk
Processing 320K Gmail.apk ==> 264K ../out/Gmail.apk
Processing 112K GmailProvider.apk ==> 104K ../out/GmailProvider.apk
Processing 88K GoogleApps.apk ==> 64K ../out/GoogleApps.apk
Processing 88K GoogleContactsProvider.apk ==> 84K ../out/GoogleContactsProvider.apk
Processing 1.8M GoogleMaps.apk ==> 1.1M ../out/GoogleMaps.apk
Processing 36K GooglePartnerSetup.apk ==> 32K ../out/GooglePartnerSetup.apk
Processing 12K GoogleSearch.apk ==> 12K ../out/GoogleSearch.apk
Processing 40K GoogleSettingsProvider.apk ==> 40K ../out/GoogleSettingsProvider.apk
Processing 72K GoogleSubscribedFeedsProvider.apk ==> 64K ../out/GoogleSubscribedFeedsProvider.apk
Processing 244K gtalkservice.apk ==> 228K ../out/gtalkservice.apk
Processing 380K HTCAlbum.apk ==> 356K ../out/HTCAlbum.apk
Processing 16K HtcLog.apk ==> 12K ../out/HtcLog.apk
Processing 8.0K HTMLViewer.apk ==> 8.0K ../out/HTMLViewer.apk
Processing 368K IM.apk ==> 284K ../out/IM.apk
Processing 60K ImProvider.apk ==> 48K ../out/ImProvider.apk
Processing 740K LatinIME.apk ==> 512K ../out/LatinIME.apk
Processing 804K Launcher.apk ==> 732K ../out/Launcher.apk
Processing 36K MediaProvider.apk ==> 36K ../out/MediaProvider.apk
Processing 264K MediaUploader.apk ==> 204K ../out/MediaUploader.apk
Processing 560K Mms.apk ==> 372K ../out/Mms.apk
Processing 452K Music.apk ==> 308K ../out/Music.apk
Processing 36K NetworkLocation.apk ==> 32K ../out/NetworkLocation.apk
Processing 72K PackageInstaller.apk ==> 36K ../out/PackageInstaller.apk
Processing 264K PDFViewer.apk ==> 192K ../out/PDFViewer.apk
Processing 672K Phone.apk ==> 416K ../out/Phone.apk
Processing 12K QxdmLog.apk ==> 12K ../out/QxdmLog.apk
Processing 1.3M Settings.apk ==> 684K ../out/Settings.apk
Processing 24K SettingsProvider.apk ==> 20K ../out/SettingsProvider.apk
Processing 508K SetupWizard.apk ==> 456K ../out/SetupWizard.apk
Processing 64K SoundRecorder.apk ==> 44K ../out/SoundRecorder.apk
Processing 24K SpareParts.apk ==> 16K ../out/SpareParts.apk
Processing 96K Street.apk ==> 88K ../out/Street.apk
Processing 20K Superuser.apk ==> 16K ../out/Superuser.apk
Processing 36K SystemUpdater.apk ==> 32K ../out/SystemUpdater.apk
Processing 280K Talk.apk ==> 228K ../out/Talk.apk
Processing 40K TelephonyProvider.apk ==> 36K ../out/TelephonyProvider.apk
Processing 332K TmoImPlugin.apk ==> 276K ../out/TmoImPlugin.apk
Processing 8.0K UserDictionaryProvider.apk ==> 8.0K ../out/UserDictionaryProvider.apk
Processing 360K Vending.apk ==> 288K ../out/Vending.apk
Processing 44K VoiceDialer.apk ==> 36K ../out/VoiceDialer.apk
Processing 308K VoiceSearch.apk ==> 276K ../out/VoiceSearch.apk
Processing 268K YouTube.apk ==> 184K ../out/YouTube.apk

To upload the apk files to the phone I use this commands:

cd out
for i in `ls *.apk`; do echo $i; adb push $i /system/app; done

948 KB/s (172893 bytes in 0.177s)
AlarmClock.apk
1031 KB/s (979535 bytes in 0.927s)
Browser.apk
453 KB/s (19871 bytes in 0.042s)
BugReport.apk
862 KB/s (64435 bytes in 0.072s)
Calculator.apk
530 KB/s (190508 bytes in 0.350s)
Calendar.apk
462 KB/s (118471 bytes in 0.249s)
CalendarProvider.apk
421 KB/s (519132 bytes in 1.202s)
Camera.apk
402 KB/s (27929 bytes in 0.067s)
checkin.apk
405 KB/s (172711 bytes in 0.415s)
Clicker.apk
567 KB/s (234948 bytes in 0.404s)
com.amazon.mp3.apk
610 KB/s (26733 bytes in 0.042s)
com.android.term.apk
1106 KB/s (283370 bytes in 0.250s)
Contacts.apk
307 KB/s (45466 bytes in 0.144s)
DownloadProvider.apk
130 KB/s (10341 bytes in 0.077s)
DrmProvider.apk
1120 KB/s (316198 bytes in 0.275s)
Email.apk
750 KB/s (65901 bytes in 0.085s)
FilePicker.apk
815 KB/s (42635 bytes in 0.051s)
Ftp.apk
1149 KB/s (265305 bytes in 0.225s)
Gmail.apk
1025 KB/s (100380 bytes in 0.095s)
GmailProvider.apk
822 KB/s (61164 bytes in 0.072s)
GoogleApps.apk
748 KB/s (79472 bytes in 0.103s)
GoogleContactsProvider.apk
1007 KB/s (1048338 bytes in 1.016s)
GoogleMaps.apk
484 KB/s (31000 bytes in 0.062s)
GooglePartnerSetup.apk
167 KB/s (9236 bytes in 0.053s)
GoogleSearch.apk
811 KB/s (37006 bytes in 0.044s)
GoogleSettingsProvider.apk
824 KB/s (59763 bytes in 0.070s)
GoogleSubscribedFeedsProvider.apk
967 KB/s (229351 bytes in 0.231s)
gtalkservice.apk
1030 KB/s (360407 bytes in 0.341s)
HTCAlbum.apk
160 KB/s (10657 bytes in 0.064s)
HtcLog.apk
118 KB/s (5867 bytes in 0.048s)
HTMLViewer.apk
1122 KB/s (284583 bytes in 0.247s)
IM.apk
268 KB/s (47286 bytes in 0.171s)
ImProvider.apk
547 KB/s (518927 bytes in 0.924s)
LatinIME.apk
1136 KB/s (743879 bytes in 0.639s)
Launcher.apk
740 KB/s (32826 bytes in 0.043s)
MediaProvider.apk
774 KB/s (204774 bytes in 0.258s)
MediaUploader.apk
1260 KB/s (373891 bytes in 0.289s)
Mms.apk
1081 KB/s (311112 bytes in 0.280s)
Music.apk
737 KB/s (31684 bytes in 0.041s)
NetworkLocation.apk
773 KB/s (34128 bytes in 0.043s)
PackageInstaller.apk
620 KB/s (191106 bytes in 0.300s)
PDFViewer.apk
1041 KB/s (421547 bytes in 0.395s)
Phone.apk
119 KB/s (8264 bytes in 0.067s)
QxdmLog.apk
758 KB/s (693135 bytes in 0.892s)
Settings.apk
426 KB/s (18149 bytes in 0.041s)
SettingsProvider.apk
683 KB/s (459006 bytes in 0.655s)
SetupWizard.apk
646 KB/s (41663 bytes in 0.062s)
SoundRecorder.apk
261 KB/s (15133 bytes in 0.056s)
SpareParts.apk
897 KB/s (84552 bytes in 0.092s)
Street.apk
267 KB/s (14915 bytes in 0.054s)
Superuser.apk
652 KB/s (28685 bytes in 0.042s)
SystemUpdater.apk
1226 KB/s (226442 bytes in 0.180s)
Talk.apk
783 KB/s (35263 bytes in 0.043s)
TelephonyProvider.apk
908 KB/s (276494 bytes in 0.297s)
TmoImPlugin.apk
115 KB/s (6253 bytes in 0.052s)
UserDictionaryProvider.apk
892 KB/s (290106 bytes in 0.317s)
Vending.apk
318 KB/s (35945 bytes in 0.110s)
VoiceDialer.apk
953 KB/s (277737 bytes in 0.284s)
VoiceSearch.apk
633 KB/s (180351 bytes in 0.277s)
YouTube.apk

After uploading the files the phone needs a little time to reprocess them,

when it is responsive again it is best to reboot it.

No settings are lost as far as i can tell, only some icons and widgets in

home need to be restored.

Hints, improvements and critics are welcome.

This is experimental stuff so USE AT YOUR OWN RISK.