android lint 删除无法的资源

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;
        }
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,180评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,200评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,996评论 6 342
  • 七月的第一天,不!昨天晚上,六月的最后一天。在我散步回来的路上捡了一只小小鸟。我特别焦虑的把它放在了一个陈旧的酒盒...
    那些鸟儿阅读 408评论 0 0
  • 你是天空,我是云朵 你是大海,我是浪花 你是大地,我是山峦 你是轻风,我是风筝 你是夜晚,我是星空 你是草原,我是...
    _赵小妮_阅读 208评论 0 0