https://kotlinlang.org logo
Title
a

Arjan van Wieringen

05/12/2023, 12:55 PM
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:
@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

Chrimaeon

05/12/2023, 1:25 PM
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

Arjan van Wieringen

05/12/2023, 2:27 PM
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

Chrimaeon

05/12/2023, 2:35 PM
did you then try to move the
resource
folder to
jvmMain
?
a

Arjan van Wieringen

05/12/2023, 3:32 PM
No not yet, but that is also not what is being done in the compose Multiplatform examples
c

Chrimaeon

05/12/2023, 3:33 PM
yes, but they are also not using the
painterResource
but load the resources from the
resource
composable function.
a

Arjan van Wieringen

05/12/2023, 3:33 PM
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

Chrimaeon

05/12/2023, 3:59 PM
What error do you get?
a

Arjan van Wieringen

05/12/2023, 4:00 PM
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

Chrimaeon

05/12/2023, 4:00 PM
👍
a

Arjan van Wieringen

05/12/2023, 5:25 PM
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

Chrimaeon

05/12/2023, 5:32 PM
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

Arjan van Wieringen

05/13/2023, 5:40 AM
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

Chrimaeon

05/13/2023, 9:16 AM
Did you “inspect” the
.jar
file if the resources are all bundled correctly?
a

Arjan van Wieringen

05/13/2023, 9:54 AM
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