Is it possible to package native libraries (.so li...
# multiplatform
f
Is it possible to package native libraries (.so lib) in a shared module? Where would be the jnilib folder in Android? And what about iOS?
s
m
Native libraries can be packaged as static (.a) files. Why do you need it as dynamic (.so) ?
m
you will have to setup this lib separately for Android and iOS, for Android you have to setup the lib with NDK (use jni and etc.), for iOS you will have the setup a the native lib using the c_interop approach (link above)
👍 1
f
@msink My understanding is that in Android I can't use static packaged libraries.
m
In Android you can load, via NDK: https://developer.android.com/ndk/guides/prebuilts
f
@magnumrocha I manage to use and call the library in an Android app. However, I don't find the right place where to put my library in androidMain or what to specify in the build file.
m
the default source folder for android gradle plugin is:
<module>/src/main
, to load a lib in side
androidMain
you will have to map the source folder to find
androidMain/lib/...
f
@magnumrocha How do I do that?
Something like
Copy code
sourceSets["androidMain"].dependencies {
    implementation(fileTree("jnilibs"))
and then put the files (with arch folders) in
androidMain/jnilibs
?
I tried various places without success: "Could not load zenroom native library: no zenroom in java.library.path"
as an alternative, you can create the path for the default source folder in your kmp module:
src/main/jni
f
When I try to put the srcDir in the kotlin.android configuration like this
Copy code
sourceSets["main"].jniLibs.srcDir = "androidMain/jniLibs"
it complains about unresolved reference jniLibs
I tried all these at the same time.
In
androidTest
I have a unit test that calls a method in androidMain that loads the library:
Copy code
System.loadLibrary("zenroom")
As I don't know how to do the mapping of jniLibs for the androidMain source set I try to find the default location. Without success so far.
m
try:
Copy code
sourceSets["androidMain"].jniLibs.srcDir = "src/androidMain/jniLibs"
f
@magnumrocha I meant the reference after sourceSets["androidMain"]
m
this is the wrong place to do that...
you have to make it inside the android gradle plugin configs
this is the kmp android target config
Copy code
android {  // -> android gradle plugin
    compileSdkVersion 29
    buildToolsVersion '29.0.3'
    ...
}

kotlin { // -> kmp plugin config
    android {
    }
}
f
Thank you for the clarification, the difference was not clear
It makes much more sense like that, but the library is still not found. I have created a branch of kampkit. The test still fails: https://github.com/friedger/KaMPKit/commit/19cd9f47e00c40cb38ea533fde124c04277b85cb
m
not proper a solution but, I would not put that inside the
androidMain/kotlin
folder but aside it
androidMain/jniLibs
f
@magnumrocha ok, I have moved the lib. Interestingly, loading the library in an app from androidMain works. So, there is an issue with finding it in a unit test. I have updated the app with a call to an expected
loadLibrary
method and the library is loaded: https://github.com/touchlab/KaMPKit/commit/ea5bbcdcff03bc8f0fcbd3581d171217c0445245
👍 1
101 Views