Hello everyone. A friend asked me if it is possibl...
# coroutines
g
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
If native code is code using the Android SDK (not the NDK), then sure, people have been doing it since 2017 with success.
g
Please, @louiscad, can you give me some examples of such libraries?
When I say native code, I mean C code... Targeting Android.
l
So NDK… Then you'll need to do the callbacks wrapping and cancellation handling yourself…
g
OK I see... Thanks for the info👍
I was just wondering if there were any libraries that are carrying out something similar, @louiscad.
l
Not that I'm aware of
g
OK...
l
Now, Android Studio recent versions added autocompletion for JNI interop in Kotlin/C, so that might be helpful
g
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
Android Studio is free and is almost the same.
g
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
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
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
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
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
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
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
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
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
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
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
you also can use something like JNA to wrap C API without writing all glue manually
g
Please @gildor, do you have any links or hints on how to achieve that? It will be of great help.
g
I meant that you can use something like JNA https://github.com/java-native-access/jna or similar approaches to generate jni
g
Ok. Thanks very much...🙏
But @gildor,I can use JNA directly with Kotlin, right?
g
Yeah, JNA works with standard interfaes, shouldn't be an issue
g
Thanks very much🙏
Hi there. I found this interesting library . Maybe I could use it as an alternative...🤷‍♂️