跪拜 Guibai
← All articles
Android

Compose Multiplatform Shared Resources: The Traps That Sabotage Your Release

By 黄林晴 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Any team shipping a KMP app with shared UI needs to know where composeResources diverges from Android's resource system, because a single SVG or locale request without a fallback can break a release across targets.

Summary

Compose Multiplatform lets a shared UI module load images, fonts, and strings from commonMain/composeResources, with the Gradle plugin generating a type-safe Res class. The resource library handles language, dark mode, and density via directory qualifiers, so a single composable can adapt across platforms without explicit checks. Most of the setup is straightforward, but several edge cases turn into release-blocking failures.

The generated accessors are extension properties, so importing Res alone does not resolve concrete resources like Res.drawable.brand_logo; each specific import must be present. The painterResource and Font functions come from org.jetbrains.compose.resources, not the Android-only package, and Font is composable. For localization, keeping a bare values-zh directory is mandatory when supporting both Simplified and Traditional Chinese, because Android and desktop sometimes request only zh, leaving only zh-Hans and zh-Hant as ambiguous matches that throw an exception.

Platform gaps also matter. SVG images are not supported on Android, and vector XML copied from Android projects may reference @color/ or ?attr/ values that the shared resource library cannot resolve. The files directory supports no qualifiers and no type-safe accessors; use Res.readBytes for small data or Res.getUri for media. On Web, painterResource returns an empty Painter on first composition, so reserving image dimensions prevents layout jumps. Dark mode and locale switching require platform-level environment changes; modifying MaterialTheme alone does not alter the resource environment.

Takeaways
— Only importing Res is not enough; each concrete accessor like brand_logo must be imported separately because generated code uses extension properties.
— painterResource and Font from org.jetbrains.compose.resources differ from Android Compose versions, so migration code must verify the correct package.
— The resource library does not support SVG on Android; use PNG/WebP or vector XML without external resource references.
— For Simplified and Traditional Chinese, keep a values-zh fallback because Android and desktop may request bare zh; having only zh-Hans and zh-Hant causes ambiguous matching and an exception.
— The files directory does not support qualifiers and yields no type-safe accessors; use Res.readBytes for small data or Res.getUri for media.
— On Web, painterResource returns an empty Painter on the first frame; reserving image dimensions avoids layout jumps and false missing-image detection.
— Changing MaterialTheme colors does not switch the resource environment; dark mode and locale handling remain platform-level concerns.
Conclusions

The resource library's qualifier handling is a strict subset of Android's, so teams that copy Android res layouts will hit silent mismatches until runtime.

The mandatory zh fallback exposes a naive script-matching model, where multi-script locales need an explicit default path to avoid ambiguous resolution.

The lack of SVG support on Android is a practical reminder that Compose Multiplatform is not a drop-in replacement for Android's resource system.

Resource environment changes stay platform-specific, so shared UI code cannot assume that theme state alone propagates to resource selection.

Concepts & terms
composeResources
A shared resource directory in Compose Multiplatform's commonMain that holds images, fonts, and strings, with a Gradle plugin generating a type-safe Res class.
Resource qualifiers
Directory suffixes like -dark, -xhdpi, or -zh that select resources based on environment; Compose Multiplatform supports a limited subset of Android's qualifiers.
BCP 47 script subtags
Language tags such as zh-Hans and zh-Hant that distinguish writing scripts; Compose Multiplatform uses plus-separated forms like b+zh+Hans.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