咱們直接在公司項目中使用,效果良好!python
分享出腳本代碼,但願對Android研發的同窗有幫助。android
提示,初學python,開發環境是Sublime Text 2,直接Ctrl+B的,其餘環境下沒調試過。應該差很少^^正則表達式
################################################# #環境: win + python 2.7 #做者:馬波 #郵箱:mabo02@baidu.com #部門:hao123-無線 #說明:首次使用時lint分析會耗幾分鐘,請耐心等待。 # 使用前先clean工程,確保工程bin下從新生成dex, # 以便lint進行分析。若是要lint從新分析多餘 # 資源,須要刪掉(2)txt記錄文件,(1)(3)(4)須要 # 根據咱們的實際工程手動設置。 # 若是清除資源後,工程缺乏文件而報錯(極少 # 狀況),嘗試經過svn恢復該文件便可。 ################################################# import subprocess import re import os import time import thread #(1)工程位置 projectPath="D:\/hao123\/code\/client-android" #(2)lint輸出txt記錄文件 txt="D:\/hao123_unused_res.txt" #(3)正則表達式,清除drawable和layout下多餘的jpg/png/xml, # 而且排除以sailor_|wenku_|zeus_|bdsocialshare_|floating_life_|weather_info_icon_|anthology_開頭的文件 regex = re.compile(r"^res\\(drawable(-land)?(-[xn]?[mhlo](dpi))|layout)?\\(?!(sailor_|wenku_|zeus_|bdsocialshare_|floating_life_|weather_info_icon_|anthology_))[0-9a-zA-Z_\.]*\.(jpg|png|xml)", re.IGNORECASE) #(4)lint.bat的位置 lint="D:\/sdk\/tools\/lint.bat" isgotTxt=False def timer(interval): while not isgotTxt: print 'Lint is analyzing: %s'%time.ctime() time.sleep(interval) if not os.path.exists(txt): thread.start_new_thread(timer, (5,)) cmd=lint+' --check "UnusedResources" "'+ projectPath +'" >'+txt p = subprocess.Popen(cmd, shell = True,stdout = subprocess.PIPE,stdin = subprocess.PIPE,stderr = subprocess.PIPE) p.wait() fobj=open(txt,'r') isgotTxt=True i=0 j=0 for line in fobj: #print str(i)+":"+line match=regex.match(line) if match: i=i+1 filename=projectPath+"\/"+match.group().replace('\\',"\\/") try: print filename os.remove(filename) j=j+1 print "was deleted!" except WindowsError: print "is not exists" pass print "Total Unused Resources = "+str(i) print "Total deleted Resources = "+str(j)