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()
        }
    }
}
相关推荐
阿pin11 小时前
Android随笔-kotlin withContext
android·kotlin·withcontext
树码小子11 小时前
开启 IDEA 中的断言功能
android·java·intellij-idea
Kslient11 小时前
HeaderBehavior
android
Android-Flutter13 小时前
android 动画详解
android·kotlin
小孔龙18 小时前
GraphicBuffer 跨进程共享
android
行业研究员18 小时前
Android内置GPS与腾讯地图定位技术对比分析
android·android定位·腾讯地图定位
Android-Flutter19 小时前
android WMS 详解(二)
android·kotlin
游戏开发爱好者820 小时前
App Store 上传 IPA 自动化,.p8 密钥认证与 CI/CD 接入实战
android·运维·ci/cd·小程序·uni-app·自动化·iphone
游戏开发爱好者821 小时前
iOS 推送怎么配置,APNs 推送证书、设备库与群发
android·ios·小程序·https·uni-app·iphone·webview
阿pin21 小时前
Android随笔-kotlin 高阶函数
android·kotlin·高阶函数