Encapsulate a tool to delete useless resource files at will

Keywords: Java xml SDK Android

Encapsulate a tool to delete useless resource files at will

Train of thought:

  • Export a txt file with lint log through lint command
  • Filter out all useless resource files by reading the "[UnusedResources]" field in it
  • Files can be deleted directly
    - drawable-mdpi
    - drawable-hdpi
    - drawable-xhdpi
    - drawable-xxhdpi
    - drawable
    - layout
    - anim
    - color
    - raw
    - xml

    • Depending on the situation, delete a line of values in the file
      • arrays
      • colors
      • dimens
      • strings
      • config_main_tab
      • prompt_message
      • styles
      • syle_base_view
      • teaching_strings
      • toast

(1) First, you need to enter the tools in the sdk

` D:\develop\AndroidStudio\Android\sdk\tools\lint.bat `

(2) Input in cmd

`  lint H:\android_workspace\smilecampus_git\SmileCampus >C:\Users\Administrator\Desktop\master_lint_log.txt`

(3) Read the exported lint log file through java code (IO stream)

By filtering "[UnusedResources]" in the resource file, determine which folder the resource file is in (delete the file or delete a line in the file)

Code three classes:
Class 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";//Barrage
    public static final String xml = "xml";//No change

    public static final String values = "values";
    /**
     * values The following configuration file
     */
    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 class

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();
            //Filter out the lint file window and a copy that were originally exported, and write "[UnusedResources]" into the copy file
            String lintResultTranscriptPath = createLintResultTranscript(lintResultPath);
            File file = new File(lintResultTranscriptPath);
            if(!file.exists()) {
                file.createNewFile();
            }
            bw = new BufferedWriter(new FileWriter(file));
            while (null != s) {
                // Judge whether a line contains
                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
                    // Intercept the first one: in front of it is the entire path to determine what folder the file is and the resource file under it
                    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)) {//Filter out the folders under 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)) {//If resource is in resources, delete the file
                                //Write compliant resource files to replica files
                                bw.write(s);
                                bw.newLine();//Line feed
                                bw.flush();

                                String deleteFilePath = projectPath + "\\" + subPath;
                                if(deleteFile(deleteFilePath)){
                                    System.out.println("File deleted successfully");
                                }
//                              resources.add(resource);
                            }   
                        }
                    }
                }
                s = br.readLine();
            }
        } catch (FileNotFoundException e) {
            System.out.println("File not found!");
        } catch (IOException e) {
            System.out.println("Read failed!");
        } finally {
            try {
                br.close();
                bw.close();
            } catch (IOException e) {
                System.out.println("Failed to close stream!");
            }
            for(String res : resources) {
                System.out.println(res);
            }
        }
    }

    /**
     * @param lintResultPath
     * @return Create a copy of the file to
     */
    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 + "-Copy 1" + after;
        return lintResultTranscriptPath;
    }

    /**
     * @param deletePath
     * @return Delete files
     */
    public static boolean deleteFile(String deletePath) {
        // A regular expression that verifies that the string is the correct pathname  
        String matches = "[A-Za-z]:\\\\[^:?\"><*]*";  
        boolean flag = false;  
        // Judge whether it is correct by the return value of sPath.matches(matches) method  
        // sPath is the path string  
        if(deletePath.matches(matches)) {
            File file = new File(deletePath);  
            // Delete if the path is file and not empty  
            if (file.isFile() && file.exists()) {  
                file.delete();  
                flag = true;  
            }  
        }
        return flag;  
    }
}

CleanResource class

import java.util.ArrayList;
import java.util.List;

/**
 * @author Wisdozzh
 * Clean up useless resource files in the project
 *
 */
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";
        //Only some files and lines in the files are deleted
        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);


        //Delete one line of these things
        resourses.add(Constant.arrays);
        resourses.add(Constant.colors);
        resourses.add(Constant.dimens);
        resourses.add(Constant.strings);
        CompactAndroidCodeUtil.cleanUnUseResourse(projectPath, lintResultPath, resourses);
    }
}

(4) The research has come to fruition

Now just delete drawable MDPI, drawable hdpi, drawable xhdpi, drawable, layout, and a little style

(5) Can be optimized (to be optimized later)

Under anim, color, raw, xml and values, select arrays, colors, dimens ions, strings, config > main tab, prompt > message, styles, style > base > view, teaching > strings, toast
Delete the useless resource file

Posted by RedDragon on Fri, 31 Jan 2020 09:35:06 -0800