Posix on Android

Hi

I’m new to Kotlin Native so please excuse any beginners mistakes. I have tried looking around in the discussions and documentation but can’t find an answer to my question. Please feel free to direct me to an already existing answer if there is one.

I’m investigating Kotlin Native as a possible platform for writing the majority of the data and logic layers for an app intended for both iOS and Android. UI will be written natively. I have started experimenting with a multiplatform library and have successfully built for both platforms, and integrated into test apps.

Now to my question. I’m thinking of doing some file operations and thought of using POSIX (I haven’t managed to find any IO libs for Native. Feel free to suggest…) The documentation states

For all Unix or Windows based targets (including Android and iPhone ) we provide the posix platform lib. … To use the library just
import platform.posix.*

If I do this, I can successfully compile for iOS, but the JVM build fails with Unresolved reference: platform. How would I go about using POSIX on Android?

You must not compile form JVM, you must compile to Android NDK.

I see. That would mean accessing my library through the JNI. I was hoping to be able to write as much common code as possible, without having to implement glue code on the app side for either platform.

I guess the alternative then is to define expect functions or classes in the common code where I can’t find a common cross platform denominator, with platform specific implementations in the jvm and iOS platform modules. Unless those specialisms remain few I suspect Kotlin/Native may not be the tool I had hoped it would be… :man_shrugging:

If you want to do that, write in Kotlin and compile to JVM. As IO library there is kotlinx-io. JNI tends to create a large overhead and you don’t really want to use it when not needed. Alternatively you create a wrapper that uses platform.posix on a native target (as Android is linux derived and thus implements posix) and java (N)IO on Java target.

kotlinx-io looks interesting! I’ll have a look. Thank you for the link.