# TraceCompat

Artifact: [androidx.core:core](https://developer.android.com/jetpack/androidx/releases/core) [View Source](https://cs.android.com/search?q=file:androidx/core/os/TraceCompat.java+class:androidx.core.os.TraceCompat) Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0) Deprecated in [1.5.0](https://developer.android.com/jetpack/androidx/releases/core#1.5.0)

*** ** * ** ***


```
public final class TraceCompat
```

<br />

*** ** * ** ***

> [!CAUTION]
> **This class is deprecated.**   
>
> TraceCompat is deprecated in favor of androidx.tracing.Trace. Please use that instead.

Writes trace events to the system trace buffer. These trace events can be collected and visualized using the Systrace tool.

This tracing mechanism is independent of the method tracing mechanism offered by `https://developer.android.com/reference/android/os/Debug.html#startMethodTracing()`. In particular, it enables tracing of events that occur across multiple processes.

For information about using the Systrace tool, read [Overview of system tracing](https://developer.android.com/studio/profile/systrace).

## Summary

| ### Public methods |
|---|---|
| `static void` | `[beginAsyncSection](https://developer.android.com/reference/androidx/core/os/TraceCompat#beginAsyncSection(java.lang.String,int))(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html methodName, int cookie)` **This method is deprecated.** Deprecated in Java |
| `static void` | `[beginSection](https://developer.android.com/reference/androidx/core/os/TraceCompat#beginSection(java.lang.String))(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html sectionName)` **This method is deprecated.** Deprecated in Java |
| `static void` | `[endAsyncSection](https://developer.android.com/reference/androidx/core/os/TraceCompat#endAsyncSection(java.lang.String,int))(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html methodName, int cookie)` **This method is deprecated.** Deprecated in Java |
| `static void` | `[endSection](https://developer.android.com/reference/androidx/core/os/TraceCompat#endSection())()` **This method is deprecated.** Deprecated in Java |
| `static boolean` | `[isEnabled](https://developer.android.com/reference/androidx/core/os/TraceCompat#isEnabled())()` **This method is deprecated.** Deprecated in Java |
| `static void` | `[setCounter](https://developer.android.com/reference/androidx/core/os/TraceCompat#setCounter(java.lang.String,int))(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html counterName, int counterValue)` **This method is deprecated.** Deprecated in Java |

## Public methods

### beginAsyncSection

Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/core#1.2.0) Deprecated in [1.5.0](https://developer.android.com/jetpack/androidx/releases/core#1.5.0)

```
public static void [beginAsyncSection](https://developer.android.com/reference/androidx/core/os/TraceCompat#beginAsyncSection(java.lang.String,int))(@NonNull String methodName, int cookie)
```

> [!CAUTION]
> **This method is deprecated.**   
> Deprecated in Java

Writes a trace message to indicate that a given section of code has begun. Must be followed by a call to `https://developer.android.com/reference/androidx/core/os/TraceCompat#endAsyncSection(java.lang.String,int)` with the same methodName and cookie. Unlike `https://developer.android.com/reference/androidx/core/os/TraceCompat#beginSection(java.lang.String)` and `https://developer.android.com/reference/androidx/core/os/TraceCompat#endSection()`, asynchronous events do not need to be nested. The name and cookie used to begin an event must be used to end it.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html methodName` | The method name to appear in the trace. |
| `int cookie` | Unique identifier for distinguishing simultaneous events |

### beginSection

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0) Deprecated in [1.5.0](https://developer.android.com/jetpack/androidx/releases/core#1.5.0)

```
public static void [beginSection](https://developer.android.com/reference/androidx/core/os/TraceCompat#beginSection(java.lang.String))(@NonNull String sectionName)
```

> [!CAUTION]
> **This method is deprecated.**   
> Deprecated in Java

Writes a trace message to indicate that a given section of code has begun. This call must be followed by a corresponding call to `https://developer.android.com/reference/androidx/core/os/TraceCompat#endSection()` on the same thread.

At this time the vertical bar character '\|', newline character '\\n', and null character '\\0' are used internally by the tracing mechanism. If sectionName contains these characters they will be replaced with a space character in the trace.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html sectionName` | The name of the code section to appear in the trace. This may be at most 127 Unicode code units long. |

### endAsyncSection

Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/core#1.2.0) Deprecated in [1.5.0](https://developer.android.com/jetpack/androidx/releases/core#1.5.0)

```
public static void [endAsyncSection](https://developer.android.com/reference/androidx/core/os/TraceCompat#endAsyncSection(java.lang.String,int))(@NonNull String methodName, int cookie)
```

> [!CAUTION]
> **This method is deprecated.**   
> Deprecated in Java

Writes a trace message to indicate that the current method has ended. Must be called exactly once for each call to `https://developer.android.com/reference/androidx/core/os/TraceCompat#beginAsyncSection(java.lang.String,int)` using the same name and cookie.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html methodName` | The method name to appear in the trace. |
| `int cookie` | Unique identifier for distinguishing simultaneous events |

### endSection

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0) Deprecated in [1.5.0](https://developer.android.com/jetpack/androidx/releases/core#1.5.0)

```
public static void [endSection](https://developer.android.com/reference/androidx/core/os/TraceCompat#endSection())()
```

> [!CAUTION]
> **This method is deprecated.**   
> Deprecated in Java

Writes a trace message to indicate that a given section of code has ended. This call must be preceeded by a corresponding call to `https://developer.android.com/reference/androidx/core/os/TraceCompat#beginSection(java.lang.String)`. Calling this method will mark the end of the most recently begun section of code, so care must be taken to ensure that beginSection / endSection pairs are properly nested and called from the same thread.

### isEnabled

Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/core#1.2.0) Deprecated in [1.5.0](https://developer.android.com/jetpack/androidx/releases/core#1.5.0)

```
public static boolean [isEnabled](https://developer.android.com/reference/androidx/core/os/TraceCompat#isEnabled())()
```

> [!CAUTION]
> **This method is deprecated.**   
> Deprecated in Java

Checks whether or not tracing is currently enabled. This is useful to avoid intermediate string creation for trace sections that require formatting. It is not necessary to guard all Trace method calls as they internally already check this. However it is recommended to use this to prevent creating any temporary objects that would then be passed to those methods to reduce runtime cost when tracing isn't enabled.

| Returns |
|---|---|
| `boolean` | true if tracing is currently enabled, false otherwise |

### setCounter

Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/core#1.2.0) Deprecated in [1.5.0](https://developer.android.com/jetpack/androidx/releases/core#1.5.0)

```
public static void [setCounter](https://developer.android.com/reference/androidx/core/os/TraceCompat#setCounter(java.lang.String,int))(@NonNull String counterName, int counterValue)
```

> [!CAUTION]
> **This method is deprecated.**   
> Deprecated in Java

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

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html counterName` | The counter name to appear in the trace. |
| `int counterValue` | The counter value. |