Initialization Strategies

When it comes to initializing a Kotlin Multiplatform SDK, there are three strategies to consider:

Shared Initializer

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

Android

MainActivity.kt
Copied
import your.kmp.app.initializeSentry

class YourApplication : Application() {
  override fun onCreate() {
    super.onCreate()
      initializeSentry()
   }
}

iOS

AppDelegate.swift
Copied
import shared

func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil
) -> Bool {
    AppSetupKt.initializeSentry()
    return true
}

Platform-Specific Initializers

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.

Common

commonMain/AppSetup.kt
Copied
import io.sentry.kotlin.multiplatform.Context

expect fun initializeSentry()

androidMain

androidMain/AppSetup.kt
Copied
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

iosMain/AppSetup.kt
Copied
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.

Android

MainActivity.kt
Copied
import your.kmp.app.initializeSentry

class YourApplication : Application() {
  override fun onCreate() {
    super.onCreate()
      initializeSentry()
   }
}

iOS

AppDelegate.swift
Copied
import shared

func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil
) -> Bool {
    AppSetupKt.initializeSentry()
    return true
}

Hybrid Approach

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.

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