Hello everybody, I am a student who is writing he...
# multiplatform
a
Hello everybody, I am a student who is writing her bachelor thesis about Koltin Multiplatform Mobile. When creating a prototype I wanted to create a test file in the common main, read it and delete it again. For this I wanted to use the Multiplatfrom libary Okio (https://square.github.io/okio/). Unfortunately I can't access the libary in the common main. With the example:
val readmeContent = FileSystem.SYSTEM.read(path) {
readUtf8()
}
I can't find a SYSTEM for example. If I use the libary in the android folder I have full access to the libary. Does this mean that Okio cannot be used in the common main? I could really use your help.
1
a
okio can be used in commonMain, its just that some targets (i.e. jsBrowser) don't have a FileSystem.SYSTEM to begin with So how do you approach this kind of problem?? Since FileSystem is an abstract class with different implementations depending on the platfrom, Make your multiplatform code expect a FileSystem object to be injected. And then select a FileSystem depending on where you want to run your code i.e. if your running in Jvm, you can use FileSystem.JvmSystemFileSystem or FileSystem.NioSystemFileSystem which can be automatically selected with just FileSystem.SYSTEM if your running in Node, you can use NodeJsFileSystem, and I haven't tried this in darwin, but I am sure there is an equivalent file system that can be used there too
a
Thank you for @andylamax your quick reply🙏 . I found a page where you can find dependencies for the different filesystems. https://mvnrepository.com/artifact/com.squareup.okio
m
Actually it is quite simple but a bit confusing to find out via the documentation. Gradle source set commonMain
Copy code
implementation("com.squareup.okio:okio:3.3.0")
In commonMain
Copy code
expect val fs: FileSystem
In androidMain and desktopMain ...
Copy code
actual val fs: FileSystem = FileSystem.SYSTEM
FileSystem.SYSTEM is only defined for each specific platform. Therefore you have to use expect/actual to make it visible in common too.
a
Thank your for your reply @Michael Paus And in iosMain 🙂? I tested :
Copy code
implementation("com.squareup.okio:okio-iossimulatorarm64:3.3.0")
implementation("com.squareup.okio:okio-iosarm64:3.3.0")
implementation("com.squareup.okio:okio-iosx64:3.3.0")
m
For iOS it is the same. I only have one single dependency in Gradle for commonMain but I am not sure whether other dependencies are pulled in by the KTOR client. It works for me so I did not further investigate this.
a
Many thanks 🙂 I have an idea now!
l
If you're not using jsBrowser at all, you can use FileSystem.SYSTEM in commonMain, but the IDE marks it as an error. I usually just put it in a separate file and ignore that the file shows red. It will work when you compile.
I think okio outputs metadata in a weird way, since it should be available in commonMain if you're only using targets it supports (and in fact it does), but the IDE doesn't seem to know this.
j
It might compile, but you could end up with an error during
compileKotlinMetadata
when publishing. https://youtrack.jetbrains.com/issue/KT-53434 Unless this has been fixed? That'd be great if it has been!
l