Android无用代码、资源扫描的其他思路(1),这操作真香

本文介绍了一个脚本,它扫描项目构建目录,识别未使用的类文件和无用资源,并按照业务逻辑进行排序和整理,以帮助Android开发者优化代码。作者提供了一份全面的学习资料,旨在帮助开发者系统学习和提升技能,减轻学习负担。
摘要由CSDN通过智能技术生成

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 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 sortByClassName(List list, int defaultStartLength){
    List result = new ArrayList<>(list.size())
    result.addAll(list)
    sortByClassName(result, 0, result.size()-1, defaultStartLength)
    return result
    }

static sortByClassName(List 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 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 classMethodList = new ArrayList<>()
}

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

写在最后

在技术领域内,没有任何一门课程可以让你学完后一劳永逸,再好的课程也只能是“师傅领进门,修行靠个人”。“学无止境”这句话,在任何技术领域,都不只是良好的习惯,更是程序员和工程师们不被时代淘汰、获得更好机会和发展的必要前提。

如果你觉得自己学习效率低,缺乏正确的指导,可以一起学习交流!

加入我们吧!群内有许多来自一线的技术大牛,也有在小厂或外包公司奋斗的码农,我们致力打造一个平等,高质量的Android交流圈子,不一定能短期就让每个人的技术突飞猛进,但从长远来说,眼光,格局,长远发展的方向才是最重要的。

35岁中年危机大多是因为被短期的利益牵着走,过早压榨掉了价值,如果能一开始就树立一个正确的长远的职业规划。35岁后的你只会比周围的人更值钱。

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

,如果能一开始就树立一个正确的长远的职业规划。35岁后的你只会比周围的人更值钱。

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 14
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值