Thread
#multiplatform
    f

    Friedger

    2 years ago
    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?
    saket

    saket

    2 years ago
    m

    msink

    2 years ago
    Native libraries can be packaged as static (.a) files. Why do you need it as dynamic (.so) ?
    m

    magnumrocha

    2 years ago
    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)
    f

    Friedger

    2 years ago
    @msink My understanding is that in Android I can't use static packaged libraries.
    m

    magnumrocha

    2 years ago
    In Android you can load, via NDK: https://developer.android.com/ndk/guides/prebuilts
    f

    Friedger

    2 years ago
    @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

    magnumrocha

    2 years ago
    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

    Friedger

    2 years ago
    @magnumrocha How do I do that?
    Something like
    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

    Friedger

    2 years ago
    When I try to put the srcDir in the kotlin.android configuration like this
    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:
    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

    magnumrocha

    2 years ago
    try:
    sourceSets["androidMain"].jniLibs.srcDir = "src/androidMain/jniLibs"
    f

    Friedger

    2 years ago
    @magnumrocha I meant the reference after sourceSets["androidMain"]
    m

    magnumrocha

    2 years ago
    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
    android {  // -> android gradle plugin
        compileSdkVersion 29
        buildToolsVersion '29.0.3'
        ...
    }
    
    kotlin { // -> kmp plugin config
        android {
        }
    }
    f

    Friedger

    2 years ago
    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

    magnumrocha

    2 years ago
    not proper a solution but, I would not put that inside the
    androidMain/kotlin
    folder but aside it
    androidMain/jniLibs
    f

    Friedger

    2 years ago
    @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