原因:
项目时间长了,总会有些不再使用的图片,xml文件等资源,一个一个删除,太费时间,也不是程序员的风格。懒人都是批量删除的
O(∩_∩)O哈哈~
思路:
- 执行lint命令,结果保存到result.txt文件
- 执行java代码,删除不用的文件
详细步骤:
- 第一步:
需要知道项目路径,即:Project_path.例如:/Users/lincoln
需要知道结果保存在当前路径下的文件名,即:result.txt
lint --check "UnusedResources" Project_path > result.txt
package test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
/**
* 删除多余资源
* @author lincoln
*
*/
public class DeleteUnUsedFile {
public static void main(String[] args) {
init(true);
}
public static void init(boolean b){
System.out.println("开始删除多余文件");
try {
String encoding = "UTF-8";
String projectPath = "/Users/lincoln/Android/Admaster/surprise-app-android/";
String filePath1 = "/Users/lincoln/Android/Admaster";
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 = null;
while ((line = bufferedReader.readLine()) != null) {
if (line.contains("UnusedResources") && !line.contains("res/value") && !line.contains("appcompat")
&& !line.contains("res/xml")) {
int end = line.indexOf(":");
if (end != -1) {
String file_end = line.substring(0, end);
String f = projectPath + file_end;
System.out.println(f);
if (b && !f.contains("emoji") && !f.contains("mc_")) {
new File(f).delete();
System.out.println("删除成功");
}
}
}
}
read.close();
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("多余文件删除完毕");
}
}