带有本地库的多平台库

Multiplatform library with native library

我正在尝试为 kotlin 创建一个多平台库,我想用一个原生的 cinterop(ted) 库来完成它。

我正在将它导入 gradle,如果我将 commonMain 更改为 Native,它会加载,库会加载,否则它只是红色。

此时我正在尝试做的事情是否可行?我们可以将本机(cinteropted)库导入通用 kotlin 吗?

我在底部添加我的 gradle 文件 (Kotlin DSL)

plugins {
    id("org.jetbrains.kotlin.multiplatform").version("1.3.41")
    id("maven-publish")
}
repositories {
    mavenCentral()
}
group = "xyz.mglolenstine"
version = "0.0.1"

kotlin {
    jvm()
    js {
        browser {
        }
        nodejs {
        }
    }
    // For ARM, should be changed to iosArm32 or iosArm64
    // For Linux, should be changed to e.g. linuxX64
    // For MacOS, should be changed to e.g. macosX64
    // For Windows, should be changed to e.g. mingwX64
    linuxX64("linux")
    val commonMain by sourceSets.getting {
        dependencies {
            implementation(kotlin("stdlib-common"))
            implementation(fileTree("libs"))
        }
    }
    val commonTest by sourceSets.getting {
        dependencies {
            implementation(kotlin("test-common"))
            implementation(kotlin("test-annotations-common"))
            implementation(fileTree("libs"))
        }
    }
    val jvmMain by sourceSets.getting {
        dependencies {
            implementation(kotlin("stdlib-jdk8"))
            implementation(fileTree("libs"))
        }
    }
    val jvmTest by sourceSets.getting {
        dependencies {
            implementation(kotlin("test"))
            implementation(kotlin("test-junit"))
            implementation(fileTree("libs"))
        }
    }
    val jsMain by sourceSets.getting {
        dependencies {
            implementation(kotlin("stdlib-js"))
            implementation(fileTree("libs"))
        }
    }
    val jsTest by sourceSets.getting {
        dependencies {
            implementation(kotlin("test-js"))
            implementation(fileTree("libs"))
        }
    }
    val linuxMain by sourceSets.getting {
        dependencies {
            implementation(fileTree("libs"))
        }
    }
    val linuxTest by sourceSets.getting {
        dependencies {
            implementation(fileTree("libs"))
        }
    }
}

如果我可以做些什么来改进 gradle 文件,请告诉我,因为我对 Kotlin DSL 还是个新手,我的 TODO 正在改进中。

可以使用平台相关代码使用 expect/actual 机制为给定 class 创建不同的实现,其工作方式类似于接口:

expect class Foo {
    fun bar(): String
}

actual class WindowsFoo : Foo() {
    actual fun bar(): String {
        return "Windows platform calls go here"
    }
}

我想这是不可用的,因为 cinterop 工具只为外部库创建绑定,而不是表示。此外,klibs 是纯原生乐器,如您在 documentation.

中所见