Question: Using `composeResources` with Kotlin 2.0...
# multiplatform
u
Question: Using
composeResources
with Kotlin 2.0 etc. and it is working great so far (android + iOS targets) However, I would like to be able to apply separate android and iOS variants of a drawable resource and use that within commonMain. Example:
Copy code
// Android-styled back icon
// androidMain/composeResources/drawable/ic_arrow_back.xml 

// iOS-styled back icon
// iosMain/composeResources/drawable/ic_arrow_back.xml
..

// Access drawable inside commonMain @Composable
Res.drawable.ic_arrow_back
This obviously doesn’t work, since commonMain doesn’t have access to the platform-specific drawables (or the generated extension properties on
Res
). Is it possible to apply some form of the
expect
/
actual
mechanism for resources? Or some other pattern/workaround to make this happen? I would like to avoid the platform conditionals in commonMain.
k
If you wanted to share icons of android+ iOS between shared UI then you need to use the expect and actual machnisam. Moreover, if you don't want to get the icons from other modules then just do one thing, create actual/ expect to check the platforms like if it's android then display the icons from composeResources and same for the iOS too. In this scenario, place all the icons inside the composeResources similar to dark_theme_android.png or dark_theme_ios.png and display respectively by checking platforms name like android & iOS
u
But I’m looking for an expect/actual solution using
Res
so I don’t have to do anything other than define the 2 (actual) variant drawable resources in androidMain/composeResources and iosMain/composeResources and then just reference the (expect) Res.drawable.ic_arrow_back in commonMain.
k
You can also use that as well.
m
Write a expect/actual fun to which is capable of getting and returning the data in the ByteArray form(in case of images) so that you can use them in commonMain.
k
Copy code
internal expect val Res.drawable.ic_platform: DrawableResource