This tool is on my github. Address: Https://github.com/NashLegend/AndroidResourceCleaner
Many people know that Androidunusedresources.jar is a tool that can list the useless resources in an Android project. However, what it does is stop here, after listing all the useless resources, developers have to manually delete these files, this is a technical content but annoying physical activity, but as a programmer, there is a solution, why don't we write a program, let the program to achieve this function?
The function to be implemented should look like this:
1. Read the list of useless resources exported by Androidunusedresources.jar.
2, clean up useless resources.
For drawable and layout and other resources, of course, is to delete the file directly, because a file corresponds to a resource, and for other resources is not such a matter, a file may have a lot of resources, so can only be deleted in the file part, but to keep another part.
The procedure is broadly as follows:
First, Main.
public static void Main (string[] args) {if (Args.length > 0) {unusedcleaner (args[0]);}}
Called the Unusedcleaner method, we use this method to clean up the useless resources, the passed parameter is a list of useless resource files exported by Androidunusedresources.jar.
Then look at the Unusedcleaner method.
currents is all the resource IDs listed before the next file is found. Because a file may correspond to multiple IDs, there may be a many-to-many relationship. Static arraylist<typesource> currents = new arraylist<> ();static Boolean lastispath = false;public static void unusedcleaner (String filePath ) {ArrayList<TypeSource> files = new ArrayList<> ();//List of resources to be cleaned try { File file = new file (FilePath);//androidunusedresources exported list of useless resource files if (File.isfile () && file.exists ()) {inputstreamreader read = new inputstreamreader ( New fileinputstream (file), "Utf-8"); Bufferedreader bufferedreader = new bufferedreader (read); string linetxt = null;while ((Linetxt = bufferedreader.readline ()) != NULL) {if (!parsetype (linetxt)) { //read data row by line , and extract the data. The Parsetype method can determine whether the row represents a resource name found. The corresponding format such as drawable: bg_list //if not, then this line is likely to be the file corresponding to the resource. String trim = linetxt.trim ();if (New file (Trim). Exists ()) { // Simple with the existence of a file to determine whether this row is a file name, if so, all the corresponding resource names in the Currents list must be included in this file//add it to a list of resources to be cleaned, correspond these resources and files, and add to the list for (Iterator<typesource> iterator = currents.iterator (); iterator.hasnext ();) {TypeSource typeSource = (Typesource) iterator.next (). Clone (); typesource.path = trim;typesource.xmltag = typesource.getxmltag (); Files.add (Typesource);} Lastispath = true;} else {continue;}}} Read.close ();} else {system.out.println ("Nofile");}} catch (exception e) {system.out.println ("Failed"); E.printstacktrace ();} //all found out, Cleaning up these resources one by one is the way to clean up files, which are Typesource, and the Typesource class represents a resource. for (Iterator<typesource> iterator = files.iterator (); iterator.hasnext ();) {TypeSource typeSource = (Typesource) iterator.next (); System.out.println (Typesource); Typesource.cleanself ();} System.out.println ("Done");} Public static boolean parsetype (string linetxt) { //determines whether the current row is a resource name. If so, add to currents, if the previous resource is a file, then empty currents, because the previous currents has been added to the list to be cleaned string reg = "((drawable) | ( Layout) | (dimen) | (String) | (attr) | (style) | (styleable) | (color) | (ID) | (Anim)) \\s*:\\s* (\\s+) "; Matcher matcher = pattern.compile (REG). Matcher (linetxt);//use regular match for current row if (Matcher.find ()) {if (Lastispath) {currents.clear ();} lastispath = false; Typesource typesource = new typesource (); Typesource.type = matcher.group (1); Typesource.name = matcher.group (n); Currents.add (typesource); return true;} else {return faLSE;}} static class typesource {string type = "";// type string name = ""; name attribute in// xml string xmltag = "";// xml tag name string path = "" ;// belongs to which file Public string getxmltag () {if ("Styleable". Equals (type)) {return "Declare-styleable";} else {return type;}} @Overridepublic string tostring () {return type + | + name + " | " + xmlTag + " | " + path;} /** * A single delete, anyway quickly, does not consider efficiency. If a file corresponds to all the resources listed to the unified deletion should be quick, but here lazy */public void cleanself () {try {if (type == null) {return;} if (Type.equals ("drawable") | | type.equals ("layout") | | type.equals ("Anim")) {new file (path). Delete (); else if (type.equals ("id") | | type.equals ("")) {// skipped ID resource, if you want to delete it, it is similar to Deletenodebyname method. The} else { //deletenodebyname method deletes a single resource item in the XML. Deletenodebyname (Path, xmltag, name);}} catch (exception e) {}}public typesource clone () {TypeSource ts = new typesource ();ts.type = type;ts.name = name;ts.xmltag = xmltag; Ts.path = path;return ts;}} @SuppressWarnings ("Unchecked") Public static void deletenodebyname (string path, string tag, string name) { //Delete a resource item in the XML file try {saxreader reader = new saxreader ();D Ocument document = reader.read ( New file (path)); Element element = document.getrootelement (); List<element> list = element.elements ("Dimen");for (int i = 0; i < list.size (); i++) {Element ele = list.get (i); String tname = ele.attributevalue ("name");if (tname != null && tname.length () > 0) {if (Name.equals (Ele.attributevalue ("name")) { Element.remove (ele); break;}}} Outputformat format = new outputformat ("", false);//xmlwriter xmlwriter = new xmlwriter (New filewriter (path), format); Xmlwriter.write (document); Xmlwriter.flush ();} catch (EXCEPTION&NBSP;E1) {e1.printstacktrace ();}}
Then export them as jars, using the following method
Java-jar Androidunusedresources.jar Unused.txtjava-jar Cleaner.jar unused.txt
Well, it's only been a few seconds now for a few hours.
Because a resource referenced by a useless resource is not considered a useless resource, it is necessary to execute the command more than once to actually erase it.
Note Because the results of the androidunusedresources export are not very accurate, there may be errors, which can cause the cleanup to be inaccurate. Some resources may be omitted from deletion. It's time to do it.
Finally, remember to clean up the resources before you submit the code, you understand
This article is from the "nashlegend" blog, make sure to keep this source http://nashlegend.blog.51cto.com/5635342/1657683
A tool class that automatically cleans up unwanted resources for Android projects