您的位置:首页 > 移动开发 > Android开发

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

2017-03-28 16:30 701 查看

在第一次接手别人的项目时,不清楚项目某个功能的函数调用顺序,一般的做法就是打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")
}

}
}
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: