自己封装一个可以随意删减 没用的资源文件的工具
思路:
-
通过lint命令 导出一个带有lint log的txt文件
-
通过读取里面的“[UnusedResources]”字段 筛选出所有没用的资源文件
-
可以直接删除 文件
- drawable-mdpi
- drawable-hdpi
- drawable-xhdpi
- drawable-xxhdpi
- drawable
- layout
- anim
- color
- raw
- xml
-
看情况 删除文件里面的某一行 values
- arrays
- colors
- dimens
- strings
- config_main_tab
- prompt_message
- styles
- syle_base_view
- teaching_strings
- toast
(1)首先得进入 sdk里面的 tools
` D:\develop\AndroidStudio\Android\sdk\tools\lint.bat `
复制代码
(2)在cmd里面输入
` lint H:\android_workspace\smilecampus_git\SmileCampus >C:\Users\Administrator\Desktop\master_lint_log.txt`
复制代码
(3)通过java代码(IO流) 读取导出来的lint log文件
通过筛选里面的“[UnusedResources]” 判断资源文件所处哪一个文件夹 (到底是删除文件 还是删除里面的某一行)
代码三个类: Constant类
public class Constant {
public static final String UnusedResources = "[UnusedResources]";
public static final String drawable_mdpi = "drawable-mdpi";
public static final String drawable_hdpi = "drawable-hdpi";
public static final String drawable_xhdpi = "drawable-xhdpi";
public static final String drawable_xxhdpi = "drawable-xxhdpi";
public static final String drawable = "drawable";
public static final String anim = "anim";
public static final String layout = "layout";
public static final String color = "color";
public static final String raw = "raw";//弹幕
public static final String xml = "xml";//不改动
public static final String values = "values";
/**
* values下面的配置文件
*/
public static final String arrays = "arrays";
public static final String colors = "colors";
public static final String dimens = "dimens";
public static final String strings = "strings";
public static final String config_main_tab = "config_main_tab";
public static final String prompt_message = "prompt_message";
public static final String styles = "styles";
public static final String syle_base_view = "syle_base_view";
public static final String teaching_strings = "teaching_strings";
public static final String toast = "toast";
}
复制代码
CompactAndroidCodeUtil类
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
public class CompactAndroidCodeUtil {
public static void cleanUnUseResourse(String projectPath, String lintResultPath, List<String> resources) {
BufferedReader br = null;
BufferedWriter bw = null;
try {
FileReader fr = new FileReader(lintResultPath);
br = new BufferedReader(fr);
String s = br.readLine();
//将原本导出来的lint文件 窗以及一个副本 并把"[UnusedResources]"筛选出来 写进副本文件中
String lintResultTranscriptPath = createLintResultTranscript(lintResultPath);
File file = new File(lintResultTranscriptPath);
if(!file.exists()) {
file.createNewFile();
}
bw = new BufferedWriter(new FileWriter(file));
while (null != s) {
// 判断一行包含
if (s.contains(Constant.UnusedResources)) {
// res\color\org_apply_text_color_selector.xml:2: Error: The
// resource R.string.lepi_un_sub_any_service appears to be
// unused
// 截取第一个:前面 是整个路径 判断该文件 是什么文件夹下面的资源文件
int end = s.indexOf(":");
if (end > 0) {
String subPath = s.substring(0, end);
// Test
int startDiagonal = subPath.indexOf("\\");
String temp = subPath.substring(startDiagonal + 1);
int secondDiagonal = temp.indexOf("\\");
String resource = temp.substring(0 , secondDiagonal);
if(resource.equals(Constant.values)) {//将values下面的文件夹筛选出来
String temp1 = temp.substring(secondDiagonal + 1);
int thirdDiagonal = temp1.indexOf(".");
resource = temp1.substring(0, thirdDiagonal);
// if (!resources.contains(resource)) {
// resources.add(resource);
// }
} else {
if (resources.contains(resource)) {//如果resourse是resourses里面的话 那就删除文件
//将符合标准的资源文件 写入副本文件中
bw.write(s);
bw.newLine();//换行
bw.flush();
String deleteFilePath = projectPath + "\\" + subPath;
if(deleteFile(deleteFilePath)){
System.out.println("文件删除成功");
}
// resources.add(resource);
}
}
}
}
s = br.readLine();
}
} catch (FileNotFoundException e) {
System.out.println("文件未找到!");
} catch (IOException e) {
System.out.println("读取失败!");
} finally {
try {
br.close();
bw.close();
} catch (IOException e) {
System.out.println("关闭流失败!");
}
for(String res : resources) {
System.out.println(res);
}
}
}
/**
* @param lintResultPath
* @return 创建一个文件副本 来
*/
public static String createLintResultTranscript(String lintResultPath) {
String lintResultTranscriptPath = "";
int index = lintResultPath.indexOf(".");
String before = lintResultPath.substring(0, index);
String after = lintResultPath.substring(index);
lintResultTranscriptPath = before + "-副本1" + after;
return lintResultTranscriptPath;
}
/**
* @param deletePath
* @return 删除文件
*/
public static boolean deleteFile(String deletePath) {
// 验证字符串是否为正确路径名的正则表达式
String matches = "[A-Za-z]:\\\\[^:?\"><*]*";
boolean flag = false;
// 通过 sPath.matches(matches) 方法的返回值判断是否正确
// sPath 为路径字符串
if(deletePath.matches(matches)) {
File file = new File(deletePath);
// 路径为文件且不为空则进行删除
if (file.isFile() && file.exists()) {
file.delete();
flag = true;
}
}
return flag;
}
}
复制代码
CleanResource类
import java.util.ArrayList;
import java.util.List;
/**
* @author Wisdozzh
* 清除项目中的无用资源文件
*
*/
public class CleanResource {
public static void main(String[] args) {
String projectPath = "H:\\android_workspace\\smilecampus_git\\SmileCampus";
String lintResultPath = "C:\\Users\\Administrator\\Desktop\\master_lint_log.txt";
//这里面只删除 某一些文件 以及文件里面的某一行
List<String> resourses = new ArrayList<>();
resourses.add(Constant.drawable_mdpi);//ok
resourses.add(Constant.drawable_hdpi);//ok
resourses.add(Constant.drawable_xhdpi);//ok
resourses.add(Constant.drawable_xxhdpi);//ok
resourses.add(Constant.drawable);
resourses.add(Constant.layout);
// resourses.add(Constant.color);
//这些事删除一行
resourses.add(Constant.arrays);
resourses.add(Constant.colors);
resourses.add(Constant.dimens);
resourses.add(Constant.strings);
CompactAndroidCodeUtil.cleanUnUseResourse(projectPath, lintResultPath, resourses);
}
}
复制代码
(4)研究已经出成果了
现在只删除drawable-mdpi、drawable-hdpi、drawable-xhdpi、drawable-xxdpi、drawable、layout、以及一点儿style
(5)可以优化 (以后在进行优化)
把anim、color、raw、xml、values下面的arrays、colors、dimens、strings、config_main_tab、prompt_message、styles、style_base_view、teaching_strings、toast 里面没用的资源文件 进行删除