Sentry for Kotlin Multiplatform

Kotlin Multiplatform lets you write code once and use it on various platforms. It makes building apps easier and more efficient by sharing common code, while still benefitting from native capabilities.

Sentry's Kotlin Multiplatform SDK builds on top of multiple Sentry SDKs, allowing developers to share code between platforms while being able to integrate Sentry's features into their applications. Installing this SDK will enable native crash reporting and automatic breadcrumbs for app lifecycle and UI events.

Don't already have an account and Sentry project established? Head over to sentry.io, then return to this page.

Xcode 16 support starts with Sentry Cocoa SDK version 8.36.0 or higher.

Starting May 1, 2024, Apple requires all apps submitted to the App Store to provide a list of privacy-related APIs they use. Use Sentry Cocoa SDK version 8.25.0 or higher to ensure compliance with these requirements. Follow our Apple Privacy Manifest guide to learn more.

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

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

shared/build.gradle.kts
plugins {
  id("io.sentry.kotlin.multiplatform.gradle") version "0.30.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 NONE to 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.

SentrySetup.kt
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
  }
}

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

fun captureError() {
  try {
    throw Exception("This is a test.")
  } catch (e: Exception) {
    Sentry.captureException(e)
  }
}

Learn more about manually capturing an error or message in our Usage documentation.

To view and resolve the recorded error, log into sentry.io and select your project. Clicking on the error's title will open a page where you can see detailed information and mark it as resolved.

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