Resource management library for Compose Multiplatform applications providing type-safe access to images, strings, fonts, and drawable assets across all platforms.
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Compose Resources provides comprehensive resource management for Compose Multiplatform applications. It enables type-safe access to images, strings, fonts, and drawable assets across Android, Desktop (JVM), iOS, Web (JS/Wasm), and Native (macOS) platforms with automatic locale selection, density-aware scaling, and environment-specific resource loading.
build.gradle.kts:
implementation("org.jetbrains.compose.components:components-resources:1.8.2")import org.jetbrains.compose.resources.*
import androidx.compose.ui.unit.Densityimport androidx.compose.foundation.Image
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.text.font.Font
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import org.jetbrains.compose.resources.*
@Composable
fun MyApp() {
// Load string resources
val title = stringResource(Res.string.app_title)
val formattedMessage = stringResource(Res.string.welcome_message, "User")
// Load image resources
val icon = painterResource(Res.drawable.app_icon)
val bitmap = imageResource(Res.drawable.profile_image)
// Load font resources
val customFont = Font(
resource = Res.font.custom_font,
weight = FontWeight.Bold
)
// Use resources in composables
Text(text = title, fontFamily = FontFamily(customFont))
Image(painter = icon, contentDescription = "App Icon")
}
// Non-composable usage
suspend fun loadResources() {
val message = getString(Res.string.app_title)
val imageBytes = getDrawableResourceBytes(
getSystemResourceEnvironment(),
Res.drawable.app_icon
)
}The library is built around several key concepts:
Provides type-safe string loading with formatting support and localization.
@Composable
fun stringResource(resource: StringResource): String
@Composable
fun stringResource(resource: StringResource, vararg formatArgs: Any): String
suspend fun getString(resource: StringResource): String
suspend fun getString(resource: StringResource, vararg formatArgs: Any): String
suspend fun getString(environment: ResourceEnvironment, resource: StringResource): StringHandles images, vector graphics, and SVG files with density-aware loading and automatic format detection.
@Composable
fun painterResource(resource: DrawableResource): Painter
@Composable
fun imageResource(resource: DrawableResource): ImageBitmap
@Composable
fun vectorResource(resource: DrawableResource): ImageVector
suspend fun getDrawableResourceBytes(environment: ResourceEnvironment, resource: DrawableResource): ByteArrayExtension functions for decoding raw image bytes into Compose graphics objects.
/**
* Decodes a byte array of a Bitmap to an ImageBitmap. Supports JPEG, PNG, BMP, WEBP
* Different platforms can support additional formats.
*/
fun ByteArray.decodeToImageBitmap(): ImageBitmap
/**
* Decodes a byte array of a vector XML file to an ImageVector.
* @param density density to apply during converting the source units to the ImageVector units.
*/
fun ByteArray.decodeToImageVector(density: Density): ImageVectorProvides font loading with support for variable fonts, weights, and styles.
@Composable
expect fun Font(
resource: FontResource,
weight: FontWeight = FontWeight.Normal,
style: FontStyle = FontStyle.Normal,
variationSettings: FontVariation.Settings = FontVariation.Settings(weight, style)
): Font
suspend fun getFontResourceBytes(environment: ResourceEnvironment, resource: FontResource): ByteArrayHandles quantity-based pluralized strings following CLDR plural rules.
@Composable
fun pluralStringResource(resource: PluralStringResource, quantity: Int): String
@Composable
fun pluralStringResource(resource: PluralStringResource, quantity: Int, vararg formatArgs: Any): String
suspend fun getPluralString(resource: PluralStringResource, quantity: Int): String
suspend fun getPluralString(resource: PluralStringResource, quantity: Int, vararg formatArgs: Any): StringProvides access to arrays of localized strings.
@Composable
fun stringArrayResource(resource: StringArrayResource): List<String>
suspend fun getStringArray(resource: StringArrayResource): List<String>
suspend fun getStringArray(environment: ResourceEnvironment, resource: StringArrayResource): List<String>Manages the environment context for resource selection including locale, theme, and density.
@Composable
fun rememberResourceEnvironment(): ResourceEnvironment
fun getSystemResourceEnvironment(): ResourceEnvironmentsealed class Resource(
internal val id: String,
internal val items: Set<ResourceItem>
)
data class ResourceItem(
internal val qualifiers: Set<Qualifier>,
internal val path: String,
internal val offset: Long,
internal val size: Long
)
class StringResource(id: String, val key: String, items: Set<ResourceItem>) : Resource(id, items)
class DrawableResource(id: String, items: Set<ResourceItem>) : Resource(id, items)
class FontResource(id: String, items: Set<ResourceItem>) : Resource(id, items)
class PluralStringResource(id: String, val key: String, items: Set<ResourceItem>) : Resource(id, items)
class StringArrayResource(id: String, val key: String, items: Set<ResourceItem>) : Resource(id, items)
class ResourceEnvironment(
internal val language: LanguageQualifier,
internal val region: RegionQualifier,
internal val theme: ThemeQualifier,
internal val density: DensityQualifier
)interface Qualifier
class LanguageQualifier(val language: String) : Qualifier
class RegionQualifier(val region: String) : Qualifier
enum class ThemeQualifier : Qualifier {
LIGHT, DARK;
companion object {
fun selectByValue(isDark: Boolean): ThemeQualifier
}
}
enum class DensityQualifier(val dpi: Int) : Qualifier {
LDPI(120), MDPI(160), HDPI(240), XHDPI(320), XXHDPI(480), XXXHDPI(640);
companion object {
fun selectByValue(dpi: Int): DensityQualifier
fun selectByDensity(density: Float): DensityQualifier
}
}@RequiresOptIn("This API is experimental and is likely to change in the future.")
annotation class ExperimentalResourceApi
@RequiresOptIn("This is internal API of the Compose gradle plugin.")
annotation class InternalResourceApiclass MissingResourceException(path: String) : Exception("Missing resource with path: $path")
class MalformedXMLException(message: String?) : Exception(message)