#!/system/bin/sh #Modified script by BanditGR @ xiaomi-miui.gr #1.0 - Initial corrected version #1.1 - Log file optimizations, clean ZIPALIGNDB every x days for better efficiency #Define file locations LOG_FILE=/data/zipalign.log; ZIPALIGNDB=/data/zipalign.db; ZIPALIGNTM=/data/zipalign_execute_clean.log; #Interval between DB file clean, in seconds, 604800=1 week, 432000=5 days RUN_EVERY=432000 #If Log file exists and size is greater than 50k then delete it if [ -e $LOG_FILE ]; then LOGSIZE=`stat -c %s "$LOG_FILE"` if [ $LOGSIZE -gt 50000 ];then rm $LOG_FILE; fi; fi; #If file to determine DB clean exists, execute the following code if [ -e $ZIPALIGNTM ]; then LASTRUN=`stat -c %Y "$ZIPALIGNTM"` # Get current date in epoch format CURRDATE=`date +%s` # Check the interval INTERVAL=$(expr $CURRDATE - $LASTRUN) # If interval is more than the set one, then run the DB clean script if [ $INTERVAL -gt $RUN_EVERY ];then echo "More than $RUN_EVERY seconds have elapsed since last DB clean..." | tee -a $LOG_FILE; echo "Deleting DB file $ZIPALIGNDB..." | tee -a $LOG_FILE; if [ -e $ZIPALIGNDB ]; then rm $ZIPALIGNDB; fi; #Delete file to check DB clean, it will be re-created later on rm $ZIPALIGNTM; else echo "Less than $RUN_EVERY seconds have elapsed since last DB clean..." | tee -a $LOG_FILE; echo "No need to clean DB file. Continuing with main script..." | tee -a $LOG_FILE; fi; fi; #If file to determine DB clean doesn't exist, create it if [ ! -f $ZIPALIGNTM ]; then touch $ZIPALIGNTM; echo "This file is used to determine when to clean the zipalign DB file, do NOT delete, unless you know what you are doing." | tee -a $ZIPALIGNTM; fi; #If DB file doesn't exist, create it if [ ! -f $ZIPALIGNDB ]; then touch $ZIPALIGNDB; fi; #Set a working path cd /data/ echo "Starting FV Automatic ZipAlign $( date +"%m-%d-%Y %H:%M:%S" )" | tee -a $LOG_FILE; for DIR in /system/app /data/app; do cd $DIR; #for APK in *.apk; do for APK in `busybox find -iname "*.apk"`; do if [ $APK -ot $ZIPALIGNDB ] && [ $(grep "$DIR/$APK" $ZIPALIGNDB|wc -l) -gt 0 ]; then echo "Already checked: $DIR/$APK" | tee -a $LOG_FILE; else ZIPCHECK=`/system/xbin/zipalign -c -v 4 $APK | grep FAILED | wc -l`; if [ $ZIPCHECK == "1" ]; then echo "Now aligning: $DIR/$APK" | tee -a $LOG_FILE; APKFILE="$(basename $APK)" /system/xbin/zipalign -v -f 4 $APK /data/local/$APKFILE; busybox mount -o rw,remount /system; #Copy file to original dir #cp -f -p /data/local/$APKFILE $DIR/$APK; echo "Now copying aligned file /data/local/$APKFILE to original directory, preserving permissions: $DIR/$APK" | tee -a $LOG_FILE; cat "/data/local/$APKFILE" > "$DIR/$APK"; #Set proper permissions aka 644 #chmod 644 $DIR/$APK; #Remove temporary file from /data/local echo "Now removing temporary file: /data/local/$APKFILE, from /data/local" | tee -a $LOG_FILE; rm -f /data/local/$APKFILE; grep "$DIR/$APK" $ZIPALIGNDB > /dev/null || echo $DIR/$APK >> $ZIPALIGNDB; else echo "Already aligned: $DIR/$APK" | tee -a $LOG_FILE; grep "$DIR/$APK" $ZIPALIGNDB > /dev/null || echo $DIR/$APK >> $ZIPALIGNDB; fi; fi; done; done; busybox mount -o ro,remount /system; touch $ZIPALIGNDB; echo "Automatic ZipAlign finished at $( date +"%m-%d-%Y %H:%M:%S" )" | tee -a $LOG_FILE;