Hello. I have recently bought a book on NDK and I ...
# kotlin-native
v
Hello. I have recently bought a book on NDK and I found that most of it teaches you to use JNI with your Java/ Kotlin code. I wonder if a better approach would be to use Kotlin native
g
Not really, JNI (manually written or generated) is the only way
v
Now let's say that the project is not Android, is it still better to use JNI?
g
how JNI related to Android?
JNI is just a way to interact between JVM and Native libraries
v
The Android NDK book basically teaches how how to use JNI in your Android project. This is the book https://www.amazon.com/Android-Native-Development-Kit-Cookbook-ebook/dp/B00BAOC2JW?tag=duckduckgo-d-20
g
Sure, you can use JNI on Android, but I don’t understand you question
JNI needed only to use some native code from Java/Kotlin, same for Android or pure Java/Kotlin-JVM project
v
My question is: What option is better for interoperability between Kotlin and C/C++ JNI or Kotlin Native?
g
Mostly the same
but tooling for generation of JNI for C/C++ is just better (it exists)
if you write JNI manually not a big difference, you still use C headers generated by K/N to interop with C
v
Ok. thanks a lot
n
As Andrey has mentioned Kotlin Native doesn't have a way to expose APIs which are JVM compatible. If Kotlin Native had this system then JNI wouldn't be required.
A much better system would be one where there is bi-directional interop between Kotlin Native and Kotlin JVM/Android (might also include Kotlin JS).
g
Yes, but it's not a thing that can be easily implemented, a couple weeks ago it was discussed and K/N team don't see simple way to implement it, but still you can share your use case with them
n
Is there a existing issue in the Issue Tracker covering this problem (no standard way of doing bi-directional interop between different Kotlin platforms - Native/JVM/JS/Android)?
g
None that I know of
n
Was thinking along the lines of what is done in JSerialComm ( https://fazecast.github.io/jSerialComm/ ) by general idea (low level and high level sides with a interop system in between). That is the low level side (library) is developed using Kotlin Native, and the high level part (program/library) is developed using Kotlin JS, Native, or Android. In between there is a interop system that exposes certain APIs that have been marked for export on the low level side, which are accessible on the high level side with memory/resources automatically managed by the low level side.
g
That is the low level side (library) is developed using Kotlin Native
What is the point of this? If code is already platform agnostic, you can just run it directly on JS/JVM using MPP, it’s exactly case of MPP
🤔 1
@napperley I just don’t understand why to use Kotlin/Native library/code on a platfrom that already supported by MPP (JVM/Android, JS)