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.ktsplugins {
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")
}
}
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.
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 the Sentry Cocoa SDK with CocoaPods.
Configuration should happen as early as possible in your application's lifecycle.
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 shared
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil
) -> Bool {
SentrySetupKt.initializeSentry()
return true
}
// OR if you're using SwiftUI:
@main
struct SwiftUIApp: App {
init() {
SentrySetupKt.initializeSentry()
}
}
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
fun captureError() {
try {
throw Exception("This is a test.")
} catch (e: Exception) {
Sentry.captureException(e)
}
}
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.
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").