One-click deletion of useless resources under android, one-click android
The project needs to be changed and the UI needs to be adjusted. The result is that a bunch of junk Resources in the project that are not used but are not cleaned up, not to mention the project size, for new project users or those who read code from other modules, these uncleared resources may also cause problems. Therefore, it is best to clear the garbage, for a slightly larger project, manual cleanup is obviously unrealistic. This requires a method to do these tasks.
I am most afraid of code words, the above content introduced http://www.cnblogs.com/angeldevil/p/3725358.html
For more information about how to use android lint, see.
The following is my cleanup code, which is mainly used for dom node parsing to delete useless resources. We need to introduce dom. jar. We recommend that you download http://search.maven.org/to this repository, and the speed can still be reached. The code is not optimized and is temporarily written.
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(cmd.exe c (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 (); // name of the current traversal Element
If (node. getNodeType () = Node. ELEMENT_NODE) {// indicates the node type and the name of the output Node.
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 (); // obtain the subnode of the node to be traversed
Int size = allNodes. getLength ();
If (size> 0 ){
For (int j = 0; j <size; j ++ ){
Node childNode = allNodes. item (j );
NodeTranserse (childNode, sbf );
If (childNode. getNodeType () = Node. ELEMENT_NODE ){
// Output the end tag after each traversal.
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 \\";
// Detect problematic parts
String resultPath = projectPath + "result. xml ";
// Check the file comparison result.
String checkPath = projectPath + "check. xml ";
// Delete the values directory of useless nodes
String clearPath = projectPath + "res \ values \\";
// Lint detection and save the result to the project root directory result. xml
DoDocCheck (projectPath, resultPath );
// Delete the carefree drawable layout anim menu
ClearResources (projectPath, resultPath );
// Check the unused clor string-array in the value directory and save it to the project root directory check. xml.
CheckResource (resultPath, checkPath );
// Delete useless nodes in the values directory
PrepairClear (clearPath, checkPath );
}
}
Let's take a look.