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.Context
import io.sentry.kotlin.multiplatform.Sentry

// Application context is only needed for Android targets
fun initializeSentry(context: Context?) {
  Sentry.init(context) {
    it.dsn = "https://examplePublicKey@o0.ingest.sentry.io/0"
  }
}

Android

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

class YourApplication : Application() {
  override fun onCreate() {
    super.onCreate()
      // Make sure to add the context!
      initializeSentry(this)
   }
}

Cocoa/Apple

AppDelegate.swift
Copied
import shared

func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil
) -> Bool {
    AppSetupKt.initializeSentry(context = nil)
    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.

With platform-specific initializers, you can initialize the SDK directly in the target platform's codebase. This approach gives you the flexibility to fine-tune the SDK's behavior to meet the unique needs of each platform.

Android

MainActivity.kt
Copied
import io.sentry.kotlin.multiplatform.Sentry

class YourApplication : Application() {
  override fun onCreate() {
    super.onCreate()
      // Make sure to add the context!
      Sentry.init(this) {
        it.dsn = "https://examplePublicKey@o0.ingest.sentry.io/0"
      }
   }
}

Cocoa/Apple

AppDelegate.swift
Copied
import shared

func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil
) -> Bool {
    Sentry.shared.doInit() { options in
      options.dsn = "https://examplePublicKey@o0.ingest.sentry.io/0"
    }
    return true
}

Hybrid Approach

It's also possible to mix the two initialization strategies by creating custom 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!) to suggesting an update ("yeah, this would be better").