Hi everyone! :wave: I’m looking for a good KMP lib...
# multiplatform
d
Hi everyone! 👋 I’m looking for a good KMP library for reading the application configurations (yaml, properties, env etc) , I want to use it from the shared module, and the configs to be accessible in ios, android, server targets. I’m not sure how KMP works with resources yet, but I think it should be possible. Any suggestions are welcome, many thanks!
m
I just read all my configurations from a JSON file which I import via Kotlins serialization. So, I do not even need any 3rd party library for that.
👍🏻 1
d
@Michael Paus I will try this solutions, however I come from Java&Spring Boot world, they use .yaml and .properties often there, never thought about json, I will check it, thanks.
b
you can use kotlinx serialization for YAML also with https://github.com/charleskorn/kaml
👍🏻 1
don't know if there's one for properties files though
b
@jw isn't the built-in properties serializer just for doing conversion with a key-value structure, and not a serialized (on-disk) format though ?
j
Oh, weird. Why do they call it a format then. I've linked it multiple times not realizing...
b
Maybe someone wrote a serializer for it, wouldn't be too hard anyway, but I couldn't find it
Seems like it's tracked as an issue here https://github.com/Kotlin/kotlinx.serialization/issues/1152
d
Btw, I just realised that parsing ≠ reading, you can’t just write in the shared module something like:
Copy code
Toml.loadFromFile(path = “resources/file.toml”)
As I see, the library makers provide just the parser, but I need the full support for seamless usage, I need somehow to get the string from the file located in “./shared/commonMain/resources/application.toml/yaml/json/etc” first. To be honest at this point it’s a lot easier to write a Kotlin Class/Object directly in the shared module and hardcode all the config fields there, Just wanted to know how the KMP community handles this case Chat Gpt suggested to impl the reading of the file from each platform via expect/actual and use the parser in the shared code, but it’s getting too verbose, maybe there is already a starter mobile pack for boilerplate or something similar?
b
For file handling, you can use Okio or
<http://kotlinx.io|kotlinx.io>
, and then it's quite easy to plug with the serialization
m
What could be easier than:
Copy code
val configString = Res.readBytes("files/config.json").decodeToString()
val config: Config = jsonFormat.decodeFromString(configString)
You could even reduce that to a single line if you want to. And the file has to reside in:
Copy code
[…]/src/commonMain/composeResources/files/config.json
You do not even need Okio or anything else for the IO part. This assumes of course that you can use Compose resources. If that is not the case you have to otherwise get the config string.
d
@bishiboosh, @Michael Paus I was thinking yesterday about both approaches, the "Res" solution seems quite clean, do I need special dependencies/configs in gradle for it to work in the shared module ? Also I suppose "Res" works not only in Composable context
I find it a bad design to create a new Folder "composeResources" - It's not used only in compose, it's a Multiplatform thing, I want to use same config file for: server, mobile apps, libs etc
m
You have to follow this documentation: https://www.jetbrains.com/help/kotlin-multiplatform-dev/compose-multiplatform-resources.html The resources work in common code. You can use Res everywhere. You just have to be aware of the fact that Res.readBytes() is a suspend function.
👍🏻 1
I agree that I would also prefer a general KMP resource mechanism.
b
for json, bridges to okio and/or kotlinx.io already exist. You're free to contribute to the other projects to implement the same kind of things
m
Which bridges do you mean?
d
@bishiboosh Maybe when I get more experience I will start to contribute, for now I just gain information
👍🏻 1
b
kotlinx-serialization-json-okio
exists for Okio (I thought there was one for kotlinx.io but I'm mistaken), and it's quite easy to do the same for some other formats (at least I know I've done in my projects)
🙏 1
d
Is there / will be there, a way to move away from xml on android (and CMP) ? Even properties files are better and less verbose
image.png
j
The verbosity is low and "better" is subjective
I would probably put a "better" format last on my list of concerns
👍🏻 1
d
Why it should be last ? In general or only for Android/CMP ? Maybe I'm just used to yaml and properties from Spring Boot because I work mostly with it, now when I want to start something in KMP/CMP, I get the vibes from 2014-2016 before Jetpack Compose appeared on Android