Android(安卓)studio 删除无用代码
16lz
2021-12-04
max 下需要添加环境变量
环境变量添加 参考这里
1) 工具
1.1) Android lint 在 Android sdk tools 当中 如果可以希望大家能配好环境变量
本文主讲 以命令行形式的删除无效资源的批处理
2) 输入
打开命令行 使用lint命令
如图:
lint --check "UnusedResources" /Users/baozi/Dev/android/android > result.txt
/Users/baozi/Dev/android/android为工程路径
/User/baozi/result.txt 为输出txt路径
在命令行中输入: /Users/baozi/Dev/android/android > /User/baozi/result.txt
生成的扫描结果将会存放在当前目录下的 result.txt 当中
如我的目录 /Users/baozi/result.txt
3) 输出文件result.txt
打开文件目录 /Users/baozi/result.txt 重点内容
4) 根据结果 批量删除对应的文件
本文重点 当你第一次运行时 发现需要数千资源文件需要删除的时候就会伤脑筋
手工逐条删除 并不符合程序猿三大优秀品质 : 懒惰 没有耐心 骄傲
尝试过使用 vim 删除 发现操作起来也相当麻烦
大家可以参考下面的代码 使用FIle 获取 result.txt 中的文件信息 调用 File .delete(); 方法删除
/** * 删除 未使用的冗余资源(图片 xml布局) * * @param b * * false 显示资源列表 * * true 显示资源列表 并删除资源 * * @throws Exception */ private static void init(boolean b) throws Exception { String encoding = "UTF-8"; // 字符格式 String projectPath = "/Users/baozi/Dev/shihui/android/";//Android工程所在地址 String filePath1 = "/Users/baozi";//result的所在路径 File file = new File(filePath1, "result.txt");//获取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")) { // System.out.println(line); 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) { new File(f).delete(); System.out.println("删除成功"); } } } } read.close(); } }
更多相关文章
- NPM 和webpack 的基础使用
- 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
- Android特殊字体引入,以及描边和投影
- android kernel+busybox
- android ZygoteInit.java文件解析 从main(String args[])开始
- Android(安卓)-Recovery
- Android(安卓)ApiDemos 系列解析【View-ImageView/ImageButton】
- 在 Eclipse 中导入 Android(安卓)示例程序
- android nfc中MifareClassic格式的读写