How to use .klib library in Kotlin/Multiplatform project

1.3k views Asked by At

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.

1

There are 1 answers

2
Róbert Nagy On

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"))
            }
        }
    }