[Android Software/Hacking General [Developers Only]] zeppelinrox:
hah.... well if you want can you see how long this takes?

#!/system/bin/sh
line=================================================
cd "${0%/*}" 2>/dev/null
mount -o remount,rw /data 2>/dev/null
busybox mount -o remount,rw /data 2>/dev/null
# Next line only applies to the init.d boot script. "bootfixalign=1" means run on boot. Anything else disables it.
bootfixalign=1
while [ ! "`ps | grep android | grep -v grep`" ]; do sleep 10; done
if [ "`echo $0 | grep "init\.d"`" ] && [ "$bootfixalign" -ne 1 ]; then exit 69
elif [ -d "/system/etc/init.d" ]; then install /system/etc/init.d/95fixalign
fi
if [ ! "`echo $0 | grep xbin`" ]; then install /system/xbin/fixalign; fi
LOG_FILE=/data/Ran_FixAlign.log
lastran=/data/!FixAlign_Last_Ran
if [ "`grep "zipaligned" $LOG_FILE`" ]; then mv $LOG_FILE $lastran; fi 2>/dev/null
zipalign="yes"
if [ "easyloglulz" ]; then
echo ""
echo $line
echo " -=Fix Alignment=- script by -=zeppelinrox=-"
echo $line
echo ""
sleep 2
id=$(id); id=${id#*=}; id=${id%%[\( ]*}
if [ "$id" != "0" ] && [ "$id" != "root" ]; then
sleep 1
echo " You are NOT running this script as root..."
echo ""
sleep 3
echo $line
echo " ...No SuperUser For You!!"
echo $line
echo ""
sleep 3
echo " ...Please Run as Root and try again..."
echo ""
echo $line
echo ""
sleep 3
exit 69
elif [ ! "`which zipalign`" ]; then
zipalign=
sleep 1
echo " Doh... zipalign binary was NOT found..."
echo ""
sleep 3
echo $line
echo " ...No ZepAligning For You!!"
echo $line
echo ""
sleep 3
echo " Load the XDA SuperCharger thread..."
echo ""
sleep 3
echo " ...and install The SuperCharger Starter Kit!"
echo ""
echo $line
echo ""
sleep 3
su -c "LD_LIBRARY_PATH=/vendor/lib:/system/lib am start -a android.intent.action.VIEW -n com.android.browser/.BrowserActivity -d file://$storage/!SuperCharger.html"
echo ""
echo $line
echo ""
sleep 3
echo $line
echo " So... for now... can ONLY Fix Emissions!"
echo $line
echo ""
sleep 3
elif [ -f "$lastran" ]; then
echo " ZipAlign ALL Apps or only recently installed?"
echo ""
sleep 1
echo -n " Enter (A)LL, any key for recently added: "
stty -icanon min 0 time 200
read how
stty sane
if [ ! "$how" ]; then echo ""; fi
echo ""
case $how in
a|A)rm $lastran;;
*);;
esac
echo $line
echo ""
sleep 1
fi
fi | tee $LOG_FILE
mount -o remount,rw /system 2>/dev/null
busybox mount -o remount,rw /system 2>/dev/null
busybox mount -o remount,rw $(busybox mount | awk '$3=="/system"{print $1,$3}') 2>/dev/null
START=`busybox date +%s`
BEGAN=`date`
TOTAL=`cat /d*/system/packages.xml | grep -c "^<package.*serId"`
INCREMENT=3
PROGRESS=0
PROGRESS_BAR=""
NEW=0; ALIGNED=0; ALREADY=0; FAILED=0; SKIPPED=0
echo " Start Fix Alignment: $BEGAN" >> $LOG_FILE
grep "^<package.*serId" /d*/system/packages.xml > /data/pkgline.tmp
busybox sync
while read pkgline; do
PKGNAME=`echo $pkgline | sed 's%.* name="\(.*\)".*%\1%' | cut -d '"' -f1`
CODEPATH=`echo $pkgline | sed 's%.* codePath="\(.*\)".*%\1%' | cut -d '"' -f1`
DATAPATH=/d*/d*/$PKGNAME
PKGUID=`echo $pkgline | sed 's%.*serId="\(.*\)".*%\1%' | cut -d '"' -f1`
PROGRESS=$(($PROGRESS+1))
PERCENT=$(( $PROGRESS * 100 / $TOTAL ))
if [ "$PERCENT" -eq "$INCREMENT" ]; then
INCREMENT=$(( $INCREMENT + 3 ))
PROGRESS_BAR="$PROGRESS_BAR="
fi
clear
echo ""
echo -n " >"
echo -e "\r $PROGRESS_BAR>"
echo " \"Fix Alignment\" by -=zeppelinrox=-"
echo -n " >"
echo -e "\r $PROGRESS_BAR>"
echo ""
echo " Processing Apps - $PERCENT% ($PROGRESS of $TOTAL)"
echo "" | tee -a $LOG_FILE
echo " Fix Aligning $PKGNAME..." | tee -a $LOG_FILE
echo ""
if [ -e "$CODEPATH" ]; then
if [ ! -f "$lastran" ] || [ "$CODEPATH" -nt "$lastran" ] && [ "$zipalign" ]; then NEW=$(($NEW+1))
if [ "$(busybox basename $CODEPATH)" = "framework-res.apk" ] || [ "$(busybox basename $CODEPATH)" = "SystemUI.apk" ] || [ "$(busybox basename $CODEPATH)" = "com.htc.resources.apk" ]; then
echo " NOT ZipAligning (Problematic) $CODEPATH..." | tee -a $LOG_FILE
SKIPPED=$(($SKIPPED+1))
skippedapp=`echo "$skippedapp, $(busybox basename $CODEPATH)" | sed 's/^, //'`
else
zipalign -c 4 $CODEPATH
ZIPCHECK=$?
if [ "$ZIPCHECK" -eq 1 ]; then
echo " ZipAligning $CODEPATH..." | tee -a $LOG_FILE
zipalign -f 4 $CODEPATH /cache/$(busybox basename $CODEPATH)
rc="$?"
if [ "$rc" -eq 0 ]; then
if [ -e "/cache/$(busybox basename $CODEPATH)" ]; then
busybox cp -f -p /cache/$(busybox basename $CODEPATH) $CODEPATH | tee -a $LOG_FILE
ALIGNED=$(($ALIGNED+1))
else
echo " ZipAligning $CODEPATH... Failed (No Output File!)" | tee -a $LOG_FILE
FAILED=$(($FAILED+1))
failedapp=`echo "$failedapp, $(busybox basename $CODEPATH)" | sed 's/^, //'`
fi
else echo " ZipAligning $CODEPATH... Failed (rc: $rc!)" | tee -a $LOG_FILE
FAILED=$(($FAILED+1))
failedapp=`echo "$failedapp, $(busybox basename $CODEPATH)" | sed 's/^, //'`
fi
if [ -e "/cache/$(busybox basename $CODEPATH)" ]; then busybox rm /cache/$(busybox basename $CODEPATH); fi
else
echo " ZipAlign already completed on $CODEPATH" | tee -a $LOG_FILE
ALREADY=$(($ALREADY+1))
fi
fi
fi
APPDIR=`busybox dirname $CODEPATH`
if [ "$APPDIR" = "/system/app" ] || [ "$APPDIR" = "/vendor/app" ] || [ "$APPDIR" = "/system/framework" ] && [ "`busybox stat -c "%u %g %a" $CODEPATH`" != "0 0 644" ]; then
busybox chown 0:0 $CODEPATH
busybox chmod 644 $CODEPATH
elif [ "$APPDIR" = "`ls -d /d*/app`" ] && [ "`busybox stat -c "%u %g %a" $CODEPATH`" != "1000 1000 644" ]; then
busybox chown 1000:1000 $CODEPATH
busybox chmod 644 $CODEPATH
elif [ "$APPDIR" = "`ls -d /d*/app-private`" ] && [ "`busybox stat -c "%u %g %a" $CODEPATH`" != "1000 $PKGUID 640" ]; then
busybox chown 1000:$PKGUID $CODEPATH
busybox chmod 640 $CODEPATH
fi
if [ -d "$DATAPATH" ]; then
if [ "`busybox stat -c "%u %g %a" $DATAPATH`" != "$PKGUID $PKGUID 755" ]; then
busybox chown $PKGUID:$PKGUID $DATAPATH
busybox chmod 755 $DATAPATH
fi
DIRS=`busybox find $DATAPATH -mindepth 1 -type d`
for file in $DIRS; do
PERM=755
NEWUID=$PKGUID
NEWGID=$PKGUID
FNAME=`busybox basename $file`
case $FNAME in
lib)if [ "`busybox stat -c %a $file`" != "755" ]; then busybox chmod 755 $file; fi
NEWUID=1000
NEWGID=1000
PERM=755;;
shared_prefs|databases)if [ "`busybox stat -c %a $file`" != "771" ]; then busybox chmod 771 $file; fi
PERM=660;;
cache)if [ "`busybox stat -c %a $file`" != "771" ]; then busybox chmod 771 $file; fi
PERM=600;;
files)if [ "`busybox stat -c %a $file`" != "771" ]; then busybox chmod 771 $file; fi
PERM=775;;
*)if [ "`busybox stat -c %a $file`" != "771" ]; then busybox chmod 771 $file; fi
PERM=771;;
esac
if [ "`busybox stat -c "%u %g" $file`" != "$NEWUID $NEWGID" ]; then busybox chown $NEWUID:$NEWGID $file; fi
busybox find $file -type f -maxdepth 2 ! -perm $PERM -exec busybox chmod $PERM {} ';'
busybox find $file -type f -maxdepth 1 ! -user $NEWUID -exec busybox chown $NEWUID {} ';'
busybox find $file -type f -maxdepth 1 ! -group $NEWGID -exec busybox chown :$NEWGID {} ';'
done
fi
echo " Fixed Permissions..." | tee -a $LOG_FILE
fi
done < /data/pkgline.tmp
busybox sync
rm /data/pkgline.tmp
rm $lastran 2>/dev/null
mount -o remount,ro /system 2>/dev/null
busybox mount -o remount,ro /system 2>/dev/null
busybox mount -o remount,ro $(busybox mount | awk '$3=="/system"{print $1,$3}') 2>/dev/null
STOP=`busybox date +%s`
ENDED=`date`
RUNTIME=`busybox expr $STOP - $START`
HOURS=`busybox expr $RUNTIME / 3600`
REMAINDER=`busybox expr $RUNTIME % 3600`
MINS=`busybox expr $REMAINDER / 60`
SECS=`busybox expr $REMAINDER % 60`
RUNTIME=`busybox printf "%02d:%02d:%02d\n" "$HOURS" "$MINS" "$SECS"`
echo "" | tee -a $LOG_FILE
echo $line | tee -a $LOG_FILE
if [ "$zipalign" ]; then
echo "" | tee -a $LOG_FILE
sleep 1
echo " Done \"ZepAligning\" ALL data and system APKs..." | tee -a $LOG_FILE
echo "" | tee -a $LOG_FILE
sleep 1
echo " $NEW Apps were checked (added since last run)" | tee -a $LOG_FILE
echo " $ALIGNED Apps were zipaligned" | tee -a $LOG_FILE
echo " $ALREADY Apps were already zipaligned" | tee -a $LOG_FILE
echo " $FAILED Apps were NOT zipaligned due to error" | tee -a $LOG_FILE
if [ "$failedapp" ]; then echo " ($failedapp)" | tee -a $LOG_FILE; fi
echo " $SKIPPED Problematic Apps were skipped" | tee -a $LOG_FILE
if [ "$skippedapp" ]; then echo " ($skippedapp)" | tee -a $LOG_FILE; fi
echo "" | tee -a $LOG_FILE
echo " $TOTAL Apps were processed!" | tee -a $LOG_FILE
echo "" | tee -a $LOG_FILE
sleep 1
echo " ...Say Hello To Optimized Apps!"
echo ""
echo $line | tee -a $LOG_FILE
fi
echo "" | tee -a $LOG_FILE
sleep 1
echo " FIXED Permissions For ALL $TOTAL Apps..." | tee -a $LOG_FILE
echo "" | tee -a $LOG_FILE
sleep 1
echo " ...Say Buh Bye To Force Close Errors!"
echo ""
echo $line
echo ""
sleep 1
echo " Start Time: $BEGAN" | tee -a $LOG_FILE
echo " Stop Time: $ENDED" | tee -a $LOG_FILE
echo "" | tee -a $LOG_FILE
echo " Completion Time: $RUNTIME" | tee -a $LOG_FILE
echo "" | tee -a $LOG_FILE
sleep 1
echo " See $LOG_FILE for more details!"
echo ""
sleep 1
echo " ==============================" | tee -a $LOG_FILE
echo " ) Fix Alignment Completed! (" | tee -a $LOG_FILE
echo " ==============================" | tee -a $LOG_FILE
echo "" | tee -a $LOG_FILE
sleep 1
if [ ! "`ps | grep android | grep -v grep`" ]; then
exec > /data/BootLog_FixAlign.log 2>&1
Fix_Align & exit 0
fi
if [ -d "/system/etc/init.d" ]; then Configure; fi
Fix_Align
exit 0

I hope that checking to see whether or not to do chown/chmod doesn't slow it down.

I think it shouldn't because performing a comparison test should be faster than applying chown and chmod