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()
        }
    }
}
相关推荐
用户985120035832 小时前
Compose Navigation 3 深度解析(二):基础用法
android·android jetpack
恋猫de小郭2 小时前
Android 官方正式官宣 AI 支持 AppFunctions ,Android 官方 MCP 和系统级 OpenClaw 雏形
android·前端·flutter
黄林晴3 小时前
Android 17 Beta 2,隐私这把锁又拧紧了
android
Kapaseker3 小时前
研究表明,开发者对Kotlin集合的了解不到 20%
android·kotlin
bqliang3 小时前
Compose 媒体查询 (Media Query API) 🖱️👇🕹️
android·android jetpack
程序员陆业聪12 小时前
Android 平台 AI Agent 技术架构深度解析
android·人工智能
BD_Marathon17 小时前
工厂方法模式
android·java·工厂方法模式
王码码203517 小时前
Flutter for OpenHarmony:socket_io_client 实时通信的事实标准(Node.js 后端的最佳拍档) 深度解析与鸿蒙适配指南
android·flutter·ui·华为·node.js·harmonyos
勇气要爆发18 小时前
吴恩达《LangChain LLM 应用开发精读笔记》2-Models, Prompts and Parsers 模型、提示和解析器
android·笔记·langchain