Skip to content

Commit b0eb091

Browse files
authored
Don't throw an error if a context is null in the DefaultAndroidResourceReader (#5433)
It fixes Robolectric tests where a context is available through InstrumentationRegistry Fixes https://youtrack.jetbrains.com/issue/CMP-6612 ## Release Notes ### Fixes - Resources - Fix resource access in a Robolectric test environment.
1 parent f420cb8 commit b0eb091

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

components/resources/library/src/androidMain/kotlin/org/jetbrains/compose/resources/ResourceReader.android.kt

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,7 @@ internal actual fun getPlatformResourceReader(): ResourceReader = DefaultAndroid
1313

1414
@ExperimentalResourceApi
1515
internal object DefaultAndroidResourceReader : ResourceReader {
16-
private val assets: AssetManager by lazy {
17-
val context = androidContext ?: error(
18-
"Android context is not initialized. " +
19-
"If it happens in the Preview mode then call PreviewContextConfigurationEffect() function."
20-
)
21-
context.assets
22-
}
16+
private val assets: AssetManager? get() = androidContext?.assets
2317

2418
private val instrumentedAssets: AssetManager?
2519
get() = try {
@@ -69,7 +63,7 @@ internal object DefaultAndroidResourceReader : ResourceReader {
6963
Uri.parse("file:///android_asset/$path")
7064
} else {
7165
val classLoader = getClassLoader()
72-
val resource = classLoader.getResource(path) ?: throw MissingResourceException(path)
66+
val resource = classLoader.getResource(path) ?: throwMissingResourceException(path)
7367
resource.toURI()
7468
}
7569
return uri.toString()
@@ -83,11 +77,23 @@ internal object DefaultAndroidResourceReader : ResourceReader {
8377
instrumentedAssets.open(path)
8478
} catch (e: FileNotFoundException) {
8579
val classLoader = getClassLoader()
86-
classLoader.getResourceAsStream(path) ?: throw MissingResourceException(path)
80+
classLoader.getResourceAsStream(path) ?: throwMissingResourceException(path)
8781
}
8882
}
8983
}
9084

85+
private fun throwMissingResourceException(path: String): Nothing {
86+
if (androidContext == null) {
87+
throw MissingResourceException(
88+
path = path,
89+
message = "Android context is not initialized. " +
90+
"If it happens in the Preview mode then call PreviewContextConfigurationEffect() function."
91+
)
92+
} else {
93+
throw MissingResourceException(path)
94+
}
95+
}
96+
9197
private fun getClassLoader(): ClassLoader {
9298
return this.javaClass.classLoader ?: error("Cannot find class loader")
9399
}

components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/ResourceReader.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import androidx.compose.runtime.Composable
44
import androidx.compose.runtime.ProvidableCompositionLocal
55
import androidx.compose.runtime.staticCompositionLocalOf
66

7-
class MissingResourceException(path: String) : Exception("Missing resource with path: $path")
7+
class MissingResourceException(path: String) : Exception("Missing resource with path: $path") {
8+
internal constructor(path: String, message: String) : this("$path. $message")
9+
}
810

911
/**
1012
* Reads the content of the resource file at the specified path and returns it as a byte array.

0 commit comments

Comments
 (0)