Manual Setup for Kotlin Multiplatform

Learn how to manually set up the SDK.

If you can't (or prefer not to) use the gradle plugin, you can follow the instructions below to configure your application manually.

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. For Apple targets, this installs the Sentry Cocoa SDK with Kotlin's official SwiftPM import, which requires Kotlin 2.4 or newer:

shared/build.gradle.kts
Copied
plugins {
  kotlin("multiplatform")
  // .. your other plugins
}

kotlin {
  androidTarget()
  iosArm64()
  iosSimulatorArm64()

  sourceSets {
    commonMain.dependencies {
      implementation("io.sentry:sentry-kotlin-multiplatform:0.28.0")
    }
  }

  swiftPMDependencies {
    swiftPackage(
      url = url("https://github.com/getsentry/sentry-cocoa.git"),
      // Use the exact version from the Cocoa SDK version compatibility table.
      version = exact("9.29.2"),
      products = listOf(product("Sentry")),
    )
    // Only if you target macOS or tvOS. Use a higher version if your app needs it.
    macosMinimumDeploymentTarget.set("12.0")
    tvosMinimumDeploymentTarget.set("15.0")
  }
}

Make sure the Sentry Cocoa SDK version matches the one listed for your Kotlin Multiplatform SDK version in the version compatibility table.

Then run Kotlin's one-time Xcode linkage package setup so Xcode links the Sentry Cocoa SDK into your app.

Configuration should happen as early as possible in your application's lifecycle.

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

Copied
import io.sentry.kotlin.multiplatform.Sentry

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

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").
How to contribute   |  Edit this page   |  Create a docs issue   |  Get support