项目需求一改再改,UI一调再调,结果就是项目中一堆已经用不到但却没有清理的垃圾资源,不说工程大小问题,对新进入项目的人或看其他模块的代码的人来说,这些没清理的资源可能也可能会带来困扰,所以最好还是清理掉这些垃圾,对于一个稍微大一点的工程来说,手工清理明显是不现实的,这就需要一个方法做这些事情。
本人最怕码字,上面内容引入http://www.cnblogs.com/angeldevil/p/3725358.html
关于android lint的使用,如果不了解的请自行去了解。
下面是我的清除代码,主要就是使用dom 节点解析来删除无用资源,需要引入dom.jar ,建议到这个仓库去下载http://search.maven.org/,速度还可以。代码没有做优化,临时写的。
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();// 当前遍历元素名称
if (node.getNodeType() == Node.ELEMENT_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) { // 文本类型,输出文本
sbf.append(((Text) node).getWholeText());
// System.out.print(((Text) node).getWholeText());
}
NodeList allNodes = node.getChildNodes();// 获取所要遍历节点的子节点
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) {
// 每遍历完一个标签,输出结束标签
sbf.append("</" + childNode.getNodeName() + ">");
// System.out.print("</" + childNode.getNodeName() + ">");
}
}
}
}
public static void main(String[] args) throws IOException,
ParserConfigurationException, SAXException, InterruptedException {
// 项目根目录
String projectPath = "E:\\workspace\\Bless\\";
// 检测问题件
String resultPath = projectPath + "result.xml";
// 检测文件比对未使用结果
String checkPath = projectPath + "check.xml";
// 指定删除无用节点values目录
String clearPath = projectPath + "res\\values\\";
// lint 检测 并将结果保存在项目根目录result.xml
doDocCheck(projectPath, resultPath);
// 删除无忧drawable layout anim menu
clearResources(projectPath, resultPath);
// 检测value 目录项未使用的clor string string-array等,并保存在项目根目录check.xml
checkResource(resultPath, checkPath);
// 删除values 目录项无用的节点
prepairClear(clearPath, checkPath);
}
}
大家凑活看吧。
原文地址:http://blog.csdn.net/jtf18/article/details/46445053