Hi All, I have written an Android library in Kotlin which calls the C++ library which makes all the ...
n
Hi All, I have written an Android library in Kotlin which calls the C++ library which makes all the network calls. I have build MyLibraryLib as JNI wrapper and exposed them as suspend function and Added another wrapper MyLibraryClient on top of this JNI to expose as normal function. Inside which I am calling these suspend functions in GlobalScope.launch/returnBlocking so that MyLibraryClient can be used by Java code. What I understood is GlobalScope.launch/runBlocking is not supposed to be used for this purpose. My objective is to write a library which will take care of running the CPU or IO operation on the IO thread and return the results back on the main thread so that the Java/Kotlin caller need not worry about the threading and simply call my library function. e.g. MyLibraryLib looks something like this suspend fun getProfile(): Response{ returnWithContext(Dispatchers.IO){ val response = ProfileResponse() getProfile(response) // It’s an JNI function return@withContext response } } MyLibraryClient looks something like this fun getProfile(): Response{ return runBlocking{ val response = async{ MyLibraryLib.getProfile() } return@runBlocking response.await() } }
n
Thank you let me check how to do this, appreciate if you can share any examples
n
Thank you @baxter
1
I am also thinking of more strategic way to deal with this. Like Room library or other I can expose my library with built-in kotlin flow support and optional rxJava support. So that Java client will still be able to use my library with rxjava. But now sure how to achieve this. Have you ever developed any library with such support
p
Nope, solo kotlin these days
n
OK got it have posted the same query on the channel, will see if someone can help me on this otherwise will go with adding Rxjava wrapper around the calls