Trace

public final class Trace
extends Object

java.lang.Object
   ↳ androidx.tracing.Trace


Writes trace events to the system trace buffer.

These trace events can be collected and visualized using the Android Studio System Trace, Perfetto, and Systrace tools.

Tracing should generally be performed in a non-debuggable app for more accurate measurements, representative of real user experience. In a non-debuggable app, tracing is enabled if a trace is currently being captured, as well as one of the following:

  • Android 12 (API 31) or greater: On by default, unless
    <profileable enabled=false/>
    or
    <profileable shell=false/>
    is set in the manifest.
  • Android 10 or 11 (API 29 or 30):
    <profileable shell=true/>
    is set in the manifest, or forceEnableAppTracing() has been called
  • JellyBean through Android 11 (API 18 through API 28): forceEnableAppTracing() has been called

This tracing mechanism is independent of the method tracing mechanism offered by Debug.startMethodTracing(). In particular, it enables tracing of events that occur across multiple processes.

For information see Overview of system tracing.

Summary

Public methods

static void beginAsyncSection(String methodName, int cookie)

Writes a trace message to indicate that a given section of code has begun.

static void beginSection(String label)

Writes a trace message to indicate that a given section of code has begun.

static void endAsyncSection(String methodName, int cookie)

Writes a trace message to indicate that the current method has ended.

static void endSection()

Writes a trace message to indicate that a given section of code has ended.

static void forceEnableAppTracing()

Enables the app tracing tag in a non-debuggable process.

static boolean isEnabled()

Checks whether or not tracing is currently enabled.

static void setCounter(String counterName, int counterValue)

Writes trace message to indicate the value of a given counter.

Inherited methods