Android無用代碼、資源掃描的其他思路

語言: CN / TW / HK

零、背景

之前一直是用Android自帶的 Analyze -> Run inspection by name... 但缺點也很多。 後來在stackoverflow看到一種基於minifyEnabled,和shrinkResources的思路

一、基於minifyEnabled 結果獲取無用代碼

1、我們都知道minifyEnabled=true會開啟代碼縮減,那如果知道minifyEnabled都刪了哪些代碼,就知道哪些代碼是沒用的了

在最新官方文檔上關於minifyEnabled的描述中 minifyEnabled 屬性設為 true,系統會默認啟用 R8 代碼縮減功能,在排查R8問題的文檔中我們發現官方提供了一個R8 移除的(或保留的)代碼的報告的功能 http://developer.android.com/studio/build/shrink-code#usage
這個生成的報告usage.txt 大概長這樣子:

image.png 這個文件列出了minifyEnabled開啟後,縮減掉的代碼內容:最大粒度為類,到類的成員變量、方法;
移除的內容包含三方Jar包的無用代碼,以及工程中自己的無用代碼

2、如何生成usage.txt

官方文檔中提到了minifyEnabled開啟後,默認用是R8做代碼縮減,但在R8之前呢? 又是由誰來做縮減的工作的呢,其實是proguard! 關於兩者的區別可以參考這篇文章

下面説這兩種情況下分別怎麼生成:

1、不想開啟R8,生成usage.txt
設置minifyEnabled=true進行編譯即可,
生成的文件位於build/outputs/mapping/release(或debug)/usage.txt
2、開啟R8,生成usage.txt
1、設置minifyEnabled=true
2. 指定生成路徑,在proguard-rules.pro文件中添加:
   -printusage <output-dir>/usage.txt
3. 編譯即可

筆者對比過這兩種方式的代碼縮減效果,相比之下開啟R8後被刪掉的代碼要比proguard的稍微多一些,但整體相差不大。如下圖:左邊是proguard,4萬1千行,右邊是R8,4萬4千行

image.png

3、基於usage文件內容,我們根據包名進行過濾,可以拿到當前工程中被縮減那部分的代碼,文章第三部分實踐,可以參考

二、基於shrinkResources結果獲取無用資源

獲取無用資源相對容易些,將shrinkResources置為true,編譯後shrinkResources的結果位於build/outputs/mapping/release(或debug)/resources.txt。內容大概長這樣:

image.png

除此之外,官方還提供了一個開啟嚴苛引用檢查的開關。開啟了之後,掃描出的無用資源數量大大增加,但需要注意是否會影響業務

開啟嚴苛檢查方法:在res/raw/目錄下新增keep.xml文件

三、實踐

編譯後基於usage.txt 和 resources.txt 的結果,可以通過task來過濾,排序處理。可參考以下:

``` task codeScan(dependsOn: assembleRelease) { ... doLast { if (project.getBuildDir().exists()) { String basePath = project.getBuildDir().path + "/outputs/mapping/release/" //無用Class File uoUseClassRecode = new File(basePath + "usage.txt") if (uoUseClassRecode.exists()) { FileReader fr = new FileReader(uoUseClassRecode) BufferedReader reader = new BufferedReader(fr) List classList = new ArrayList<>() ClassRecorder recorder = null String packageName = "${project.android.defaultConfig.applicationId}" if (packageName == null || packageName.size() == 0) { throw new IllegalArgumentException( "packageName為空,請檢查是否在build.gradle的defaultConfig中配置applicationId屬性") } while(reader.ready()){ String line = reader.readLine() //新的類 if (!line.startsWith(" ")) { if (isBusinessCode(recorder, packageName)){ //如果是業務代碼,記錄下來 classList.add(recorder) } recorder = new ClassRecorder() recorder.className = line } else { recorder.classMethodList.add(line) } } reader.close() fr.close() //讀取結束,排序整理 List result = sortByClassName(classList, packageName.size()+1) //排序完,輸出到文件 File outPutFile = new File(basePath + "unusedClass.txt") if (outPutFile.exists()) outPutFile.createNewFile() BufferedWriter bw = new BufferedWriter(new FileWriter(outPutFile)) for (ClassRecorder cr : result) { bw.writeLine(cr.className) } bw.close() } else { throw new IllegalArgumentException("編譯產物文件不存在") }

        boolean checkResPrefix = true
        //無用資源
        File uoUsedRes = new File(basePath + "resources.txt")
        if (uoUseClassRecode.exists()) {
            FileReader fr = new FileReader(uoUsedRes)
            BufferedReader reader = new BufferedReader(fr)
            List<String> resList = new ArrayList<>()
            while(reader.ready()){
                String line = reader.readLine()
                if (line.startsWith("Skipped unused resource")) {
                    String name = line.split(" ")[3]
                    name = name.substring(0, name.size()-1)
                    resList.add(name)
                }
            }
            reader.close()
            fr.close()
            File outPutFile = new File(basePath + "unusedRes.txt")
            if (outPutFile.exists()) outPutFile.createNewFile()
            BufferedWriter bw = new BufferedWriter(new FileWriter(outPutFile))
            for (String name : resList) {
                bw.writeLine(name)
            }
            bw.close()
        }
    }
}

/**
 * 是否是業務代碼,是否是含有包名
 */
static boolean isBusinessCode(ClassRecorder recorder, String packageName) {
    if (recorder == null) return false
    return recorder.className.contains(packageName)
}
/**
* 排序,按類名 —— 高位優先字符串排序
*/
static List<ClassRecorder> sortByClassName(List<ClassRecorder> list, int defaultStartLength){
    List<ClassRecorder> result = new ArrayList<>(list.size())
    result.addAll(list)
    sortByClassName(result, 0, result.size()-1, defaultStartLength)
    return result
}

static sortByClassName(List<ClassRecorder> list, int begin, int end, int d){
    if(begin >= end){return }
    int[] count = new int[258]
    for (int i = 0; i < 256+2; i++) {
        count[i] = 0;
    }
    for(int i = begin; i <= end; i++){ //attention 這個起始的位置是begin,end,每次只處理這一部分
        int index = charAt(list.get(i).className, d) + 2;
        count[index]+=1;
    }
    for(int i = 0; i < count.length-1; i++){
        count[i+1] += count[i];
    }
    List<ClassRecorder> result = new ArrayList<>(list.size());
    for(int i = begin; i <= end; i++){
        int index = charAt(list[i].className ,d) + 1
        result[count[index]++] = list.get(i);
    }
    for(int i = begin; i <= end; i++){
        list[i] = result[i - begin];
    }
    //當前按d位的排序已完成
    for(int r = 0; r < count.length-2; r++){
        sortByClassName(list, begin + count[r], begin + count[r+1]-1, d+1);
    }
}

static int charAt(string, d) {
    if (d < string.size()){
        return Character.codePointAt(string, d)
    } else {
        return -1;
    }
}

class ClassRecorder {
    String className
    List<String> classMethodList = new ArrayList<>()
}

```

本文作者:自如大前端研發中心-李墨磊