標籤:
iOS項目冗餘資源掃描指令碼
隨著iOS項目的版本不斷迭代,app中冗餘檔案會越來越多,app size也持續增加,是時候需要對app冗餘資源進行檢測,對app進行瘦身。
使用方法:
1. 運行環境為mac, 首先準備好工程原始碼;
2. 建立check.sh,將下面的代碼粘貼到check.sh中並且儲存;(可將txt檔案改成sh尾碼)
#!/bin/sh ##### several cases that the scripts does not work:##### 1) there is space or slash in the resources file, such as "aaa .png" 資源檔名中含有空格或者/##### 2) reference resources in commented code 資源引用代碼被注釋了##### 3) you need to manually checked the resources one by one in the result 對於指令碼檢查結果,最好人工檢查一遍##### 4) you can add some other types more than png, jpg, gif, wav, m4a 如果需要檢查其他資源,請自行修改指令碼;##### 5)預設檔案尾碼都是如@2x.png格式,如果尾碼格式不同,請自行修改指令碼; #### set parameters:PrjPath為項目工程所在目錄,包含.m .xib檔案;ResPath為被掃描的資源檔目錄,包含.png .wav#### xcodeprojPath為工程xcodeproj位置PrjPath=/Users/.......ResPath=/Users/.......xcodeprojPath=/Users/....../******.xcodeproj if [ -f ~/Desktop/resource_san_result.txt ];then rm -f ~/Desktop/resource_san_result.txtfi cd $PrjPathfiles=$(find . -name "*.m" -o -name "*.xib" -o -name "*.mm" -o -name "*.plist") cd $ResPathfor png in $(find . -name "*.png" -o -name "*.jpg" -o -name "*.gif" -o -name "*.wav" -o -name "*.m4a")do basename=‘basename/‘$png basename=${basename##*/} # echo $basename if [ "${basename##*.}" == "png" ];then echo $basename|grep -q @2x.png if [ $? -eq 0 ];then name=${basename%%@2x.png} else echo $basename|grep -q @3x.png if [ $? -eq 0 ];then name=${basename%%@3x.png} else name=${basename%.png} fi fi elif [ "${basename##*.}" == "jpg" ];then echo $basename|grep -q @2x.jpg if [ $? -eq 0 ];then name=${basename%%@2x.jpg} else echo $basename|grep -q @3x.jpg if [ $? -eq 0 ];then name=${basename%%@3x.jpg} else name=${basename%%.jpg} fi fi elif [ "${basename##*.}" == "gif" ];then echo $basename|grep -q @2x.gif if [ $? -eq 0 ];then name=${basename%%@2x.gif} else echo $basename|grep -q @3x.gif if [ $? -eq 0 ];then name=${basename%%@3x.gif} else name=${basename%%.gif} fi fi elif [ "${basename##*.}" == "wav" ];then name=${basename%%.wav} elif [ "${basename##*.}" == "m4a" ]; then name=${basename%%.m4a} else name=‘‘ fi if [ ${#name} -gt 0 ];then # # name=${name%%[0-9]*} cd $PrjPath if grep -q $name $files;then echo "$png" is used else cd $xcodeprojPath if grep -q $name project.pbxproj;then echo "$png" is not used >> ~/Desktop/resource_san_result.txt else echo "$png" is not packaged fi fi else echo name is empty fi done if [ -f ~/Desktop/resource_san_result.txt ]; then echo ***************the end of scan. Please see result from resource_san_result.txtelse echo ***************the end of scan, everything is OKfi
3. 設定指令碼中參數:
PrjPath為項目工程所在目錄,包含.m .xib檔案;
ResPath為被掃描的資源檔目錄,包含.png .wav;
xcodeprojPath為工程xcodeproj位置;
例如:
PrjPath=/Users/zhuquan/Documents/secret-develop/ProjectResPath=/Users/zhuquan/Documents/secret-develop/Project/Phoenix/ResxcodeprojPath=/Users/zhuquan/Documents/secret-develop/Project/Phoenix.xcodeproj
4. 執行指令碼check.sh;
5. 最後會出檢測結果,檢測出來的冗餘資源最好人工檢查一遍。
[zhuquandeMacBook-Pro:Desktop zhuquan$ ./check.sh./1.png is used./2.png is used./3.png is used./4.png is used./5.png is used./ajax-loader.gif is not packaged./動作活動.png is not packaged***************the end of scan, everything is OK
使用總結:
指令碼使用過程中有一些注意事項如下,
1. 如果資源檔名中含有空格或者/,比如”aaa .png”,該資源無法正常檢測;
2. 如果資源檔在代碼中被引用了,但是該引用代碼被注釋掉了,也無法成功檢測;
3. 對於最終指令碼輸出的指令碼檢查結果,最好人工檢查一遍,有些資源可能並非是冗餘資源;
4. 目前指令碼中支援的資源類型有.png .jpg .gif .wav .m4a,如果需要檢查其他資源,請自行修改指令碼。
iOS項目冗餘資源掃描指令碼