Android无用代码、资源扫描的其他思路

零、背景

之前一直是用Android自带的 Analyze -> Run inspection by name… 但不知道从什么时候开始就不好使了(也可能是我不会用。。请明白的大佬多多赐教)。 后来在网络寻找答案时候,看到一种基于minifyEnabled,和shrinkResources作文章的思路

一、基于minifyEnabled 结果获取无用代码

1、我们都知道minifyEnabled=true会开启代码缩减,那如果知道minifyEnabled都删了哪些代码,就知道哪些代码是没用的了

在最新官方文档上关于minifyEnabled的描述中 minifyEnabled 属性设为 true,系统会默认启用 R8 代码缩减功能,而在R8使用的排查R8问题的文档中我们发现官方还很贴心的提供了一个生成 R8 移除的(或保留的)代码的报告的功能 developer.android.com/studio/buil…
这个生成的报告usage.txt 大概长这样子:

image.png
最大粒度为类,到类的成员变量、方法; 移除的内容包含三方Jar包的无用代码,以及工程中自己的无用代码

1、如何生成usage.txt文件
只要设置minifyEnabled=true再进行编译即可

2. 默认生成的路径
build/outputs/mapping/release(或debug)/usage.txt

3. 指定生成路径
-printusage <output-dir>/usage.txt
复制代码

基于usage文件内容,我们根据包名进行过滤,可以拿到当前工程中被缩减那部分的代码,在文章第三部分实践,有对应的task可以参考

三、实践

1、usage过滤
task codeScan(dependsOn: assembleRelease)  {
    ...
    doLast {
        if (project.getBuildDir().exists()) {
            String basePath = project.getBuildDir().path + "/outputs/mapping/release/"
            //无用Class
            File unUseClassRecode = new File(basePath + "usage.txt")
            if (unUseClassRecode.exists()) {
                FileReader fr = new FileReader(unUseClassRecode)
                BufferedReader reader = new BufferedReader(fr)
                List<ClassRecorder> classList = new ArrayList<>()
                ClassRecorder recorder = null
                String packageName = "${project.android.defaultConfig.applicationId}"
                
                //此处使用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<ClassRecorder> 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("编译产物文件不存在")
            }            
        }
    }
}

/**
 * 是否是业务代码,是否是含有包名
 */
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<>()
}
复制代码
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享