# Initialization Options | Sentry for Kotlin Multiplatform

When it comes to initializing a Kotlin Multiplatform SDK, there are multiple options to consider:

* [Use native platform options directly](https://docs.sentry.io/platforms/kotlin/guides/kotlin-multiplatform/initialization-strategies.md#native-platform-options)
* [Shared initializer](https://docs.sentry.io/platforms/kotlin/guides/kotlin-multiplatform/initialization-strategies.md#shared-initializer)
* [Platform specific initializers](https://docs.sentry.io/platforms/kotlin/guides/kotlin-multiplatform/initialization-strategies.md#platform-specific-initializers)
* [Hybrid approach](https://docs.sentry.io/platforms/kotlin/guides/kotlin-multiplatform/initialization-strategies.md#hybrid-approach)

## [Where to Place Initialization Code](https://docs.sentry.io/platforms/kotlin/guides/kotlin-multiplatform/initialization-strategies.md#where-to-place-initialization-code)

Regardless of the initialization option you choose, it's important to place the initialization code at an early lifecycle stage of your application to ensure the SDK is set up correctly before it is used. Here are general guidelines for placing the initialization code for different platforms:

`MainActivity.kt`

```kotlin
import your.kmp.app.initializeSentry

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

## [Native Platform Options](https://docs.sentry.io/platforms/kotlin/guides/kotlin-multiplatform/initialization-strategies.md#native-platform-options)

Requires Sentry SDK version `0.7.1` or higher.

Available options are determined by the native SDK versions used in the build. For example, Kotlin Multiplatform SDK `0.10.0` uses Sentry Android `7.16.0` and Sentry Cocoa `8.38.0`, so it supports all options from these versions.

This approach involves initializing the SDK with the platform's specific options directly, using `PlatformOptionsConfiguration`. This strategy is ideal for projects that require configuring options that are not yet supported by the KMP SDK, such as experimental options. For example, if you configure your KMP application for Android, you will have access to every option that the Android SDK provides.

### [Apple Target Requirements](https://docs.sentry.io/platforms/kotlin/guides/kotlin-multiplatform/initialization-strategies.md#apple-target-requirements)

In order to be able to use the native platform options with Apple targets, you need to opt-in to `ExperimentalForeignApi`.

`build.gradle.kts`

```kotlin
kotlin {
    // other configuration ...
  sourceSets {
    all {
        languageSettings.optIn("kotlinx.cinterop.ExperimentalForeignApi")
    }
  }
}
```

### [Usage](https://docs.sentry.io/platforms/kotlin/guides/kotlin-multiplatform/initialization-strategies.md#usage)

`SentrySetup.kt`

```kotlin
import io.sentry.kotlin.multiplatform.Sentry
import io.sentry.kotlin.multiplatform.PlatformOptionsConfiguration

fun initializeSentry() {
  Sentry.initWithPlatformOptions(platformOptionsConfiguration())
}

expect fun platformOptionsConfiguration(): PlatformOptionsConfiguration
```

## [Shared Initializer](https://docs.sentry.io/platforms/kotlin/guides/kotlin-multiplatform/initialization-strategies.md#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` (such as `SentrySetup.kt` or whatever you decide to call it), and write an initialization function. You'll then be able to call it in an early lifecycle stage in your platforms.

`SentrySetup.kt`

```kotlin
import io.sentry.kotlin.multiplatform.Sentry
import io.sentry.kotlin.multiplatform.SentryOptions

// 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)
}
```

## [Platform-Specific Initializers](https://docs.sentry.io/platforms/kotlin/guides/kotlin-multiplatform/initialization-strategies.md#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. However, the available options are limited to what the KMP SDK has implemented. If you need to use the respective platform's options directly, you should use the [native platform options](https://docs.sentry.io/platforms/kotlin/guides/kotlin-multiplatform/initialization-strategies.md#native-platform-options) approach.

### [Common](https://docs.sentry.io/platforms/kotlin/guides/kotlin-multiplatform/initialization-strategies.md#common)

`commonMain/SentrySetup.kt`

```kotlin
import io.sentry.kotlin.multiplatform.Context

expect fun initializeSentry()
```

### [androidMain](https://docs.sentry.io/platforms/kotlin/guides/kotlin-multiplatform/initialization-strategies.md#androidmain)

`androidMain/SentrySetup.kt`

```kotlin
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](https://docs.sentry.io/platforms/kotlin/guides/kotlin-multiplatform/initialization-strategies.md#iosmain)

`iosMain/SentrySetup.kt`

```kotlin
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
  }
}
```

## [Hybrid Approach](https://docs.sentry.io/platforms/kotlin/guides/kotlin-multiplatform/initialization-strategies.md#hybrid-approach)

It's also possible to mix the initialization strategies by creating [intermediate sourceSets](https://kotlinlang.org/docs/multiplatform-hierarchy.html) that 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 platforms. This approach provides a balance between consistency and customization.
