隨著iOS項(xiàng)目的版本不斷迭代,app中冗余文件會(huì)越來(lái)越多,app size也持續(xù)增加旨巷,是時(shí)候需要對(duì)app冗余資源進(jìn)行檢測(cè),對(duì)app進(jìn)行瘦身添忘。
使用方法:
1. 運(yùn)行環(huán)境為mac采呐, 首先準(zhǔn)備好工程源代碼;
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 對(duì)于腳本檢查結(jié)果拴清,最好人工檢查一遍
##### 4) you can add some other types more than png, jpg, gif, wav, m4a 如果需要檢查其他資源,請(qǐng)自行修改腳本会通;
##### 5)默認(rèn)文件后綴都是如@2x.png格式口予,如果后綴格式不同,請(qǐng)自行修改腳本涕侈;
#### set parameters:PrjPath為項(xiàng)目工程所在目錄沪停,包含.m .xib文件;ResPath為被掃描的資源文件目錄裳涛,包含.png .wav
#### xcodeprojPath為工程xcodeproj位置
PrjPath=/Users/webersongao/WorkProject/Doumao_iOS
ResPath=/Users/webersongao/WorkProject/Doumao_iOS/Doumao/Assets.xcassets
xcodeprojPath=/Users/webersongao/WorkProject/Doumao_iOS/DouMao.xcodeproj
if [ -f ~/Desktop/Not_Used_resource_san_result.txt ];then
? ? rm -f ~/Desktop/Not_Used_resource_san_result.txt
fi
cd $PrjPath
files=$(find . -name "*.m" -o -name "*.xib" -o -name "*.mm" -o -name "*.plist")
cd $ResPath
for 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
? ? ? ? ? ? # echo "$png" is used >> ~/Desktop/Used_resource_san_result.txt
? ? ? ? else
? ? ? ? ? ? echo "$png" is used >> ~/Desktop/Not_Used_resource_san_result.txt
? ? ? ? ? ? # 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/Not_Used_resource_san_result.txt ]; then
? ? echo ***************the end of scan. Please see result from resource_san_result.txt
else
? ? echo ***************the end of scan, everything is OK
fi
3. 設(shè)置腳本中參數(shù):
PrjPath為項(xiàng)目工程所在目錄木张,包含.m .xib文件;
ResPath為被掃描的資源文件目錄端三,包含.png .wav舷礼;
xcodeprojPath為工程xcodeproj位置;
例如:
PrjPath=/Users/zhuquan/Documents/secret-develop/Project
ResPath=/Users/zhuquan/Documents/secret-develop/Project/Phoenix/Res
xcodeprojPath=/Users/zhuquan/Documents/secret-develop/Project/Phoenix.xcodeproj
4. 執(zhí)行腳本check.sh;
5. 最后會(huì)出檢測(cè)結(jié)果郊闯,檢測(cè)出來(lái)的冗余資源最好人工檢查一遍妻献。
WebersonGaodeiMac:~ webersongao$ /Users/webersongao/Desktop/check.sh?
./CloudLibrary/disk_transferProgress_high.imageset/disk_transferProgress_high@3x.png is ./CloudLibrary/disk_shujia_gouxuanbg.imageset/disk_shujia_gouxuanbg@2x.png is used
./CloudLibrary/disk_shujia_gouxuanbg.imageset/disk_shujia_gouxuanbg@3x.png is used
./CloudLibrary/disk_diskuse_info01.imageset/disk_diskuse_info01.png is used
./CloudLibrary/disk_diskuse_info00.imageset/disk_diskuse_info00.png is used
./運(yùn)動(dòng)記錄.pngis not packaged***************the end of scan, everythingisO
使用總結(jié):
腳本使用過(guò)程中有一些注意事項(xiàng)如下,
1. 如果資源文件名中含有空格或者/团赁,比如”aaa .png”育拨,該資源無(wú)法正常檢測(cè);
2. 如果資源文件在代碼中被引用了欢摄,但是該引用代碼被注釋掉了熬丧,也無(wú)法成功檢測(cè);
3.?對(duì)于最終腳本輸出的腳本檢查結(jié)果怀挠,最好人工檢查一遍析蝴,有些資源可能并非是冗余資源;
4. 目前腳本中支持的資源類型有.png .jpg .gif .wav .m4a唆香,如果需要檢查其他資源嫌变,請(qǐng)自行修改腳本。