https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
f

Francis Mariano

03/04/2023, 3:32 PM
Hello I am trying to use
"org.jetbrains.compose.components:components-resources:1.3.1-rc01"
to load images images via compose in common module, but the images are not loaded. More info in the thread.
image.png
I added the file logo.jpg in resoures dir of commonMain.
So, in code I added :
Copy code
Image(
    bitmap = resource("logo.jpg").rememberImageBitmap().orEmpty(),
    contentDescription = null
)
Image(
    painter = painterResource("logo.jpg"),
    contentDescription = null
)
The first
Image()
is not load the image and the second
Image()
crash the app with the following message:
Caused by: org.jetbrains.compose.resources.MissingResourceException: Missing resource with path: logo.jpg
The resource is missing. What is the right path to put the resources???
j

Jeff Lockhart

03/04/2023, 4:38 PM
Unfortunately common resources aren't automatically added to binary compilations. I've had to use a combination of symlinking for Android and Gradle copy tasks for JVM and native targets to copy common resources to the required bundle or runtime paths.
f

Francis Mariano

03/06/2023, 3:44 PM
you can add the following instead of: android { sourceSets["main"].resources.srcDir("src/commonMain/resources") }
j

Jeff Lockhart

03/06/2023, 4:31 PM
When I tried adding the common resources directory as a resource directory for the other source sets, IntelliJ or Android Studio throws a "duplicate content roots detected" error and removes it from the other source sets. This solution might work from the terminal, but not in the IDE, which is why I resorted to symlinking instead, as this causes the directory to appear as a unique path. There are several YouTrack issues about this error, but unfortunately it's a behavior that's deeply relied on for IDE functionality.
f

Francis Mariano

03/06/2023, 4:42 PM
hmmmmm...here worked fine, but I have been tested just in android
j

Jeff Lockhart

03/06/2023, 4:44 PM
Android is your only source set?
f

Francis Mariano

03/06/2023, 4:44 PM
no, I have android and ios, but I tested just in android
sourceSets["main"].resources.srcDir("src/commonMain/resources")
if different of:
sourceSets["main"].res.srcDirs("src/androidMain/res", "src/commonMain/resources")
resources != res
so you need add the resources directory for the library work properly
j

Jeff Lockhart

03/06/2023, 4:56 PM
Maybe that one case might work because of the difference between resources and res in Android? In my case I ended up symlinking
src/commonMain/resources
to
src/androidMain/resources/assets
, as I consume the common resources as Android assets in my Android
actual
implementations.
910 Views