随着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/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. 设置脚本中参数:
PrjPath为项目工程所在目录,包含.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. 执行脚本check.sh;
5. 最后会出检测结果,检测出来的冗余资源最好人工检查一遍。
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
./运动记录.pngis not packaged***************the end of scan, everythingisO
使用总结:
脚本使用过程中有一些注意事项如下,
1. 如果资源文件名中含有空格或者/,比如”aaa .png”,该资源无法正常检测;
2. 如果资源文件在代码中被引用了,但是该引用代码被注释掉了,也无法成功检测;
3. 对于最终脚本输出的脚本检查结果,最好人工检查一遍,有些资源可能并非是冗余资源;
4. 目前脚本中支持的资源类型有.png .jpg .gif .wav .m4a,如果需要检查其他资源,请自行修改脚本。