Skip to content

Commit 1c5183c

Browse files
feat: override native options (#221)
* Add native options * Update * Update * Update * Format * Update * Update * Format code * Update * Update * Update changelog * Format code * update * Update api * Update * Format code * Update * Update * Update comments * Fix detekt * Update * Add review * Update * Fix test * Update samples * Update * Update * Format code * Update --------- Co-authored-by: Sentry Github Bot <[email protected]>
1 parent dc19da0 commit 1c5183c

File tree

50 files changed

+628
-121
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+628
-121
lines changed

CHANGELOG.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,44 @@
22

33
## Unreleased
44

5+
### Features
6+
7+
- Allow initializing the KMP SDK with native options ([#221](https://github.com/getsentry/sentry-kotlin-multiplatform/pull/221))
8+
- This allows you to initialize the SDK with platform-specific options that may not be available in the common code of the KMP SDK yet.
9+
10+
Usage:
11+
```kotlin
12+
// build.gradle.kts
13+
kotlin {
14+
sourceSets {
15+
all {
16+
languageSettings.optIn("kotlinx.cinterop.ExperimentalForeignApi")
17+
}
18+
}
19+
}
20+
21+
// commonMain
22+
fun init() {
23+
Sentry.initWithPlatformOptions(createPlatformOptions())
24+
}
25+
26+
expect fun platformOptionsConfiguration(): PlatformOptionsConfiguration
27+
28+
// iOS
29+
actual fun createPlatformOptions(): PlatformOptionsConfiguration = {
30+
dsn = "your_dsn"
31+
release = "1.0.0"
32+
// ...
33+
}
34+
35+
// Android
36+
actual fun createPlatformOptions(): PlatformOptionsConfiguration = {
37+
dsn = "your_dsn"
38+
release = "1.0.0"
39+
// ...
40+
}
41+
```
42+
543
### Dependencies
644

745
- Bump Java SDK from v7.8.0 to v7.9.0 ([#219](https://github.com/getsentry/sentry-kotlin-multiplatform/pull/219))

scripts/build-apple.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ PROJECT_NAME="$1"
1111
"iosX64Test" \
1212
"watchosX64Test" \
1313
"tvosX64Test" \
14+
"iosSimulatorArm64Test" \
1415
"publishKotlinMultiplatformPublicationToMavenLocal" \
1516
"publishIosArm64PublicationToMavenLocal" \
1617
"publishIosSimulatorArm64PublicationToMavenLocal" \

sentry-kotlin-multiplatform/api/android/sentry-kotlin-multiplatform.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public final class io/sentry/kotlin/multiplatform/Sentry {
8484
public final fun crash ()V
8585
public final fun init (Landroid/content/Context;Lkotlin/jvm/functions/Function1;)V
8686
public final fun init (Lkotlin/jvm/functions/Function1;)V
87+
public final fun initWithPlatformOptions (Lkotlin/jvm/functions/Function1;)V
8788
public final fun isCrashedLastRun ()Z
8889
public final fun setUser (Lio/sentry/kotlin/multiplatform/protocol/User;)V
8990
}

sentry-kotlin-multiplatform/api/jvm/sentry-kotlin-multiplatform.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public final class io/sentry/kotlin/multiplatform/Sentry {
8181
public final fun crash ()V
8282
public final fun init (Lio/sentry/kotlin/multiplatform/Context;Lkotlin/jvm/functions/Function1;)V
8383
public final fun init (Lkotlin/jvm/functions/Function1;)V
84+
public final fun initWithPlatformOptions (Lkotlin/jvm/functions/Function1;)V
8485
public final fun isCrashedLastRun ()Z
8586
public final fun setUser (Lio/sentry/kotlin/multiplatform/protocol/User;)V
8687
}

sentry-kotlin-multiplatform/build.gradle.kts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import com.codingfeline.buildkonfig.compiler.FieldSpec.Type.STRING
2+
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
23
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
34
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
45

@@ -64,6 +65,11 @@ kotlin {
6465
macosArm64()
6566

6667
sourceSets {
68+
@OptIn(ExperimentalKotlinGradlePluginApi::class)
69+
compilerOptions {
70+
freeCompilerArgs.add("-Xexpect-actual-classes")
71+
}
72+
6773
all {
6874
languageSettings.apply {
6975
optIn("kotlinx.cinterop.ExperimentalForeignApi")
@@ -87,7 +93,7 @@ kotlin {
8793
}
8894

8995
androidMain.dependencies {
90-
implementation(Config.Libs.sentryAndroid)
96+
api(Config.Libs.sentryAndroid)
9197
}
9298

9399
// androidUnitTest.dependencies doesn't exist
@@ -102,7 +108,7 @@ kotlin {
102108
val commonJvmMain by creating {
103109
dependsOn(commonMain.get())
104110
dependencies {
105-
implementation(Config.Libs.sentryJava)
111+
api(Config.Libs.sentryJava)
106112
}
107113
}
108114

sentry-kotlin-multiplatform/sentry_kotlin_multiplatform.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@ Pod::Spec.new do |spec|
5050
}
5151
]
5252

53-
end
53+
end
Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,12 @@ import android.content.Context
66
import android.database.Cursor
77
import android.net.Uri
88
import io.sentry.android.core.SentryAndroid
9-
import io.sentry.kotlin.multiplatform.extensions.toAndroidSentryOptionsCallback
109

11-
internal actual fun initSentry(configuration: OptionsConfiguration) {
12-
val options = SentryOptions()
13-
configuration.invoke(options)
14-
15-
val context = applicationContext ?: run {
16-
// TODO: add logging later
17-
return
18-
}
19-
20-
SentryAndroid.init(context) { sentryOptions ->
21-
options.toAndroidSentryOptionsCallback().invoke(sentryOptions)
22-
}
23-
}
10+
public actual typealias Context = Context
2411

2512
internal var applicationContext: Context? = null
2613
private set
2714

28-
public actual typealias Context = Context
29-
3015
/**
3116
* A ContentProvider that does NOT store or provide any data for read or write operations.
3217
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.sentry.kotlin.multiplatform
2+
3+
import io.sentry.android.core.SentryAndroid
4+
5+
internal actual class SentryPlatformInstance : SentryInstance {
6+
override fun init(configuration: PlatformOptionsConfiguration) {
7+
val context = applicationContext ?: run {
8+
// TODO: add logging later
9+
return
10+
}
11+
12+
SentryAndroid.init(context, configuration)
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.sentry.kotlin.multiplatform
2+
3+
import io.sentry.kotlin.multiplatform.extensions.toAndroidSentryOptionsCallback
4+
5+
public actual typealias SentryPlatformOptions = io.sentry.android.core.SentryAndroidOptions
6+
7+
internal actual fun SentryPlatformOptions.prepareForInit() {
8+
sdkVersion?.name = BuildKonfig.SENTRY_KMP_ANDROID_SDK_NAME
9+
sdkVersion?.version = BuildKonfig.VERSION_NAME
10+
if (sdkVersion?.packageSet?.none { it.name == BuildKonfig.SENTRY_ANDROID_PACKAGE_NAME } == true) {
11+
sdkVersion?.addPackage(BuildKonfig.SENTRY_ANDROID_PACKAGE_NAME, BuildKonfig.SENTRY_ANDROID_VERSION)
12+
}
13+
}
14+
15+
internal actual fun SentryOptions.toPlatformOptionsConfiguration(): PlatformOptionsConfiguration =
16+
toAndroidSentryOptionsCallback()
17+
18+
internal actual fun SentryPlatformOptions.prepareForInitBridge() {
19+
prepareForInit()
20+
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.sentry.kotlin.multiplatform.extensions
22

33
import io.sentry.android.core.SentryAndroidOptions
4-
import io.sentry.kotlin.multiplatform.BuildKonfig
54
import io.sentry.kotlin.multiplatform.SentryOptions
65
import kotlin.collections.forEach as kForEach
76

@@ -15,16 +14,9 @@ internal fun SentryOptions.toAndroidSentryOptionsCallback(): (SentryAndroidOptio
1514
it.isAnrEnabled = this.isAnrEnabled
1615
it.anrTimeoutIntervalMillis = this.anrTimeoutIntervalMillis
1716

18-
it.sdkVersion?.name = this.sdk?.name ?: BuildKonfig.SENTRY_KMP_ANDROID_SDK_NAME
19-
it.sdkVersion?.version = this.sdk?.version ?: BuildKonfig.VERSION_NAME
20-
2117
// kForEach solves an issue with linter where it thinks forEach is the Java version
2218
// see here: https://stackoverflow.com/questions/44751469/kotlin-extension-functions-suddenly-require-api-level-24/68897591#68897591
2319
this.sdk?.packages?.kForEach { sdkPackage ->
2420
it.sdkVersion?.addPackage(sdkPackage.name, sdkPackage.version)
2521
}
26-
27-
if (it.sdkVersion?.packages?.none { it.name == BuildKonfig.SENTRY_ANDROID_PACKAGE_NAME } == true) {
28-
it.sdkVersion?.addPackage(BuildKonfig.SENTRY_ANDROID_PACKAGE_NAME, BuildKonfig.SENTRY_ANDROID_VERSION)
29-
}
3022
}

0 commit comments

Comments
 (0)