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()
        }
    }
}
相关推荐
墨狂之逸才13 小时前
React Native 移动项目目录导致的 Android 编译失败问题及解决方案
android·react native
feng一样的男子13 小时前
住在手机里的“小龙虾” (OpenClaw):接入本地模型,解决记忆“装死”顽疾
android·ai·智能手机·openclaw
hongtianzai13 小时前
MySQL中between and的基本用法
android·数据库·mysql
Zender Han14 小时前
Flutter Bloc / Cubit 最新详解与实战指南(2026版)
android·flutter·ios
sun00770015 小时前
pthread_once
android
阿拉斯攀登16 小时前
第 20 篇 RK 平台 NPU / 硬件编解码驱动适配与安卓调用
android·驱动开发·瑞芯微·rk安卓驱动
Volunteer Technology16 小时前
mysql面试场景题(二)
android·mysql·面试
代码s贝多芬的音符17 小时前
Android NV21 转 YUV 系列格式
android·开发语言·python
匆忙拥挤repeat17 小时前
Android Compose 《编程思想》解读
android
进击的cc18 小时前
Activity 生命周期是如何被调度的?(从源码到实战全链路拆解)
android