https://kotlinlang.org logo
Title
a

Alexander Stark

04/09/2018, 10:45 AM
Hi, is it possible to consume a Kotlin/Native klib in a Kotlin module?
o

olonho

04/09/2018, 11:44 AM
It depends on what Kotlin module your’re talking about. In Kotlin/Native module it’s pretty straightforward, use
-l mine.klib
or
libraries
DSL verb in Gradle, in Kotlin/JVM it is way harder, you need to produce dynamic library with
-p dynamic
and manually convert object arguments. For Kotlin/JS it is never tried, but likely using N-API (https://nodejs.org/api/addons.html) it could be done in the way similar to Kotlin/JVM.
But please note, that for MPP expected scenario is to compile same common source with both Native and JVM/JS backends, see https://github.com/JetBrains/kotlin-native/pull/1474
a

Alexander Stark

04/09/2018, 1:04 PM
As mentioned. I have a kotlin/native module which is used to integrate libcurl precombiled lib. This module should then be used in a common pure kotlin module to do networking
does this make sense at all?
o

olonho

04/09/2018, 1:21 PM
nope, it doesn’t work that way, instead you could create HTTP module as an expect class and provide different implementations on different platforms (i.e.
java.net.HttpURLConnection
on Java,
NSURLSession
on iOS, libcurl on Linux)
a

Alexander Stark

04/09/2018, 1:24 PM
that’s exactly what i want to avoid
i want to share as much as possible that’s why my plan was to use libcurl as platform independent solution
This is a snippet from my build.gradle file of the kotlin/native module: konanArtifacts { interop(‘libcurl’) { target(‘android_arm32’) { includeDirs “../external/curl/android/include” } target(‘android_arm64’) { includeDirs “../external/curl/android/include” } } library(‘kurl’) { libraries { artifact ‘libcurl’ } } }
it created the kurl.klib
and now I want to use this lib in my pure kotlin module (no native)
Or is it possible to create a jar?
s

Sam

04/09/2018, 2:07 PM
If you want to call a C library from Kotlin/JVM you need to use JNI and doing that is complicated and has extra overhead. The jvm is relatively fast these days so the practice of replacing slow code with native code like they do in Ruby or Python isn't as necessary.
o

olonho

04/09/2018, 2:08 PM
although support for Native is not yet there
g

gildor

04/09/2018, 2:21 PM
I want to use this lib in my pure kotlin module
Common module cannot work without platform, on compile time common module just compiled to target platform code, so only platform independent code can work everywhere (the code that you can write in common module). libcure is obviously native only dependency. You just cannot implement http client completely module independent, you need some platfrom specific part that work at least with sockets (but usually you want to use some more high level abstraction). Of course you can compile libcurl and use it on iOS and Android as native dependency, but you need also JNI for JVM and ObjC glue code for Swift to use native lib, so I don’t think this is good solution, actually almost nobody uses such approach for pure Android/iOS apps without big native part But I agree, that everyone needs multiplatform Kotlin HTTP client library for multiplatfrom client and would be nice to have one, hope ktor http-client will be released as separate http library for multiplatform kotlin.