Sentry for Compose Multiplatform
Learn how to integrate Sentry's Kotlin Multiplatform SDK into your Compose Multiplatform project for cross-platform error tracking.
Experimental Support
While Sentry's Kotlin Multiplatform SDK works in Compose Multiplatform (CMP) projects, it isn't natively supported or fully tested for CMP. Some features may behave unexpectedly or provide incomplete data. Use with caution in production environments and report any issues you encounter.
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.ktsplugins {
id("io.sentry.kotlin.multiplatform.gradle") version "0.29.0"
}
plugins {
id("io.sentry.kotlin.multiplatform.gradle") version "0.29.0"
}
The plugin does the following:
- Automatically installs the Sentry KMP dependency to
commonMain. - Installs the Sentry Cocoa dependency for Apple targets with Swift Package Manager (SwiftPM), using Kotlin's official SwiftPM import on Kotlin 2.4 and newer, or the spm4Kmp plugin if you've applied it.
- If you added Sentry Cocoa to your Xcode project yourself, set the Apple provider to
NONEto avoid installing it twice. The plugin then links your existing framework. Learn more in Install the Sentry Cocoa SDK Yourself.
If you use Kotlin's official SwiftPM import, run Kotlin's one-time Xcode linkage package setup so Xcode links Sentry Cocoa into your app.
If you use spm4Kmp, apply the Sentry Gradle plugin before spm4Kmp in the plugins block.
Starting with version 0.28.0, the SDK requires iOS 15, tvOS 15, macOS 12, or watchOS 9 or newer, and no longer supports installing Sentry Cocoa with CocoaPods. Learn more in the CocoaPods migration guide.
Configuration should happen as early as possible in your application's lifecycle to ensure a working instrumentation. Configure with the following steps:
- Add a function to initialize Sentry to your shared module (
commonMain). - Call the function in your iOS and Android app modules.
SentrySetup.ktimport io.sentry.kotlin.multiplatform.Sentry
fun initializeSentry() {
Sentry.init { options ->
options.dsn = "___PUBLIC_DSN___"
// 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
}
}
import io.sentry.kotlin.multiplatform.Sentry
fun initializeSentry() {
Sentry.init { options ->
options.dsn = "___PUBLIC_DSN___"
// 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
}
}
import ComposeApp // Replace this with the name of the shared framework
@main
struct iOSApp: App {
init() {
SentrySetupKt.initializeSentry()
}
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
// OR alternatively in AppDelegate:
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil
) -> Bool {
SentrySetupKt.initializeSentry()
return true
}
import your.kmp.app.initializeSentry
class YourApplication : Application() {
override fun onCreate() {
super.onCreate()
initializeSentry()
}
}
This snippet includes an intentional error, so you can test that everything is working as soon as you set it up.
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")
}
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")
}
For a complete list of supported platforms and target presets, see the Kotlin Multiplatform features documentation.
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 Android applications, you need to manually upload ProGuard mapping files using the sentry-cli. Follow the ProGuard Mapping Upload guide for detailed instructions.
For iOS applications, follow the Uploading Debug Symbols guide to set up debug symbols upload.
- Explore practical guides on what to monitor, log, track, and investigate after setup
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").