Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 一鍵刪除android下面無用資源

一鍵刪除android下面無用資源

編輯:關於Android編程

項目需求一改再改,UI一調再調,結果就是項目中一堆已經用不到但卻沒有清理的垃圾資源,不說工程大小問題,對新進入項目的人或看其他模塊的代碼的人來說,這些沒清理的資源可能也可能會帶來困擾,所以最好還是清理掉這些垃圾,對於一個稍微大一點的工程來說,手工清理明顯是不現實的,這就需要一個方法做這些事情。

 

關於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();
sbf.append( );
while ((line = reader.readLine()) != null) {
if (line.contains( sbf.append(line);
if (line.contains( sbf.append();
}
sbf.append( );
}
}
sbf.append(
);
// 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();
nodeTranserse(rootElement, sbf);
sbf.append();
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();
// System.out.print();
}
}
}
}

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);
}
}

 

 


大家湊活看吧。

 

 

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved