How to use .klib library in Kotlin/Multiplatform project
Asked Answered
R

1

2

I want to use C code in my Kotlin/Multiplatform project (common level). By this article: https://theprogrammershangout.com/resources/kotlin/native/creating-c-bindings.md/ I got my .klib file and now I want to add it to my IntelliJ Kotlin/Multiplatform project. I'm trying to add this to Gradle without success:

implementation files("/path/to/App.klib")

The project is built successfully but the importing App is not resolved. What am I missing?

I already searched all over the internet and did not found anything helpful.

My build.gradle:

buildscript {
    repositories {
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.1'
    }
}

plugins {
    id 'org.jetbrains.kotlin.multiplatform' version '1.4.10'
    id 'maven-publish'
}

apply plugin: 'com.android.library'

repositories {
    mavenCentral()
    google()
    jcenter()
}

group 'com.texel.examples.kotlinnative'
version '0.0.1'

android {
    compileSdkVersion 28
    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName '1.0'
        testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
    }
    buildTypes {
        release {
            minifyEnabled false
        }
    }
    sourceSets {
        main {
            manifest.srcFile 'src/jvmMain/AndroidManifest.xml'
            java.srcDirs = ['src/jvmMain/kotlin']
            res.srcDirs = ['src/jvmMain/resources']
        }
    }
}

dependencies {
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
}


kotlin {
    // This is for iPhone emulator
    // Switch here to iosArm64 (or iosArm32) to build library for iPhone device
//    iosX64("ios") {
//        binaries {
//            framework()
//        }
//    }

    macosX64("maccos") {
        binaries {
            framework()
        }
    }

    android("android") {
        // you can also publish both "release" and "debug"
        publishLibraryVariants("release")
    }


    sourceSets {
        commonMain {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-stdlib-common:1.4.10'
                implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9-native-mt"
                implementation files("/Users/user/Downloads/App.klib")
            }
        }
        jvmMain {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
            }
        }
        jsMain {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-stdlib-js'
            }
        }
        macosMain {
            dependencies {
            }
        }
        iosMain {
            dependencies {
            }
        }
    }

}

configurations {
    compileClasspath
}

Many thanks.

Ringtail answered 18/10, 2020 at 6:49 Comment(1)
Hi Matan, have you got solution for above problem. I am facing same issue. Please let me know ur solutionParamaribo
B
1

Here is the official documentation: C Interop and also an Using C Interop and libcurl for an App (you may prefer to look at the https://github.com/JetBrains/kotlin-native/tree/master/samples/libcurl instead)

As I see the gradle setup is pretty different:

   hostTarget.apply {
    compilations["main"].cinterops {
        val libcurl by creating {
            when (preset) {
                presets["macosX64"] -> includeDirs.headerFilterOnly("/opt/local/include", "/usr/local/include")
                presets["linuxX64"] -> includeDirs.headerFilterOnly("/usr/include", "/usr/include/x86_64-linux-gnu")
                presets["mingwX64"] -> includeDirs.headerFilterOnly(mingwPath.resolve("include"))
            }
        }
    }
Bischoff answered 19/10, 2020 at 18:44 Comment(2)
Thanks for your comment. I am using Groovy and not Kotlin as my gradle. What the relevant syntax?Ringtail
I have just edited my answer with my build.gradle codeRingtail

© 2022 - 2024 — McMap. All rights reserved.