Skip to content

Commit 5f1ae97

Browse files
feat(kmp): add new init strategy & update versions (#10078)
* Update kotlin-multiplatform.mdx * Add init strategies and update index.mdx * Apply suggestions from code review Co-authored-by: vivianyentran <20403606+vivianyentran@users.noreply.github.com> --------- Co-authored-by: vivianyentran <20403606+vivianyentran@users.noreply.github.com>
1 parent e50fa30 commit 5f1ae97

File tree

4 files changed

+89
-57
lines changed

4 files changed

+89
-57
lines changed

docs/platforms/kotlin-multiplatform/initialization-strategies.mdx

Lines changed: 85 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,17 @@ description: "Strategies for initializing the Kotlin Multiplatform SDK."
44
sidebar_order: 3
55
---
66

7-
When it comes to initializing a Kotlin Multiplatform SDK, there are three strategies to consider:
7+
When it comes to initializing a Kotlin Multiplatform SDK, there are multiple strategies to consider:
88

9+
- [Use native platform options directly](#native-platform-options)
910
- [Shared initializer](#shared-initializer)
1011
- [Platform specific initializers](#platform-specific-initializers)
1112
- [Hybrid approach](#hybrid-approach)
1213

13-
## Shared Initializer
14-
15-
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.
14+
## Where to Place Initialization Code
1615

17-
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.
18-
19-
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.
20-
21-
<SignInNote />
22-
23-
```kotlin {filename:AppSetup.kt}
24-
import io.sentry.kotlin.multiplatform.Sentry
25-
26-
// Application context is only needed for Android targets
27-
fun initializeSentry() {
28-
val configuration: (SentryOptions) -> Unit = {
29-
it.dsn = "___PUBLIC_DSN___"
30-
// Add common configuration here
31-
}
32-
Sentry.init(configuration)
33-
}
34-
```
35-
36-
Now you can use that shared function to initialize the SDK.
16+
Regardless of the initialization strategy 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.
17+
Here are general guidelines for placing the initialization code for different platforms:
3718

3819
### Android
3920

@@ -62,11 +43,89 @@ func application(
6243
}
6344
```
6445

46+
## Native Platform Options
47+
48+
<Note>
49+
Requires Sentry SDK version `0.7.1` or higher.
50+
</Note>
51+
52+
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.
53+
For example, if you configure your KMP application for Android, you will have access to every option that the Android SDK provides.
54+
55+
### Apple Target Requirements
56+
57+
In order to be able to use the native platform options with Apple targets, you need to opt-in to `ExperimentalForeignApi`.
58+
59+
```kotlin {filename:build.gradle.kts}
60+
kotlin {
61+
// other configuration ...
62+
sourceSets {
63+
all {
64+
languageSettings.optIn("kotlinx.cinterop.ExperimentalForeignApi")
65+
}
66+
}
67+
}
68+
```
69+
70+
### Usage
71+
72+
```kotlin {filename:SentrySetup.kt}{tabTitle: commonMain}
73+
import io.sentry.kotlin.multiplatform.Sentry
74+
import io.sentry.kotlin.multiplatform.PlatformOptionsConfiguration
75+
76+
fun initializeSentry() {
77+
Sentry.initWithPlatformOptions(platformOptionsConfiguration())
78+
}
79+
80+
expect fun platformOptionsConfiguration(): PlatformOptionsConfiguration
81+
```
82+
83+
```kotlin {filename:SentrySetup.ios.kt}{tabTitle: iosMain}
84+
import io.sentry.kotlin.multiplatform.PlatformOptionsConfiguration
85+
86+
actual fun platformOptionsConfiguration(): PlatformOptionsConfiguration = {
87+
it.dsn = "___PUBLIC_DSN___"
88+
}
89+
```
90+
91+
```kotlin {filename:SentrySetup.android.kt}{tabTitle: androidMain}
92+
import io.sentry.kotlin.multiplatform.PlatformOptionsConfiguration
93+
94+
actual fun platformOptionsConfiguration(): PlatformOptionsConfiguration = {
95+
it.dsn = "___PUBLIC_DSN___"
96+
}
97+
```
98+
99+
## Shared Initializer
100+
101+
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.
102+
103+
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.
104+
105+
To initialize the SDK, create a Kotlin file in your `commonMain` (such as `AppSetup.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.
106+
107+
<SignInNote />
108+
109+
```kotlin {filename:AppSetup.kt}
110+
import io.sentry.kotlin.multiplatform.Sentry
111+
112+
// Application context is only needed for Android targets
113+
fun initializeSentry() {
114+
val configuration: (SentryOptions) -> Unit = {
115+
it.dsn = "___PUBLIC_DSN___"
116+
// Add common configuration here
117+
}
118+
Sentry.init(configuration)
119+
}
120+
```
121+
65122
## Platform-Specific Initializers
66123

67124
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.
68125

69126
This approach gives you the flexibility to fine-tune the SDK's behavior to meet the unique needs of each platform.
127+
However, the available options are limited to what the KMP SDK has implemented.
128+
If you need to use the respective platform's options directly, you should use the [native platform options](#native-platform-options) approach.
70129

71130
### Common
72131

@@ -108,37 +167,8 @@ actual fun initializeSentry() {
108167
}
109168
```
110169

111-
Now you can use that shared function to initialize the SDK.
112-
113-
### Android
114-
115-
```kotlin {filename:MainActivity.kt}
116-
import your.kmp.app.initializeSentry
117-
118-
class YourApplication : Application() {
119-
override fun onCreate() {
120-
super.onCreate()
121-
initializeSentry()
122-
}
123-
}
124-
```
125-
126-
### iOS
127-
128-
```swift {filename:AppDelegate.swift}
129-
import shared
130-
131-
func application(
132-
_ application: UIApplication,
133-
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil
134-
) -> Bool {
135-
AppSetupKt.initializeSentry()
136-
return true
137-
}
138-
```
139-
140170
## Hybrid Approach
141171

142-
It's also possible to mix the two initialization strategies by creating [intermediate sourceSets](https://kotlinlang.org/docs/multiplatform-hierarchy.html) that only target specific platforms. This allows you to use a shared initializer for some platforms while utilizing platform-specific initializers for others.
172+
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.
143173

144-
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.
174+
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.

docs/platforms/kotlin-multiplatform/native-access-sdk.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ cocoapods {
2929
// Make sure you use the proper version according to our Cocoa SDK Version Compatibility Table.
3030
pod("Sentry") {
3131
version = "~> 8.25"
32+
linkOnly = true
3233
extraOpts += listOf("-compiler-option", "-fmodules")
3334
}
3435
framework {

platform-includes/getting-started-install/kotlin-multiplatform.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ kotlin {
3939
// Make sure you use the proper version according to our Cocoa SDK Version Compatibility Table.
4040
pod("Sentry") {
4141
version = "~> 8.25"
42+
linkOnly = true
4243
extraOpts += listOf("-compiler-option", "-fmodules")
4344
}
4445
framework {

platform-includes/getting-started-primer/kotlin-multiplatform.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ Use the Kotlin Multiplatform and Cocoa SDK combinations listed in the table belo
1818
| Up to 0.3.0 | 8.4.0 |
1919
| 0.4.0 | 8.17.2 |
2020
| 0.5.0 | 8.21.0 |
21-
| 0.6.0 | 8.25.0 |
21+
| >= 0.6.0 | ~> 8.25 |
2222

2323
<Note>
24-
Starting May 1, 2024, Apple requires all apps submitted to the App Store to provide a list of privacy-related APIs they use. Use Sentry Cocoa SDK version `8.25.0` to ensure compliance with these requirements.
24+
Starting May 1, 2024, Apple requires all apps submitted to the App Store to provide a list of privacy-related APIs they use. Use Sentry Cocoa SDK version `8.25.0` or higher to ensure compliance with these requirements.
2525
Follow our [Apple Privacy Manifest](/platforms/kotlin-multiplatform/data-management/apple-privacy-manifest) guide to learn more.
2626
</Note>

0 commit comments

Comments
 (0)