随着android项目的进行,如果没有及时删除无用的资源时安装包会越来越大,是时候整理一下废弃资源缩小压缩包了,少年!
其实判断一个资源(drawable,layout)是否没有被使用很简单,文件名(不包含后缀)去文件中查找一下就行了,没找到就没有用到,直接删除了就可以了
python的字符串处理相对简单一点,就写了个脚本,供大家参考:
import os
import os.path
rootdir = ‘D:\HitFm_v2.1.6\hitfm\src‘
pngFiles = []
xmlFiles = []
javaFiles = []
tempFiles = []
for parent,dirnames,filenames in os.walk(rootdir):
for filename in filenames:
if(filename.find(‘.png‘) != -1):pngFiles.append(os.path.join(parent,filename))
if(filename.find(‘.jpg‘) != -1):pngFiles.append(os.path.join(parent,filename))
if(filename.find(‘.xml‘) != -1):xmlFiles.append(os.path.join(parent,filename))
if(filename.find(‘.java‘) != -1):javaFiles.append(os.path.join(parent,filename))
for pngFile in pngFiles:
pngName = pngFile.split(‘\\‘)[-1]
pngnowName = pngName.split(‘.‘)[0]
if(pngFile.find(‘res‘) == -1):continue
bFind = False
for javaFile in javaFiles:
f = open(javaFile, ‘rb‘)
javaStr = f.read().decode()
if(javaStr.find(pngnowName) != -1):bFind = True
for xmlFile in xmlFiles:
f = open(xmlFile, ‘rb‘)
javaStr = f.read().decode()
if(javaStr.find(pngnowName) != -1):bFind = True
if(bFind == False):
print (pngFile)
os.remove(pngFile)
for xmlFile in xmlFiles:
xmlName = xmlFile.split(‘\\‘)[-1]
xmlnowName = xmlName.split(‘.‘)[0]
if(xmlFile.find(‘res‘) == -1):continue
bFind = False
for javaFile in javaFiles:
f = open(javaFile, ‘rb‘)
javaStr = f.read().decode()
if(javaStr.find(xmlnowName) != -1):bFind = True
for xmlFile2 in xmlFiles:
f = open(xmlFile2, ‘rb‘)
javaStr = f.read().decode()
if(javaStr.find(xmlnowName) != -1):bFind = True
if(bFind == False):
print (xmlFile)
tempFiles.append(xmlFile)
for tempFile in tempFiles:
print (tempFile)
os.remove(tempFile)
使用的时候只需要把rootdir = ‘D:\HitFm_v2.1.6\hitfm\src‘修改为自己的代码目录,在命令行里执行xx.py即可(需要安装python3.0以上版本的解释器)