Analyze a stack trace

Debugging an app often requires working with stack traces. A stack trace is generated whenever your app crashes because of an error or an exception. You can also print a stack trace at any point in your app code using methods such as Thread.dumpStack().

While your app is running in debug mode on a connected device, Android Studio prints and highlights stack traces in the logcat view, as shown in figure 1.

Figure 1. A stacktrace in logcat.

A stack trace shows a list of method calls that lead to the exception being thrown, together with the filenames and line numbers where the calls happened. You can click on the highlighted filenames to open the files and examine the source of the method invocation. Click Up the stack trace and Down the stack trace to quickly move between stack trace lines displayed in the logcat window.

Open Stack traces from external sources

Sometimes you want to analyze stack traces that were shared with you in a bug report as opposed to those you found while debugging. For example, you might be collecting stack traces generated on your users’ devices from the Google Play Console or from some other tool such as Firebase Crash Reporting.

To get the same highlighted and clickable view of an external stack trace from a bug report, follow these steps:

  1. Open your project in Android Studio.

    Note: Make sure the source code you're viewing is from the exact same version of your app which generated the stack trace. If the code is different, you will get mismatches between the filenames and line numbers, or the order of calls in the stack trace and your project.

  2. From the Analyze menu, click Analyze Stack Trace.

    Analyze Stack Trace window

  3. Paste the stack trace text into the Analyze Stack Trace window and click OK.
  4. Android Studio opens a new <Stacktrace> tab with the stack trace you pasted under the Run window.

    Stacktrace window

Monitor the clipboard for new stack traces

If you work with external stack traces a lot, you can improve your productivity by allowing Android Studio to continuously monitor the system clipboard for new stacktraces:

  1. Open the Analyze Stacktrace tool.
  2. Check the Automatically detect and analyze thread dumps copied to the clipboard outside of IntelliJ IDEA checkbox.
  3. Copy a stack trace in another application (such as from a web browser) to the system clipboard.
  4. When you come back to the Android Studio window, the stack trace opens automatically under the Run window, without you having to paste it in the Analyze Stacktrace window.