To clean up useless resources, the first thing to do is to find them, and we know that the Anroid SDK has a tool called lintthat can help us look at problems in the project, one of which is to find useless resources, so this is a simple step, Execute the following command directly on the project that needs to be cleaned:
" unusedresources " [Project_path] > Result.txt
Or
The first step:
Using the Android-sdk-windows\tools\lint.bat tool
Export test results to a text file first
Code: Lint Project Catalog >d:/temp/res.txt
Export Engineering test results from the specified project catalog to Res.txt
After the execution of the above command after the project about the unusedresources of the problem is saved to Result.txt , first look at the contents of Result.txt
Res/values/arrays.xml:202:warning:the resource R.array.msg_my_friend_category_items appears to be unused [ Unusedresources]^m~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~res/layout/back_up_level_list.xml:warning:the Resource R.layout.back_up_level_list appears to be unused [unusedresources]res/layout/backup_list.xml:warning:the resource R.layout.backup_list appears to be unused [unusedresources]res/layout/backup_listview_item.xml:warning:the resource R.layout.backup_listview_item appears to be unused [unusedresources]
Java code
ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileReader; Public classMain { Public Static voidMain (string[] args) {/*if (args.length<3) {System.out.printf ("delunuse [????? ¼] [?????????] \ r \ n "); Return } delunused (args[1],args[2]); */delunused ("d:/androids/italk/", "D:/androids/italk/italk_unused.txt"); } /*(non-java-doc) * @see java.lang.object#object ()*/ PublicMain () {Super(); } Public Static voiddelunused (String projectpath,string projectlogfile) {Try{BufferedReader Reader=NewBufferedReader (NewFileReader (projectlogfile)); String Line; intCount = 0; while(line = Reader.readline ())! =NULL) { if(Line.contains ("unusedresources") &&!line.contains ("Res\\value") &&!line.contains ("AppCompat")) ) {count++; intEnd = Line.indexof (":"); if(End! =-1) {String file= line.substring (0, end); //if (!file.equals ("Color.xml") &&!file.equals (color.xml) &&!file.equals (color.xml)){String F= ProjectPath +file; System.out.println ("Del" +f); NewFile (f). Delete (); } } } } } Catch(Exception e) {e.printstacktrace (); System.out.printf (E.getmessage ()); } } }
Or
String ProjectPath = "* * *"; BufferedReader Reader=NewBufferedReader (NewFileReader ("/home/angeldevil/result.txt")); String Line;intCount = 0; while(line = Reader.readline ())! =NULL) { if(Line.contains ("unusedresources") &&!line.contains ("Res/value") &&!line.contains ("AppCompat")) ) {count++; intEnd = Line.indexof (":"); if(End! =-1) {String file= line.substring (0, end); String F= ProjectPath +file; System.out.println (f); NewFile (f). Delete (); } }}
The program is very simple, just a few lines of code, is to read every line of the result.txt file, according to their own requirements to filter out the rows do not need to process (for example, I just want to clean anim, drawable and layout, so filter out res/ Value directory information, and ignore AppCompat related information), each line ":" Before the string is the file name, found the file name is good to handle, directly delete, or print out, or write to a file to confirm the confirmation to delete, When the results are written to a file, we can see if the file is now useless but still do not want to delete the file, if so, the processing method is very simple, remove this line or simply make a mark, such as the previous #, and then read the file to do not mark the row corresponding file deleted.
It looks simple, but there are a few things to note:
- Some layout files may have been used by you before, and in the corresponding Java file, use the ID in the layout layouts, such as the Onclicklistener for some ID controls, and the IDs in the onclick switch...case. But finally do not use this layout, this layout is Unusedresource, but previously referenced in Java code in the layout of some of the ID of the reference has not been cleared, at this time to delete this layout will be an error, You can choose to clean up the Java code for the error because they are actually dead code. or each time you clean up a portion of the resource files, such as cleaning up layout, and then clean up the drawable, for each item can also be based on the rules of the file name to clean up a small portion at a time, such as only clean res/layout in the beginning of item_of files.
- Lint analysis seems to be not completely accurate, or not enough intelligence, such as a drawable is only a layout reference, and this layout is unused, lint may not find this drawable is unused, This requires that we repeat the previous steps several times until count is 0.
- Lint can only analyze the resource file, which is the Res directory file, if you want to parse the Java file also need other methods, and, it is possible that a resource file is referenced by a Java file, and this Java file is unused, so this resource file will escape lint check, So we'd better clean up the Java file and clean up the resource file.
Summarize
Cleaning up Resources is two steps:
- Find unused resources
- Clean up these resources on demand
by Ucdetector and Lint basically can detect the project Unusedresource related problems, generally like method visibility, a method is useless to this problem, do not deal with, To the corresponding file when the manual processing, the main treatment is that some files or classes are not used, there is a test report, analysis of the report on the line. This kind of report usually reports a problem per line and the text of each line is regular (the tool generates a certain regularity), filtering out the information we need on a regular basis.
Remove unused resources from Android projects