[G1 Android Development] Fnorder:
Here's a faster and more paranoid script which I'm tossing into my init.d. It shortens the bootup check by a significant amount, taking only 1.3 seconds as opposed to 9 or so (when the phone is idle, which means longer during boot). It'll also log to /data/zipalign/log.txt, abort overwrites if something breaks, and take a list of .apks via commandline.

It does, however, depend on busybox compiled with the '-c' option for stat. I'm attaching my static-uclibc busybox and will look into a means of quickly getting info using the stock cm busybox, unless wesgarner wants to roll his own with CONFIG_FEATURE_STAT_FORMAT. (I just noticed that there seems to be no source code or .config for CM's busybox o.O)

The phone notices when an APK is altered, so far I have not lost any settings but your mileage may vary. Look over and change the top variables before running, and test with script /data/app/a.single.apk before running with no arguments (which will zipalign everything).

#!/system/xbin/bb/ash
# Automatic ZipAlign by Wes Garner
# ZipAlign files in /data that have not been previously ZipAligned (using md5sum)
#
# Changes on Tue Dec 1 2009 by Fnorder:
# Script will not overwrite original if zipalign errors.
# Script will also abort overwrite if a verification of the temp file fails.
# Script will not attempt zipalign if there's not enough space in /cache.
# Now logs failures and successful zipaligns.
# Modification check now only spawns 3 (+4 for /cache check) processes total, instead of 4 per apk.
# Modification check uses the bytes:mtime of the apk as 'stat' takes 1/20th of the time md5sum does.
# mtime is chopped of it's last digit for a 10 second leeway.
# Time (just to check 60 .apks) reduced from 8.9 seconds to 1.2 seconds on an idle phone, there should be more speed improvements when used at bootup.
# Now checks zipalign -c if mtime differs before aligning, thanks oknowton
#
ZADIR="/data/zipalign"
LOG="$ZADIR/log.txt"
OLDIFS="$IFS"
OPTBB="/system/opt/bin/busybox"
BB="/system/xbin/busybox"
ZIPALIGN="zipalign"
###############################

FREECACHE=$($BB df -B 1 /cache/|$BB tail -1|$BB sed -r 's, +, ,g'|$BB cut -d ' ' -f 4)

_split() {
file="$1"
size="$2"
stat="$2:$3"
}

_check() {
ret=$($OPTBB stat -c "%s:%Y" "$1")
echo ${ret:0:$((${#ret}-1))}
}

if [ ! -d $ZADIR ]; then
mkdir $ZADIR
fi

if [ "x$1" == "xstart" ]; then
APKS=/data/app/*.apk
elif [ "x$*" != "x" ]; then
APKS=$*
else
APKS=/data/app/*.apk
fi

echo "=== `date` ZipAligning Data Apps: " $APKS|tee $LOG

for apk in $($OPTBB stat -c '%n:%s:%Y' /data/app/*.apk|$BB sed -r 's,(^/data/app/|.$),,g'); do
IFS=':'
_split $apk
IFS="$OLDIFS"
if [ -e "$ZADIR/$file.md5" ]; then
MDSAVE=$(cat "$ZADIR/$file.md5")
if [ ! "x$stat" == "x$MDSAVE" ]; then
if [ "$FREECACHE" -lt "$size" ]; then
echo "- $file: Not enough space on /cache ($FREECACHE bytes free, $size bytes needed)"|tee -a $LOG
continue
fi
###
OUT=$($ZIPALIGN -c 4 "/data/app/$file" 2>&1)
if [ $? -eq 0 ]; then
echo "= $file: Already zipaligned, caching mtime."
_check "/data/app/$file" > "$ZADIR/$file.md5"
continue
fi
###
OUT=$($ZIPALIGN -f 4 "/data/app/$file" "/cache/$file.tmp" 2>&1)
if [ $? -gt 0 ]; then
echo "- $file: zipalign error, aborting: $OUT"|tee -a $LOG
rm -f "/cache/$file.tmp"
continue
fi
OUT=$($ZIPALIGN -c 4 "/cache/$file.tmp" 2>&1)
if [ $? -gt 0 ]; then
echo "- $file: verification of /cache/$file.tmp failed: $OUT"|tee -a $LOG
rm -f "/cache/$file.tmp"
continue
fi
cp -f -p "/cache/$file.tmp" "/data/app/$file"
rm "/cache/$file.tmp"
_check "/data/app/$file" > "$ZADIR/$file.md5"
echo "+ $file: zipaligned sucessfully"|tee -a $LOG
else
echo "= $file: ZipAlign already completed"
fi;
else
if [ "$FREECACHE" -lt "$size" ]; then
echo "- $file: Not enough space on /cache ($FREECACHE bytes free, $size bytes needed)"|tee -a $LOG
continue
fi
###
OUT=$($ZIPALIGN -c 4 "/data/app/$file" 2>&1)
if [ $? -eq 0 ]; then
echo "= $file: Already zipaligned, caching mtime."
_check "/data/app/$file" > "$ZADIR/$file.md5"
continue
fi
###
OUT=$(zipalign -f 4 "/data/app/$file" "/cache/$file.tmp" 2>&1)
if [ $? -gt 0 ]; then
echo "- $file: zipalign error, aborting: $OUT"|tee -a $LOG
rm -f "/cache/$file.tmp"
continue
fi
OUT=$($ZIPALIGN -c 4 "/cache/$file.tmp" 2>&1)
if [ $? -gt 0 ]; then
echo "- $file: verification of /cache/$file.tmp failed: $OUT"|tee -a $LOG
rm -f "/cache/$file.tmp"
continue
fi
cp -f -p "/cache/$file.tmp" "/data/app/$file"
rm "/cache/$file.tmp"
_check "/data/app/$file" > "$ZADIR/$file.md5"
echo "+ $file: zipaligned sucessfully"|tee -a $LOG
fi
done

...Now I'm going to nandroid and sic this on everything >.>