android刪除無用資源檔的python指令碼

來源:互聯網
上載者:User

標籤:

隨著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指令碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.