Initialization Strategies
When it comes to initializing a Kotlin Multiplatform SDK, there are three strategies to consider:
The shared initializer approach involves initializing the SDK in your shared codebase, using the same configuration options for all platforms. This approach is ideal for projects that prioritize consistency across platforms and do not require platform-specific customizations.
Using a shared initializer provides a single source of truth for your SDK's configuration options. This can simplify maintenance and debugging efforts, as you only need to update one codebase for all platforms.
To initialize the SDK, create a Kotlin file in your commonMain
e.g. AppSetup.kt
, (or whatever you decide to call it), and create an initialization function. You'll then be able to call it in an early lifecycle stage in your platforms.
AppSetup.kt
import io.sentry.kotlin.multiplatform.Sentry
// Application context is only needed for Android targets
fun initializeSentry() {
val configuration: (SentryOptions) -> Unit = {
it.dsn = "https://examplePublicKey@o0.ingest.sentry.io/0"
// Add common configuration here
}
Sentry.init(configuration)
}
Now you can use that shared function to initialize the SDK.
MainActivity.kt
import your.kmp.app.initializeSentry
class YourApplication : Application() {
override fun onCreate() {
super.onCreate()
initializeSentry()
}
}
AppDelegate.swift
import shared
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil
) -> Bool {
AppSetupKt.initializeSentry()
return true
}
Platform-specific initializers allow for customization of the SDK's configuration options on a per-platform basis. This approach can be particularly useful when your SDK requires platform-specific functionality or performance optimizations.
This approach gives you the flexibility to fine-tune the SDK's behavior to meet the unique needs of each platform.
commonMain/AppSetup.kt
import io.sentry.kotlin.multiplatform.Context
expect fun initializeSentry()
androidMain/AppSetup.kt
import io.sentry.kotlin.multiplatform.Sentry
import io.sentry.kotlin.multiplatform.Context
actual fun initializeSentry() {
Sentry.init {
it.dsn = "https://examplePublicKey@o0.ingest.sentry.io/0"
// Add Android-specific configuration here
}
}
iosMain/AppSetup.kt
import io.sentry.kotlin.multiplatform.Sentry
import io.sentry.kotlin.multiplatform.Context
actual fun initializeSentry() {
Sentry.init {
it.dsn = "https://examplePublicKey@o0.ingest.sentry.io/0"
// Add iOS-specific configuration here
}
}
Now you can use that shared function to initialize the SDK.
MainActivity.kt
import your.kmp.app.initializeSentry
class YourApplication : Application() {
override fun onCreate() {
super.onCreate()
initializeSentry()
}
}
AppDelegate.swift
import shared
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil
) -> Bool {
AppSetupKt.initializeSentry()
return true
}
It's also possible to mix the two initialization strategies by creating intermediate sourceSets that only target specific platforms. This allows you to use a shared initializer for some platforms while utilizing platform-specific initializers for others.
For example, you may choose to use a shared initializer for Android and iOS platforms, while using a platform-specific initializer for the watchOS and tvOS platform. This approach provides a balance between consistency and customization.
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").
- Package:
- maven:io.sentry:sentry-kotlin-multiplatform
- Version:
- 0.5.0
- Repository:
- https://github.com/getsentry/sentry-kotlin-multiplatform