Skip to content

Commit 9c79652

Browse files
authored
Fix: don't crash app when applicationContext is not available (#217)
* Add fix * Update * Update CHANGELOG
1 parent 713d97c commit 9c79652

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ pod("Sentry") {
1414
}
1515
```
1616

17+
### Fixes
18+
19+
- Don't crash app when `applicationContext` is not available ([#217](https://github.com/getsentry/sentry-kotlin-multiplatform/pull/217))
20+
1721
### Enhancements
1822

1923
- Make `setSentryUnhandledExceptionHook` public ([#208](https://github.com/getsentry/sentry-kotlin-multiplatform/pull/208))

sentry-kotlin-multiplatform/src/androidMain/kotlin/io/sentry/kotlin/multiplatform/SentryInit.android.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,18 @@ import io.sentry.kotlin.multiplatform.extensions.toAndroidSentryOptionsCallback
1111
internal actual fun initSentry(configuration: OptionsConfiguration) {
1212
val options = SentryOptions()
1313
configuration.invoke(options)
14-
SentryAndroid.init(applicationContext, options.toAndroidSentryOptionsCallback())
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+
}
1523
}
1624

17-
internal lateinit var applicationContext: Context
25+
internal var applicationContext: Context? = null
1826
private set
1927

2028
public actual typealias Context = Context

sentry-kotlin-multiplatform/src/androidUnitTest/kotlin/io/sentry/kotlin/multiplatform/SentryContextProviderTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import org.junit.Before
77
import org.junit.Test
88
import org.junit.runner.RunWith
99
import org.mockito.Mockito.mock
10+
import kotlin.test.assertNotNull
1011

1112
@RunWith(AndroidJUnit4::class)
1213
class SentryContextProviderTest : BaseSentryTest() {
@@ -22,8 +23,8 @@ class SentryContextProviderTest : BaseSentryTest() {
2223
class SentryContextOnCreateTest : BaseSentryTest() {
2324
@Test
2425
fun `onCreate initializes applicationContext`() {
25-
// Simple call to the lateinit applicationContext to make sure it's initialized
26-
applicationContext
26+
// Simple call to the applicationContext to make sure it's initialized
27+
assertNotNull(applicationContext)
2728
}
2829
}
2930

0 commit comments

Comments
 (0)