https://kotlinlang.org logo
Title
g

Glen

09/24/2020, 1:12 PM
Hello everyone. A friend asked me if it is possible to build a library, based on coroutines, that interops with native code, targeted for Android. Please, any ideas?(or whether it is possible)
l

louiscad

09/24/2020, 2:27 PM
If native code is code using the Android SDK (not the NDK), then sure, people have been doing it since 2017 with success.
g

Glen

09/24/2020, 2:49 PM
Please, @louiscad, can you give me some examples of such libraries?
When I say native code, I mean C code... Targeting Android.
l

louiscad

09/24/2020, 2:55 PM
So NDK… Then you'll need to do the callbacks wrapping and cancellation handling yourself…
g

Glen

09/24/2020, 3:00 PM
OK I see... Thanks for the info👍
I was just wondering if there were any libraries that are carrying out something similar, @louiscad.
l

louiscad

09/24/2020, 3:07 PM
Not that I'm aware of
g

Glen

09/24/2020, 3:10 PM
OK...
l

louiscad

09/24/2020, 3:11 PM
Now, Android Studio recent versions added autocompletion for JNI interop in Kotlin/C, so that might be helpful
g

Glen

09/24/2020, 3:23 PM
OK. Noted. So I will have to write wrappers for coroutines in the form if callbacks...Serious lot of work...
Thanks for your help, @louiscad.
I wanted to use IntelliJ IDEA to create the library...
l

louiscad

09/24/2020, 3:43 PM
Android Studio is free and is almost the same.
g

gildor

09/25/2020, 5:42 AM
So I will have to write wrappers for coroutines in the form if callbacks...Serious lot of work...
Some C++ Future/Promice class can be exposed instead of callback, so it would be enough to write one single await() function for it, so it will be universal for all async calls to native code But in general it really depends what kind C code you try to integrate
I doubt that there is any library, there are too many different C APIs, and no single= abstraction for async operations
g

Glen

09/25/2020, 8:47 AM
But is it possible that I use Kotlin/Native and generate a library that I can then consume on the Android side?
Any thoughts, @louiscad or @andreasmattsson?
l

louiscad

09/25/2020, 8:57 AM
Yes, but there's no java-like interop between Kotlin/Native and Kotlin/JVM, so you'll need glue code/an adapter. Also, kotlinx.coroutines is not available on the Android NDK (https://github.com/Kotlin/kotlinx.coroutines/issues/812), so you'll need to compile it yourself.
g

gildor

09/25/2020, 9:11 AM
I really don’t think that you need Kotlin/Native if your final goal is to use some C API on Android/JVM, K/N is just one more layer which doesn’t improve anything Better to have JNI directly to C API from JVM (or any of options which generates JNI glue for you) And on top of this Java JNI API you can add coroutines
g

Glen

09/25/2020, 9:16 AM
Thanks @gildor. But I have two questions. The first is, why can't I use K/N to avoid JNI altogether? and, How can I add coroutines on top of the JNI API?, because my goal is to use coroutines natively.
But from what @louiscad said above, it seems that it is not possible for the moment.
g

gildor

09/25/2020, 9:39 AM
The first is, why can’t I use K/N to avoid JNI altogether
You can, but then you have to write JNI to talk with K/N from Android It maybe a viable option if most of loigic on k/n side and you have simple interaction with it
How can I add coroutines on top of the JNI API?,
JNI api is just Java (or Kotlin JVM) code, no issue to write coroutines adapter on top of it
g

Glen

09/25/2020, 9:51 AM
I am working on an image processing library, where most of the specific image processing code is already written in C. I want to use coroutines when calling specific functions from the C API, then generate the code I have written into library that I can then use on the Android side.
With all of this, is K/N still a viable option, @gildor?
g

gildor

09/25/2020, 10:01 AM
I don’t think so honestly
if most of code already written in C
why not just “specific functions from the C API” do in JNI and wrap those JNI calls to coroutines
g

Glen

09/25/2020, 10:04 AM
Yeah, seems like a more pragmatic solution.
I wanted to create Kotlin bindings for the C code, generate a KLib, and use that on the Android side, @gildor.
g

gildor

09/25/2020, 10:10 AM
if Android wouldn’t be involved, if fine solution, but writing one more layer of bindings on top of it looks as not the best idea
I would like to have direct interop from JVM to C using the same cinterop mechanism + jni, but there is no such thing (though, K/N itself uses something similar under the hood, because compiler is JVM program)
g

Glen

09/25/2020, 10:14 AM
Yeah... Thanks a lot for your help, @louiscad, and @gildor. I really appreciate it. 🙏
I will just write a bunch of
external fun
methods and wrap them with coroutines.
g

gildor

09/25/2020, 10:24 AM
you also can use something like JNA to wrap C API without writing all glue manually
g

Glen

09/25/2020, 10:27 AM
Please @gildor, do you have any links or hints on how to achieve that? It will be of great help.
g

gildor

09/25/2020, 10:56 AM
I meant that you can use something like JNA https://github.com/java-native-access/jna or similar approaches to generate jni
g

Glen

09/25/2020, 11:00 AM
Ok. Thanks very much...🙏
But @gildor,I can use JNA directly with Kotlin, right?
g

gildor

09/25/2020, 2:30 PM
Yeah, JNA works with standard interfaes, shouldn't be an issue
g

Glen

09/26/2020, 12:50 PM
Thanks very much🙏
Hi there. I found this interesting library . Maybe I could use it as an alternative...🤷‍♂️