Interoperability between kotlin-native and kotlin-jvm

In one of my current projects I have to implement two-sided interaction between JVM and native code. Basically, I have to use JNI or JNA and it requires a lot of work. Now, we have Kotlin native which have some great tools for interop with native code, so it would seem reasonable to create a kotlin-native bridge which will work as a wrapper for native library. The problem is that there seem to be no way to directly interface kotlin-native code with kotlin-jvm without explicit use of JNI. Such interface probably could be generated automatically since both kotlin-native and kotlin-jvm share the same type system. Also it would be good to have such mechanism for multi-platform support, since the situation when part of the code is native and part of the code is JVM is quite frequent.

Are there any plans to create such an interface?

8 Likes

Try cinterop -flavor=jvm

Sorry, but I do not understand, what should I try. I have not found any documentation for the option you mentioned.

While searching, I found another similar question on this forum.

There is no documentation - it used internally in K/N compiler for building stubs to interact with LLVM.
Compiler is in Kotlin/JVM, LLVM is native. Exactly what you want, if I understood correctly.

Indeed. But it will be very hard to do anything without at least a bit documentation. I will try to look through the sources. Thanks for suggestion.

Any news here? I have tried to modify the compiler to pass the flavor jvm in but I can’t make any progress…

What’s about using kotlin-native just to optimize Kotlin execution speed?
This would mean that the kotlin-native code still uses the kotlin-jvm stdlib.

At this time (and probably for quite a while into the future), HotSpot is far better at optimizing Kotlin execution speed than Kotlin/Native. Also, Kotlin/Native does not support compiling Java bytecode to native code, which would be necessary to use the kotlin-jvm stdlib.

@yole I meant that just the Kotlin code is compiled and then loaded via JNI into a standard VM.
It would still use the Java standard library of that VM.

1 Like

Calling all JVM APIs through JNI is definitely going to be much, much, much slower than running Kotlin-compiled code under HotSpot (which can inline calls to JVM APIs and perform lots and lots of optimizations on the resulting code).

The problem is not the speed, at least my problem is not about it, but interoperability with native libraries. Usually, connecting native libraries to java requires a lot of painful work with something like SWIG or JNA. Direct interface with JNI is even harder.
Kotlin native seems to provide solution since it allows to create easy interface with native libraries and then creating automatic interop between kotlin native and kotlin jvm seems much easier since the language model is the same.
I do not know about other fields, but for scientific and engineering applications, it would be tremendous achievement. It would allow to easily write top level applications in Kotlin/JVM and forget about C++.

3 Likes

Dear all,

I’m looking into the same problem as @darksnake - binding a C library to Kotlin on the JVM.
So far, I was going to delve into JNI or SWIG, but the JVM version of K/N Interop seems more attractive.
As @msink has mentioned this is used inside K/N, but there is no documentation (I just saw this project use it GitHub - msink/kotlin-ui and give some indications)…

My question: is this something the K/N developers just needed for themselves and want to keep hidden, or will it be officially supported one day as part of K/N?

Best regards,
Vito

1 Like

This is very interesting, although if I understood properly K/N would allow to create bindings only for C (and not C++)… am I right?

For now C/Objective-C/Swift interop is working. C++ does not, but nobody said it won’t in future. As far as I know people managed to create bindings for C++ programs which work fine. It requires some work though.

1 Like

I imagine myself writing a multiplatform project where one module has a native-only implementation, and the main, JVM module is able to use it through JNI.

Everything could be wired automatically by Gradle for example, allowing one to mix JVM and native code with a pretty consistent user experience.

Even though some people need to go down to C, others could just use a transparent Kotlin JVM+Native bridge, and avoid many complications like multiple build systems and languages, manual JNI wiring.

7 Likes

Exactly my use case. In my project I have the main side on jvm mixed with groovy and Java. However I wrote some kotlin code to do blurring on images and it takes a considerable amount of time, like 7 seconds per image, usually on 20 images. I’d expect that a c implementation of that blurr to be much faster, and I’d expect kotlin native to be as fast as c. Plus, I don’t enjoy writing c anymore, and jni even less.

Pending benchmarks on kotlin native, those should come in soon.

1 Like

The implementation in kotlin-native will be much slower. It is not made for performance. At least, not for now. Basically, a good jvm implementation should be almost as fast as possible. If you need something faster, you won’t get it in C since writing really fast code in C takes a lot of experience. You can use existing native libraries though.

1 Like

Is there any news here? I don’t really care about speed, I’m just searching for an easy way to call a C library from Kotlin/JVM, and doing it via Kotlin/Native sounds like it would make sense.

2 Likes

The bridge is not there yet. And there is no roadmap for it either.

FWIW my YouTrack request looks related to this discussion: https://youtrack.jetbrains.com/issue/KT-39144