Remove unwanted resources under Android with one click

Source: Internet
Author: User

The project needs to change again, the UI a tune again, the result is a bunch of items in the project has not been used but not clean up the garbage resources, not to mention the project size problem, for the new entry of the person or look at the code of other modules, these clean resources may also be troubled, so it is best to clean up the rubbish, For a slightly larger project, manual cleanup is obviously unrealistic, which requires a way to do these things.

I fear the code word, the above content introduced http://www.cnblogs.com/angeldevil/p/3725358.html

About the use of Android lint, if you do not know, please go to understand.

Here is my clear code, mainly using DOM node parsing to remove useless resources, need to introduce Dom.jar, recommend to this warehouse to download http://search.maven.org/, speed can also. The code is not optimized for temporary writing.

Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.FileReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.OutputStream;

Import Javax.xml.parsers.DocumentBuilder;
Import Javax.xml.parsers.DocumentBuilderFactory;
Import javax.xml.parsers.ParserConfigurationException;

Import org.w3c.dom.Document;
Import org.w3c.dom.Element;
Import Org.w3c.dom.NamedNodeMap;
Import Org.w3c.dom.Node;
Import org.w3c.dom.NodeList;
Import Org.w3c.dom.Text;
Import org.xml.sax.SAXException;

public class Cleanresources {

 public static void Clearresources (String projectpath, String resultpath)
   throws IOException {
  bufferedreader reader = new BufferedReader (new FileReader (Resultpath));
  string Line;
  int count = 0;
  while (line = Reader.readline ()) = null) {
   if (line.contains ("Unusedresources" ) &&!line.contains ("AppCompat")
     &&!line.contains ("Res\\values") ) {
    count++;
    int end = Line.indexof (":");
    if (end! =-1) {
     string file = line.substring (0, end);
     string f = projectpath + file;
     system.out.println (f);
     new File (f). Delete ();
    }
   }
  }
 }

public static void Dodoccheck (String projectpath, String resultpath)
Throws Interruptedexception, IOException {
String cmd = "cmd/c lint--check \" unusedresources\ "" + ProjectPath
+ ">" + resultpath;
Process process = Runtime.getruntime (). exec (CMD);
System.out.println (CMD);
String ls;
BufferedReader BufferedReader = new BufferedReader (
New InputStreamReader (Process.getinputstream ()));
while (ls = bufferedreader.readline ()) = null) {
SYSTEM.OUT.PRINTLN (LS);
}
Process.waitfor ();
}

