概要
在日常开发和debug的过程中,我们都需要看看某一个方法的调用StackTrace,如果是crash或者异常被抛出的情况下会直接看到对应的StackTrace,如:
39 5939 E AndroidRuntime: FATAL EXCEPTION: main
10-03 18:53:33.977 5939 5939 E AndroidRuntime: Process: com.oneplus.filemanager, PID: 5939
10-03 18:53:33.977 5939 5939 E AndroidRuntime: java.lang.SecurityException: UID 1000 does not have permission to content://com.android.externalstorage.documents/document/primary%3ADCIM%2FCamera%2FIMG_20181003_185324.jpg [user 0]; you could obtain access using ACTION_OPEN_DOCUMENT or related APIs
10-03 18:53:33.977 5939 5939 E AndroidRuntime: at android.os.Parcel.createException(Parcel.java:1946)
10-03 18:53:33.977 5939 5939 E AndroidRuntime: at android.os.Parcel.readException(Parcel.java:1914)
10-03 18:53:33.977 5939 5939 E AndroidRuntime: at android.os.Parcel.readException(Parcel.java:1864)
10-03 18:53:33.977 5939 5939 E AndroidRuntime: at android.app.IActivityManager$Stub$Proxy.finishActivity(IActivityManager.java:3746)
10-03 18:53:33.977 5939 5939 E AndroidRuntime: at android.app.Activity.finish(Activity.java:5620)
10-03 18:53:33.977 5939 5939 E AndroidRuntime: at android.app.Activity.finish(Activity.java:5644)
10-03 18:53:33.977 5939 5939 E AndroidRuntime: at com.oneplus.filemanager.i.u.a(Unknown Source:68)
10-03 18:53:33.977 5939 5939 E AndroidRuntime: at com.oneplus.filemanager.picturedetail.b$a.onItemClick(Unknown Source:38)
10-03 18:53:33.977 5939 5939 E AndroidRuntime: at android.widget.AdapterView.performItemClick(AdapterView.java:318)
10-03 18:53:33.977 5939 5939 E AndroidRuntime: at android.widget.AbsListView.performItemClick(AbsListView.java:1201)
10-03 18:53:33.977 5939 5939 E AndroidRuntime: at android.widget.AbsListView$PerformClick.run(AbsListView.java:3178)
10-03 18:53:33.977 5939 5939 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:873)
10-03 18:53:33.977 5939 5939 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
10-03 18:53:33.977 5939 5939 E AndroidRuntime: at android.os.Looper.loop(Looper.java:193)
10-03 18:53:33.977 5939 5939 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6863)
10-03 18:53:33.977 5939 5939 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-03 18:53:33.977 5939 5939 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
10-03 18:53:33.977 5939 5939 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
10-03 18:53:33.977 5939 5939 E AndroidRuntime: Caused by: android.os.RemoteException: Remote stack trace:
10-03 18:53:33.977 5939 5939 E AndroidRuntime: at com.android.server.am.ActivityManagerService.checkGrantUriPermissionLocked(ActivityManagerService.java:11239)
10-03 18:53:33.977 5939 5939 E AndroidRuntime: at com.android.server.am.ActivityManagerService.checkGrantUriPermissionFromIntentLocked(ActivityManagerService.java:11394)
10-03 18:53:33.977 5939 5939 E AndroidRuntime: at com.android.server.am.ActivityManagerService.grantUriPermissionFromIntentLocked(ActivityManagerService.java:11450)
10-03 18:53:33.977 5939 5939 E AndroidRuntime: at com.android.server.am.ActivityStack.finishActivityResultsLocked(ActivityStack.java:4016)
10-03 18:53:33.977 5939 5939 E AndroidRuntime: at com.android.server.am.ActivityStack.finishActivityLocked(ActivityStack.java:4078)
10-03 18:53:33.977 5939 5939 E AndroidRuntime:
但是在某些情况下是没有这种信息的,下面将介绍如何增加对应的代码来获取StackTrace信息
1、本文只针对当前进程,其他进程的不介绍
2、本文只处理java层,native、kernel层的应用组很少涉及
一、自己抛异常,这种情况比较常见,这里不做介绍
二、使用Thead.dumpStack方法
在需要调试的方法中直接调用Thread.dumpStack()方法
public void testDumpStackTrace(){
Thread.dumpStack();
}
三、StackTraceElement&Log
在需要跟踪的方法中增加如下代码
StackTraceElement[] stack = new Throwable().getStackTrace();
for (StackTraceElement element : stack){
Log.d(TAG, " |----" + element.toString());
}
四、android.os.Debug
Debug类里面有几个getCaller方法可以获得当前的StackTrace,在需要调试的方法法中直接调用即可
public void dumpStackTrace(){
android.os.Debug.getCallers(9);
}