Use python to automatically clear unnecessary resources in the Android Project

Source: Internet
Author: User

We use it directly in our company projects, and the results are good! Share the script code, hoping to help Android Developers. Note: For python beginners, the development environment is Sublime Text 2, and Ctrl + B is used directly. It has not been debugged in other environments. It should be similar to ^ copy the code ################################## ############### environment: win + python 2.7 # Author: Ma bo # Email: mabo02@baidu.com # Department: hao123-Wireless # Note: The first use of lint analysis will take a few minutes, please be patient. # Clean the project before use to ensure that dex is regenerated under the project bin, # So that lint can be analyzed. If you want lint to re-analyze redundant # resources, you need to delete (2) txt record files, (1) (3) (4) need # manually set according to our actual project. # If an error is reported (rarely #) when a file is missing in the project after the resource is cleared, use svn to restore the file. ######################################## ######### Import subprocess import reimport osimport timeimport thread # (1) project location projectPath = "D: \/hao123 \/code \/client-android" # (2) lint output txt record file txt = "D: \/hao123_unused_res.txt "# (3) Regular Expression, clear unnecessary jpg/png/xml in drawable and layout, # exclude files starting with 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 location lint = "D: \/sdk \/tools \/lint. bat "isgotTxt = Falsedef 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 = Truei = 0j = 0for 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! "Partition t WindowsError: print" is not exists "pass print" Total Unused Resources = "+ str (I) print" Total deleted Resources = "+ str (j)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.