public static void Checkresource (String resultpath, String checkpath)
Throws IOException {
BufferedReader reader = new BufferedReader (New InputStreamReader (
New FileInputStream (Resultpath), "GBK");
String Line;
StringBuffer SBF = new StringBuffer ();
Sbf.append ("<?xml version=\" 1.0\ "encoding=\" utf-8\ "? >\n");
Sbf.append ("<resources>\n");
while (line = Reader.readline ()) = null) {
if (Line.contains ("<color") | | | line.contains ("<string")) {
Sbf.append (line);
if (Line.contains ("<string-array")) {
Sbf.append ("</string-array>");
}
Sbf.append ("\ n");
}
}
Sbf.append ("</resources>");
System.out.println (Sbf.tostring ());
WriteFile (Checkpath, sbf.tostring ());
}

public static void WriteFile (string fileabsultpath, string content)
Throws IOException {
OutputStream OS = new FileOutputStream (Fileabsultpath);
Os.write (Content.getbytes ());
Os.close ();
}

public static void Prepairclear (String clearpath, String checkpath)
Throws Parserconfigurationexception, Saxexception, IOException {
File Fileparent = new file (ClearPath);
File Checkfile = new file (Checkpath);

if (Fileparent.isdirectory ()) {
For (File file:fileParent.listFiles ()) {
Clear (file, checkfile);
}
} else {
Clear (Fileparent, checkfile);
}
}

public static void Clear (file clearfile, file checkfile)
Throws Parserconfigurationexception, Saxexception, IOException {
Documentbuilderfactory builderfactory = documentbuilderfactory
. newinstance ();
Documentbuilder builder = Builderfactory.newdocumentbuilder ();
Document document = Builder.parse (Clearfile);
Element rootelement = Document.getdocumentelement ();
NodeList childNodes = Rootelement.getchildnodes ();

Documentbuilderfactory BuilderFactory2 = documentbuilderfactory
. newinstance ();
Documentbuilder builder2 = Builderfactory2.newdocumentbuilder ();
Document Document2 = Builder2.parse (checkfile);
Element RootElement2 = Document2.getdocumentelement ();
NodeList childNodes2 = Rootelement2.getchildnodes ();

for (int i = 0; i < childnodes.getlength (); i++) {
Node Childnode = Childnodes.item (i);
if (childnode.getnodetype () = = Node.element_node) {
for (int j = 0; J < Childnodes2.getlength (); j + +) {
Node ChildNode2 = Childnodes2.item (j);
if (childnode2.getnodetype () = = Node.element_node) {
if (Childnode
. GetAttributes ()
. getNamedItem ("name")
. Getnodevalue ()
. Equals (Childnode2.getattributes ()
. getNamedItem ("name"). Getnodevalue ())) {
Rootelement.removechild (Childnode);
}
}
}

}
}
StringBuffer SBF = new StringBuffer ();
Sbf.append ("<?xml version=\" 1.0\ "encoding=\" utf-8\ "? >\n");
Nodetranserse (RootElement, SBF);
Sbf.append ("</" + rootelement.getnodename () + ">");
System.out.println (Sbf.tostring ());
WriteFile (Clearfile.getabsolutepath (), sbf.tostring ());
}

public static void Nodetranserse (node node, StringBuffer SBF) {
String rnodename = Node.getnodename ();//Current traversal element name
if (node.getnodetype () = = Node.element_node) {//For node type, output node name
Sbf.append ("<" + Rnodename);
System.out.print ("<" + Rnodename);
if (Node.hasattributes ()) {
NamedNodeMap map = Node.getattributes ();
int len = Map.getlength ();
for (int i = 0; i < len; i++) {
Sbf.append ("");
Node attr = Map.item (i);
Sbf.append (Attr.getnodename () + "=\" "+ Attr.getnodevalue ()
+ "\"");
}
}
Sbf.append (">");
System.out.print (Sbf.tostring ());
}
if (node.getnodetype () = = Node.text_node) {//text type, output text
Sbf.append ((Text) node). Getwholetext ());
System.out.print ((Text) node). Getwholetext ());
}

  nodelist allnodes = Node.getchildnodes ();//Gets the child node to traverse the node
  int size = Allnodes.getlength ( );
  if (Size > 0) {
   for (int j = 0; J < size; J + +) {
    nod E Childnode = Allnodes.item (j);
    nodetranserse (Childnode, SBF);
    if (childnode.getnodetype () = = Node.element_node) {
     / /Each tab is finished, output end tag
     sbf.append ("</" + childnode.getnodename () + ">");
     //System.out.print ("</" + childnode.getnodename () + ">");
    }
   }
  }
 }

public static void Main (string[] args) throws IOException,
Parserconfigurationexception, Saxexception, interruptedexception {
Project root directory
String ProjectPath = "e:\\workspace\\bless\\";
Inspection problem Parts
String Resultpath = ProjectPath + "Result.xml";
Test file comparison with unused results
String Checkpath = ProjectPath + "Check.xml";
Specify delete useless node values directory
String ClearPath = ProjectPath + "res\\values\\";

Lint detects and saves the results in the project root directory Result.xml
Dodoccheck (ProjectPath, Resultpath);

Delete worry-free drawable layout anim Menu
Clearresources (ProjectPath, Resultpath);

Detects that the value directory entry is not used by Clor string string-array, etc., and is saved in the project root directory Check.xml
Checkresource (Resultpath, Checkpath);

Delete a node that is not useful for a values catalog item
Prepairclear (ClearPath, Checkpath);
}
}


Let's live and see.

Remove unwanted resources under Android with one click

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.