JNI C++ based

Hi! I’m currently working on Android, on a project that has a native C++ side, and I’m using Kotlin instead of Java. I’m binding the code with the external keyword and the usual Java style means. Based on my past experience in similar projects (where i used swig to bind Java and C++), it would be of great advantage if Kotlin would give a way of doing the binding for C++.

The Java way, has the advantage that is simple to build for the language developers, but it’s cumbersome especially for application developers that want to use it for binding C++ code.

What SWIG does is to build a twin object of a C++ object on the Java side. It builds also two inner layers (one on Java and the other on c++), because JNI was thought to work with C. This means that intermediate objects are necessary to link functions from Java to static functions on the C side.

would be cool if you could tag a kotlin class and have generated a twin class in c++ that you could use as if it was the original kotlin class, and ideally (in the dream world) tag a c++ class and have a correspondent kotlin class generated.

I know it’s probably crazy, but that would put Java JNI in the stone age.

This problem is a sore point (and not only for Android). Good bindings for C++ are hard to find and event for regular C JNI generation is a pain. SWIG is rather limited and is not properly maintained.

The good solution is to have those bindings for Kotlin-Native and then have seamless bridge between Kotlin-JVM and Kotlin-Native. In that case you can create a simple multiplatform project, where native module is a thin-layer binding for external native library. Unfortunately, this feature requires a lot of work.

Sorry, being complete newbie with kotlin i think i misinterpreted the native tag. As I understand native kotlin means that it’s compiled (a bit like c/c++). So my post would be more relative to Kotlin Language in general, should be moved under a more appropriate tag?

Kotlin-Native has almost seamless interop with the C API, but not with C++ so far. There is some movement in that direction. Sadly, C++ is quite unpleasant to interop with. But more complicated problem is interoping Kotlin-JVM with native binaries. JVM has different memory model and when you have different language as well, direct interop becomes real problem. What I was talking about is two-step interop, where you can make Kotlin-Native bindings for native binaries (Kotlin native allows low level memeory access so it is possible to make quite neat bindings) and then create automatic bridge between Kotlin-Native and Kotlin JVM since language is the same and it seems to be possible to create good quality automatic bindings.