⭐ Contribute!
⭐ Contribute!
  • Home
  • 🚀 Let's start
  • Configure KMP
  • User interface
  • Navigation
  • Ressources
  • Architecture
  • Connectivity
  • Preferences
  • (Local Database)

Ressources

  • For common code, store your resource files in the resources directory of the commonMain source set.
  • For platform-specific code, store your resource files in the resources directory of the corresponding source set.

Jetbrain release his experimental API painterResource from org.jetbrains.compose.resource package

@Composable
public fun painterResource(
    res: String
): Painter
  • Return a Painter from the given resource path. Can load either a BitmapPainter for rasterized images (.png, .jpg) or a VectorPainter for XML Vector Drawables (.xml).
  • XML Vector Drawables have the same format as for Android (https://developer.android.com/reference/android/graphics/drawable/VectorDrawable) except that external references to Android resources are not supported.
  • Note that XML Vector Drawables are not supported for Web target currently.

Images

Declare you ressource management library

build.gradle

        commonMain.dependencies {
            ...
            // enable resources multiplatform dependancy
            implementation(compose.components.resources)
            ...
        }

Add your image files

You can add it in the composeResources/drawable directory.

Usage

WelcomeScreen.kt

import quiz.composeapp.generated.resources.Res
...

Image(
                    modifier = Modifier.size(70.dp),
                    painter = painterResource(Res.drawable.question),
                    contentDescription = null
                )

Fonts and String

For more ressource management possibilities for font and String management, you can use a third party lib :

  • Moko
  • Libres

Strings

composeResources/values

<resources>
    <string name="app_name">Hello World App</string>
</resources>
Text(stringResource(Res.string.app_name))

Fonts

Put your font files in the composeResources/font directory.

Then you can charge your your font like this :

val fontAwesome = FontFamily(Font(Res.font.my_font))

Other ressources

  LaunchedEffect(Unit) { 
    bytes = Res.readBytes("files/myDir/someFile.ext")
  }

✅ If everything is fine, congrats, you've just finish this codelab. You can now experiment your kotlin skills eveywhere !

📖 Further reading

  • Access and usage of KMP ressources
Edit this page
Last Updated:
Contributors: Brah, Ibrahim Gharbi
Prev
Navigation
Next
Architecture