Jetpack Compose for Android

Learn more about Sentry's integrations for Jetpack Compose.

The sentry-compose-android and sentry-android-navigation3 libraries provide Jetpack Compose support in the following areas:

(New in version 6.16.0)

With Jetpack Compose performance metrics the Sentry Android SDK can automatically measure the initial composition and rendering time as performance spans of your @Composable UI elements.

Once performance metrics have been enabled, you'll see two spans inside your transactions:

  • ui.compose.composition, describing the time it took to execute your @Composable
  • ui.compose.rendering, describing the time it took to render your @Composable to the canvas

The SentryTraced function also provides an enableUserInteractionTracing argument, which automatically applies a Modifier.sentryTag() and enables the User Interactions feature mentioned below.

To add the Jetpack Compose integration, install the Android SDK, then add the sentry-compose-android dependency using Gradle:

Copied
implementation 'io.sentry:sentry-android:8.64.0'
implementation 'io.sentry:sentry-compose-android:8.64.0'

Once installed, you need to wrap your @Composable body with the SentryTraced function.

Copied
import io.sentry.compose.SentryTraced

@Composable
fun LoginScreen() {
  SentryTraced("login_screen") {
    Column {
      // ...
      Button(
          onClick = { TODO() }) {
          Text(text = "Login")
      }
    }
  }
}

(New in version 6.10.0)

The Sentry User Interactions feature for Jetpack Compose can detect click, scroll, and swipe gestures. For every gesture, the SDK can automatically collect breadcrumbs and launch transactions.

To add the Jetpack Compose integration, install the Android SDK, then add the sentry-compose-android dependency using Gradle:

Copied
implementation 'io.sentry:sentry-android:8.64.0'
implementation 'io.sentry:sentry-compose-android:8.64.0'

This feature is disabled by default, but you can enable it the following ways.

AndroidManifest.xml
Copied
<application>
    <meta-data
    android:name="io.sentry.traces.user-interaction.enable"
    android:value="true"
  />
    <meta-data
    android:name="io.sentry.breadcrumbs.user-interaction"
    android:value="true"
  />
</application>

If you initialize the SDK manually as mentioned here, you can enable user interactions like this:

MyApplication.kt
Copied
SentryAndroid.init(this) { options ->
    // ...
    options.isEnableUserInteractionTracing = true
    options.isEnableUserInteractionBreadcrumbs = true
}

The Sentry SDK requires you to have either Modifier.sentryTag("<tag>") or Modifier.testTag("<tag>") applied on each @Composable you want to capture to determine its identifier. Here's an example:

Copied
import io.sentry.compose.sentryTag

@Composable
fun LoginScreen() {
  Column {
    // ...
    Button(
        modifier = Modifier.sentryTag("button_login"),
        onClick = { TODO() }) {
        Text(text = "Login")
    }
  }
}

User interaction instrumentation uses the sentryTag or testTag value to identify the composable in the recorded breadcrumb and transaction. Keep these tags stable and free of PII or other sensitive data.

If needed, you can filter sensitive data from breadcrumbs and transactions before they're sent to Sentry via the SentryOptions.BeforeBreadcrumbCallback and SentryOptions.BeforeSendTransactionCallback, respectively.

Composable functions can run many times during recomposition. Don't send custom Sentry spans, messages, breadcrumbs, or other telemetry directly from a composable body, as it can duplicate telemetry or attach it to the wrong UI lifecycle moment.

Instead, send telemetry from:

  • LaunchedEffect, DisposableEffect, or SideEffect (more info in Google's developer docs)
  • Event callbacks such as onClick when the telemetry corresponds to a user action
  • APIs that run after composition, such as draw-time or layout-time lambdas, when that timing is what you want to measure

For example, avoid capturing a message directly from the body:

Copied
@Composable
fun LoginScreen() {
  Sentry.captureMessage("Login screen shown")
  // ...
}

Instead, capture state in the body as needed but send it from an Effect API:

Copied
@Composable
fun LoginScreen() {
  val firstComposedAt = remember { Instant.now() }

  LaunchedEffect(Unit) {
    Sentry.captureMessage("Login screen shown: $firstComposedAt")
  }
  // ...
}
Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").