Paste_Image.png
Paste_Image.png
Paste_Image.png
Paste_Image.png
在則會列表有問題的資源文件
并未提供直接刪除的命令
可導出xml文件寫段代碼刪除
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>file://$PROJECT_DIR$/fht_car63/src/main/res/drawable/startpage_edittext_bg.xml</file>
<line>1</line>
<module>fht_car63</module>
<entry_point TYPE="file" FQNAME="file://$PROJECT_DIR$/fht_car63/src/main/res/drawable/startpage_edittext_bg.xml" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unused resources</problem_class>
<hints />
<description><html>The resource <code>R.drawable.startpage_edittext_bg</code> appears to be unused</html></description>
</problem>
<problem>
<file>file://$PROJECT_DIR$/fht_car63/src/main/res/drawable-hdpi/map_stretch_bg.png</file>
<line>0</line>
<module>fht_car63</module>
<entry_point TYPE="file" FQNAME="file://$PROJECT_DIR$/fht_car63/src/main/res/drawable-hdpi/map_stretch_bg.png" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unused resources</problem_class>
<hints />
<description><html>The resource <code>R.drawable.map_stretch_bg</code> appears to be unused</html></description>
</problem>
<problem>
<file>file://$PROJECT_DIR$/fht_car63/src/main/res/values/colors.xml</file>
<line>40</line>
<module>fht_car63</module>
<entry_point TYPE="file" FQNAME="file://$PROJECT_DIR$/fht_car63/src/main/res/values/colors.xml" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unused resources</problem_class>
<hints />
<description><html>The resource <code>R.color.black75</code> appears to be unused</html></description>
</problem>
</problems>
line 0則為png等圖片文件 可直接刪除文件
line 1定義的xml 定義資源文件
line >1 的則為定義的resources 中的一條 對此應冊除該行
package cn.chinagps.fht;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Created by Administrator on 2016/1/20.
*/
public class DelLintTest {
@Test
public void test() throws Exception {
init("E:\\ProjectGit\\fhtCar6.3\\", "E:\\fht\\AndroidLintUnusedResources.xml");
// getFileName("<file>file://$PROJECT_DIR$/fht_car63/src/main/res/drawable/startpage_edittext_bg.xml</file>");
// getLineNum("<line>1</line>");
// delFileLineMap(new File("E:\\log5.txt"), 1);
}
/**
* 刪除 未使用的冗余資源(圖片 xml布局)<br>
*
* @param projectPath 項目路徑
* @param filePath1 xml文件路徑
*/
private void init(String projectPath, String filePath1) throws Exception {
String encoding = "UTF-8"; // 字符格式
File file = new File(filePath1);//獲取result.txt 文件 生成地址
if (file.isFile() && file.exists()) { // 判斷文件是否存在
InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考慮到編碼格式
BufferedReader bufferedReader = new BufferedReader(read);
String line;
boolean startProblem = false;
int lineNum;
String fileName = null;
while ((line = bufferedReader.readLine()) != null) {
if (line.contains("<problem>")) {
startProblem = true;
} else if (line.contains("</problem>")) {
startProblem = false;
} else if (line.contains("<file>")) {
fileName = getFileName(line);
} else if (line.contains("<line>")) {
lineNum = getLineNum(line);
if (startProblem && fileName != null) {
File f = new File(projectPath + fileName);
if (lineNum == 1 || lineNum == 0) {
delFile(f);
} else if (lineNum > 1) {
addDelFileLineNum(f, lineNum);
}
}
}
}
read.close();
delFileLine();
}
}
Map<String, List<Integer>> delFileLineMap = new HashMap<>();
/**
* 從文件中刪除一行<br>
* 讀取文件時 該行不讀取,再將文件寫入
*/
private void delFileLine() throws Exception {
Set<String> fileName = delFileLineMap.keySet();
for (String name : fileName) {
List<Integer> lines = delFileLineMap.get(name);
System.out.println(name + "->" + Arrays.toString(lines.toArray()));
//讀文件
FileInputStream fis = new FileInputStream(new File(name));
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
StringBuilder sb = new StringBuilder();
String line;
int readLineNum = 1;
while ((line = br.readLine()) != null) {
if (!inList(lines, readLineNum)) {
sb.append(line + "\n");
}
readLineNum++;
}
br.close();
fis.close();
//開始寫入
FileOutputStream fos = new FileOutputStream(new File(name));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
bw.write(sb.toString());
bw.close();
fos.close();
System.out.println("刪除部分行成功");
// return;
}
}
/**
* 該集合中是包含該值
*/
private boolean inList(List<Integer> lines, int readLineNum) {
for (Integer i : lines) {
if (i == readLineNum) {
return true;
}
}
return false;
}
/**
* 將文件要刪除的行號集合放入map中
*/
private void addDelFileLineNum(File f, int lineNum) {
if (delFileLineMap.get(f.getAbsolutePath()) == null) {
delFileLineMap.put(f.getAbsolutePath(), new ArrayList<Integer>());
}
delFileLineMap.get(f.getAbsolutePath()).add(lineNum);
}
private void delFile(File f) {
f.delete();
System.out.println("文件刪除成功");
}
/**
* <file>file://$PROJECT_DIR$/fht_car63/src/main/res/drawable/startpage_edittext_bg.xml</file>
*/
private String getFileName(String line) {
int start = line.indexOf("$/");
int end = line.lastIndexOf("</file>");
String str = line.substring(start + 2, end);
return str;
}
/**
* 取出其中的數字 <line>1</line>
*/
private int getLineNum(String line) {
int start = line.indexOf(">");
int end = line.lastIndexOf("</");
String str = line.substring(start + 1, end);
try {
return Integer.parseInt(str);
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
}