JetBrains / compose-multiplatform

Compose Multiplatform, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.
https://jetbrains.com/lp/compose-multiplatform
Apache License 2.0
16.23k stars 1.18k forks source link

Resources (files) are not packed in SPM for native targets #4569

Closed checoalejandro closed 4 months ago

checoalejandro commented 7 months ago

Describe the bug When using compose-resources in a multiplatform project for iOS and Android and exported as an SPM with multiplatform-swiftpackage, when attempting to use resources in /files directory, the following error is thrown:

Cause: org.jetbrains.compose.resources.MissingResourceException: Missing resource with path: files/demo/majel.html

This error is not happening when using library as a pod.

Affected platforms

Versions

To Reproduce Steps and/or the code snippet to reproduce the behavior:

  1. Setup compose resources in a kotlin multiplatform library for Android and iOS.
  2. Import multiplatform-swiftpackage gradle plugin to create SPM with createSwiftPackage task. This gets generated in swiftpackage directory.
  3. After the package is created, import to an iOS application project in XCode, go to target dependencies and add local SPM.
  4. Run project and use a section that requires any resource and crash will be thrown.

Expected behavior SPM must contain the resources in Package.swift file and inside the XCFramework, resources directory must be created and included files.

Additional context Is there any workaround to force the inclusion of these resources manually? This looks to me more of a dependency on multiplatform-swiftpackage but need details on what to do to make it included.

paxbun commented 7 months ago

As stated in the Docs, multimodule resources are not supported yet. You can also check that in the actual code, which always references NSBundle.mainBundle, so it means the current Gradle plugin implementation copies all resources to the app directly, no matter the Kotlin library is built as a static library or a dynamic library.

https://github.com/JetBrains/compose-multiplatform/blob/04edeed084d9882d3a28950a11e722a3eb2860c7/components/resources/library/src/iosMain/kotlin/org/jetbrains/compose/resources/ResourceReader.ios.kt#L12-L15

In theory, static frameworks can't have resources (they're not represented as an NSBundle), so you have to export your library as a dynamic framework.

terrakok commented 7 months ago

Compose gradle plugin doesn't support publication native frameworks as SPM or other packages. @paxbun is absolutely right. you may get resources manually from the build and do what you want, as workaround now

checoalejandro commented 7 months ago

I am able to get the bundle of resources when integrating mocko-resources along with its compose implementation. I will continue testing and if it works as expected, I will detail the process to make this XCFramework to contain resources.

vcoolish commented 5 months ago

I am able to get the bundle of resources when integrating mocko-resources along with its compose implementation. I will continue testing and if it works as expected, I will detail the process to make this XCFramework to contain resources.

Hi! could you please share some details?

terrakok commented 4 months ago

Duplicates https://github.com/JetBrains/compose-multiplatform/issues/4643

okushnikov commented 3 months ago

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.