How to load resources in new MPP

#1

Hello

I try to load resources in a new MPP like SampleMain::class.java.getResource("/sample/config.properties") without success. When i looking in the build folder, the resources are placed in the build/processedResources folder. But when i start an application with an IDEA run configuration this folder will not get applied to the classpath / classloader. The only workaround i see at the moment is to copy those resources with an custom Gradle copy taks to copy them into the build/classes/kotlin/jvm/main folder and execute this task before main start. Or do i anything wrong?

Regards

2 Likes

#2

Having the same issue here.

0 Likes

#3

If MPP is “multiplatform projects prototype” then it suppose to support JS target, right ?
In JS it is hard and invalid to perform synchronous blocking resource read, since all data fetch in JS is async.

Or am I miss something ?

0 Likes

#4

Same issue as well.

0 Likes

#5

Well, it does not have to be blocking. MPP probably needs some kind of internal way to load resources. For example using coroutines. It does not have to be done on a language level and could be implemented by libraries, but it requires some kind of support from the build system, meaning that resources must be loaded into the webpack bundle on JS.

0 Likes

#6

Well, the thread starter initially requested it to be blocking.
If async is OK, then it should fit in classical multiplatform paradigm: mpp interface and platform implementations: for web, for jvm/android, and for iOS.

So the thread starter can

  • declare multiplatform (async) resource loading interface
  • implement it on required platforms.
  • package the resources in platform-specific way.
0 Likes

#7

The problem as I see it is not in code, but in packaging and tooling. In JVM the content of resource directory is automatically included in jar. On JS, the resource should be somehow added to webpack build. On native, resources should probably come in a separate directory. Then there could be a platform specific resource getter that could load it from default directory for this platform. It should not be very hard to implement, but requires modifications to the gradle mpp plugin.

0 Likes

#8

Well, if the problem is in the tool-chain and plugin, then it might be possible to file an YT issue describing the problem ?

0 Likes

#9

Like this one: https://youtrack.jetbrains.com/issue/KT-29311? I have not found a universal proposal for common way to load resources. Probably I should write it later.

0 Likes

#10

I hope https://github.com/orangy/kotlinx-files will support cross-platform read-only Resources filesystem.

0 Likes

#11

Sorry for my very late answer!

I think, my description of the problem was not clear enough when i read it now.

I think it’s neither related to JS / shared code or async / sync. It looks to me more like an IDE / classpath / build problem. I attached a very simple MPP project with just a properties file and a main class. If i execute the main function via IDEA run configuration, the resource will not be found.

mpp.zip (78.2 KB)

0 Likes