Compose Multiplatform

Learn how to integrate Sentry's Kotlin Multiplatform SDK into your Compose Multiplatform project for cross-platform error tracking.

Compose Multiplatform is JetBrains' declarative UI framework that allows you to build native user interfaces for desktop, web, and mobile platforms using a single Kotlin codebase. It extends Jetpack Compose beyond Android to create multiplatform applications.

You need to use the Sentry Kotlin Multiplatform SDK which provides error tracking across multiple Kotlin targets. While the SDK functions in Compose Multiplatform projects use the same APIs as pure KMP setups, it's important to note a few differences:

  • Experimental support: The KMP SDK works in CMP projects and currently supports capturing crashes and errors as well as other basic features that are available through the Sentry KMP SDK.
  • Not CMP-native: No specific CMP optimizations or testing.
  • Untested edge cases: Some platform-specific behaviors may be incomplete.

Sentry captures data by using an SDK within your application's runtime.

To install the Kotlin Multiplatform SDK in your Compose Multiplatform project, you need to add the following to your build.gradle.kts file in your shared module:

composeApp/build.gradle.kts
Copied
plugins {
  id("io.sentry.kotlin.multiplatform.gradle") version "0.16.0"
}

The plugin does the following:

  • Automatically installs the Sentry KMP dependency to commonMain.
  • If you use the Kotlin Cocoapods plugin, it installs the Sentry Cocoa dependency.
  • If you use Swift Package Manager, it sets up linking to the Sentry Cocoa framework.

Configuration should happen as early as possible in your application's lifecycle to ensure a working instrumentation. Configure with the following steps:

  1. Add a function to initialize Sentry to your shared module (commonMain).
  2. Call the function in your iOS and Android app modules.
SentrySetup.kt
Copied
import io.sentry.kotlin.multiplatform.Sentry

fun initializeSentry() {
  Sentry.init { options ->
    options.dsn = "https://examplePublicKey@o0.ingest.sentry.io/0"
    // Adds request headers and IP for users, for more info visit:
    // https://docs.sentry.io/platforms/kotlin/guides/kotlin-multiplatform/data-management/data-collected/
    options.sendDefaultPii = true
  }
}

This snippet includes an intentional error, so you can test that everything is working as soon as you set it up.

Copied
import io.sentry.kotlin.multiplatform.Sentry

// Add the following button to your Compose UI and click it to trigger an error
Button(onClick = {
    try {
        throw Exception("Sentry works!")
    } catch (e: Exception) {
        Sentry.captureException(e)
    }
}) {
    Text("Send an error to Sentry")
}

Error stack traces in your app might appear unreadable or obfuscated, making it hard to debug issues. To make stack traces clear and human-readable in Sentry, you need to upload your app's debug symbols.

For step-by-step instructions, see the Debug Symbols guide.

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").