android 打印函数调用堆栈

如果是系统应用,则可以直接调用如下方法:

rust 复制代码
Debug.getCallers(10)

@UnsupportedAppUsage 注释过的方法,只能系统应用才能调用

ini 复制代码
@UnsupportedAppUsage
    public static String getCallers(final int depth) {
        final StackTraceElement[] callStack = Thread.currentThread().getStackTrace();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < depth; i++) {
            sb.append(getCaller(callStack, i)).append(" ");
        }
        return sb.toString();
    }

非系统应用,则只好用反射方式进行调用了,如下

kotlin 复制代码
private fun getCallers(depth:Int): String {
        try{
            val calssName = "android.os.Debug"
            val clazz = Class.forName(calssName)
            val method = clazz.getMethod("getCallers",Int::class.java)
            method.isAccessible = true
            val result = method.invoke(null,depth) as String
            return result
        }catch (e: ClassNotFoundException){
            e.printStackTrace()
            return null.toString()
        }catch (e:NoSuchMethodException){
            e.printStackTrace()
            return null.toString()
        }
    }
}
相关推荐
MyBili1 分钟前
【安卓开发/搞机】快图浏览(QuickPic)技术解析:为何这款3MB应用仍是本地图库的性能天花板?
android·app·安卓·文件管理·相册·看图软件·手机相册
事圆则缓29 分钟前
Kotlin 高阶工程化与 Android 深入实践
android·开发语言·kotlin
zhangphil33 分钟前
AI大模型生成maxTokens 与上下文context
android·llama
淡淡的香烟37 分钟前
Androidiot开发之猫脸识别
android·物联网
2501_915106321 小时前
iOS数据采集技术详解:从性能监控到崩溃分析的全链路实践
android·ios·小程序·https·uni-app·iphone·webview
天空之城--10 小时前
Android Koin 完全指南:从原理到实践
android
zhangphil11 小时前
Android main thread主线程Choreographer doFrame发生FullSuspendCheck
android
TimeFine14 小时前
智能眼镜开发:获取真实的音频路由
android
pengyu15 小时前
【Kotlin 协程修仙录 · 渡劫境 · 中阶】 | 造化神兵:自定义 CoroutineDispatcher 与调度器的终极定制
android·kotlin
pengyu15 小时前
【Kotlin 协程修仙录 · 渡劫境 · 初阶】 | 飞升雷劫:CPS 变换与挂起函数字节码终极透视
android·kotlin