查看android项目中方法执行顺序

在第一次接手别人的项目时,不清楚项目某个功能的函数调用顺序,一般的做法就是打log日志或者加断点,这里有个其他的办法。

在Module的gradle文件中添加如下两个任务,怎么操作这里就不详述啦,不知道的可以在评论区留言


//核心任务:在captures文件目录下输出 基于最新.trace文件的函数调用信息的txt版本
//说明:dmtracedump 为 android sdk自带工具,要执行dmtracedump命令则需要先添加环境变量(mac)
task AppOutPutMethodOrder() {
    doLast {
        def capturesDirPath = project.getProjectDir().getParentFile().path + File.separator + "captures";
        def capturesDir = new File(capturesDirPath);
        capturesDir.traverse {
            if (it.isFile() && it.name.endsWith(".trace")) {
                def orderName = it.name.replace("trace", "txt")
                def orderFile = new File(capturesDirPath, orderName)

                //说明:dmtracedump 为 android sdk自带工具,要执行dmtracedump命令则需要先添加环境变量
                def baseComand = "dmtracedump  -ho " + it.absolutePath + " >> " + orderFile.absolutePath
                println baseComand
                String osNameMatch = System.getProperty("os.name").toLowerCase();
                if (osNameMatch.contains("windows")) {
                    ("cmd /c start  /b " + baseComand).execute()
                } else {
                    ["bash", "-c", baseComand].execute()
                }
            }
        }
    }
}

//这里AppFilterMethodOrder 任务其实也不需要 执行找到 \captures 目录找到 base_order.txt
//用Notepad++ 使用正则 先过滤 带 xit 的行 (我们只关注ent 行就行,ent代表进入执行函数 xit代表退出函数)再过滤掉你不关心的包名
// Notepad++ 中过滤将会使用到的命令行如下
//^.*xit.*$ //去除掉 含有 xit 字符串的行  然后替换为空
// ^((?!XXX).)*$  //去除不包含XXX的行  然后替换为空
//^\s+   //合并空行  然后替换为空

task AppFilterMethodOrder() {
    doLast {
        //TODO 替换为你想要过滤出来的包名
        def filterPackageName = "com.dingmouren.dingdingmap"
        //处理包名
        def filterSignature = filterPackageName.replaceAll("[.]", "/")

        def capturesDirPath = project.getProjectDir().getParentFile().path + File.separator + "captures";
        def capturesDir = new File(capturesDirPath);

        capturesDir.traverse {
            if (it.isFile() && it.name.endsWith(".txt") && !it.name.contains("--filter")) {
                def orderName = it.name.replace(".txt", "--filter.txt")
                def orderFile = new File(capturesDirPath, orderName)

                it.eachLine { line ->

                    if (line.contains(" ent ")
                            //兼容不同版本traceview 有的是方法包名有的是方法签名
                            && (line.contains(filterPackageName)
                            || line.contains(filterSignature))
                    ) {
                        println line
                        orderFile.append(line + "\n")
                    }

                }
            }
        }


    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值