在则会列表有问题的资源文件
并未提供直接删除的命令
可导出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;
}
}
}