標籤:
隨著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以上版本的解譯器)
android刪除無用資源檔的python指令碼