Hey everyone, I am still quite new to both Kotlin ...
# multiplatform
m
Hey everyone, I am still quite new to both Kotlin and KMM, so this may be really obvious, I apologize. I'm just starting to translate some code to KMM to share it between both Android and IOS. Currently I'm working in Android Studio on a Windows device. I created this class:
Copy code
class SharedDiscover {
    companion object {
        fun parse(deviceId: Int, data: ByteArray, buffer: ByteBuffer): Boolean  {
            // Some code...
        }
    }
}
However, it has an unsolved reference to ByteBuffer. This links back to
import java.nio.ByteBuffer
in the existing project, but in the shared folder I obviously cannot link to this, or at least not in this way. Does anyone have a workaround? Or any insight on how this works?
j
You’ll want a cross platform solution, and the de facto standard for that for KMP is https://square.github.io/okio/
I believe its Buffer implementation should work approximately the same and is available on all kotlin targets
(I’ve not touched Native much though so someone may correct me)
m
Okay thanks for your help!
k
The reason this happens is that the common kotlin has no reference to Java or JVM. You can use
java
classes in
androidMain
, but not in any other source set. So one approach would be to use the
expect/ actual
functionality. Then you can
expect
a
Buffer
class, and then on the
androidMain
side you can use the
actual
ByteBuffer
since Android uses
JVM
. Then on the
iOS
side you would use
NSData
or the swift alternative. You should be fine with
Okio
but that’s generally how
KMM
works.