I have trouble loading resources in my compose mul...
# compose-desktop
a
I have trouble loading resources in my compose multiplatform project. In an
icons
package I have added the resources under
commonMain/resources
. I added the experimental
compose.components.resources
dependency and I created a barebones composable in that package:
Copy code
@OptIn(ExperimentalResourceApi::class)
@Composable
fun Icon() {
    Image(
        painter = painterResource("icons/create-new-folder.svg"), // i also tried with leading slashe
        contentDescription = null
    )
}
However, if I consume this package in my desktop application package (compose desktop for JVM) it can not find the resource. What am I missing here? I used the following repo as a starting point: https://github.com/Kotlin/kotlin-wasm-examples/tree/main/compose-imageviewer
c
painterResource
is an android concept that not part of the
compose.components.resources
I think. have a look at the cutom implementation of
painterResourceCached
in here https://github.com/Kotlin/kotlin-wasm-examples/blob/d28f7917848c24e26d40f8ec6b309d[…]ed/src/commonMain/kotlin/example/imageviewer/platform.common.kt
also what do you mean with
icons
package
? resources are bundled via a resource folder like this https://github.com/Kotlin/kotlin-wasm-examples/tree/d28f7917848c24e26d40f8ec6b309dcd9ba0e77f/compose-imageviewer/shared/src/commonMain/resources
a
PainterResource is part of the compose Multiplatform api and not Android specific. The icons package is just a Gradle module I made with SVGs in the commonMain/resources folder
c
did you then try to move the
resource
folder to
jvmMain
?
a
No not yet, but that is also not what is being done in the compose Multiplatform examples
c
yes, but they are also not using the
painterResource
but load the resources from the
resource
composable function.
a
Ah you’re right! I’ll try that
Alas, this also doesn't work. I think I have to go to a minimal example and try again.
painterResource
is part of the
org.jetbrains.compose.resources
package btw
c
What error do you get?
a
Error(exception=org.jetbrains.compose.resources.MissingResourceException: Missing resource with path: icons/create-new-folder.svg)
so, that is clear
I'll go to a minimal example with a minimal new project and try again....
c
👍
a
Okay, SVG's aren't supported yet it seems. I am unable to load basic svg icons. Either it's a PNG or equivalent or an XML, which I don't know.
https://github.com/JetBrains/compose-multiplatform-template/blob/main/shared/src/commonMain/kotlin/App.kt This one works with an XML, but not with an SVG. I think I'll have to wait until icons are truly more stable for Multiplatform.
c
there is also this IDEA plugin to generate
Compose Image Vectors
,like the Material Icons library, out of your SVG’s https://plugins.jetbrains.com/plugin/18619-svg-to-compose
a
Minimal example is working. However in my own project I have a JVM app project (not Multiplatform) that includes Multiplatform subprojects with resources in them. I’ll try to figure out if that’s the problem
c
Did you “inspect” the
.jar
file if the resources are all bundled correctly?
a
I found the issue. • multiplatform library containing resources in commonMain/resources directory • kotlin("multiplatform") project with JVM target having this library as implementation dependency -> works • kotlin("jvm") project having this library as implementation dependency -> fails Issue has been filed: https://github.com/JetBrains/compose-multiplatform/issues/3162
I created a repo to reproduce the issue: https://github.com/avwie/resources-issues-compose