CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-jetbrains-compose-components--components-resources

Resource management library for Compose Multiplatform applications providing type-safe access to images, strings, fonts, and drawable assets across all platforms.

Pending
Overview
Eval results
Files

Compose Resources

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.

Package Information

  • Package Name: org.jetbrains.compose.components:components-resources
  • Package Type: maven
  • Language: Kotlin Multiplatform
  • Version: 1.8.2
  • Installation: Add to build.gradle.kts:
    implementation("org.jetbrains.compose.components:components-resources:1.8.2")

Core Imports

import org.jetbrains.compose.resources.*
import androidx.compose.ui.unit.Density

Basic Usage

import 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
    )
}

Architecture

The library is built around several key concepts:

  • Resource Types: Strongly-typed resource classes (StringResource, DrawableResource, FontResource, etc.)
  • Environment-Aware Loading: Automatic selection based on locale, theme, and screen density
  • Cross-Platform Abstraction: Single API with platform-specific implementations
  • Async Loading: Non-blocking resource loading with intelligent caching
  • Compose Integration: Deep integration with Compose runtime and state management

Capabilities

String Resources

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): String

String Resources

Image and Drawable Resources

Handles 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): ByteArray

Image and Drawable Resources

Image Decoder Extensions

Extension 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): ImageVector

Font Resources

Provides 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): ByteArray

Font Resources

Plural String Resources

Handles 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): String

Plural String Resources

String Array Resources

Provides 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>

String Array Resources

Resource Environment and Context

Manages the environment context for resource selection including locale, theme, and density.

@Composable
fun rememberResourceEnvironment(): ResourceEnvironment

fun getSystemResourceEnvironment(): ResourceEnvironment

Resource Environment

Core Types

sealed 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
)

Qualifiers

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
    }
}

Annotations

@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 InternalResourceApi

Exceptions

class MissingResourceException(path: String) : Exception("Missing resource with path: $path")

class MalformedXMLException(message: String?) : Exception(message)

Install with Tessl CLI

npx tessl i tessl/maven-org-jetbrains-compose-components--components-resources
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.jetbrains.compose.components/components-resources@1.8.x