Search code examples
kotlinresourceskotlin-multiplatformkotlin-native

How to bundle and access resources in Kotlin Multiplatform for native targets like linuxX64?


I want to bundle a file along with my Kotlin Multiplatform library. The library supports both JVM and linuxX64 targets.

On the JVM, I am able to access a bundled file using MyClass::class.java.getResourceAsStream() and friends.

But on linuxX64, how can we do this? We just need to get a path to the bundled resource. After that, opening a file and reading it should be easy.

  • Is bundling of resources supported at all?
  • If it is supported, how do we get the path to a bundled resource file?

Compose Multiplatform seems to have some solutions for bundling resources, as described here: https://github.com/JetBrains/compose-jb/blob/master/tutorials/Native_distributions_and_local_execution/README.md

But

  • this would work only for apps, not libraries
  • it is for apps that use Compose

I am also aware of projects such as moko-resource, but it also doesn't have a solution for native libraries. It only supports apps on iOS, Android and Web.

Thanks.


Solution

  • Unfortunately KMM doesn't provide solution for this right now (and probably won't provide in the nearest future), it requires to create some kind of Resource Manager for Kotlin/Native. It is big architecture decision and looks like it is not priority right now. You can track progress for this in Kotlin's Youtrack.

    As workaround you can create task that adds resources to .klib in Gradle build of your library (.klib already has special resources folder that you could use for this, it is empty for non-JVM targets by default), but your library consumers should somehow pack those resources with the final executable (see example).