#kotlin #bindings-generator #bindgen

bin+lib uniffi_bindgen_kotlin_multiplatform

为 Rust 编写的 Kotlin 多平台绑定生成器(代码生成和 CLI 工具)

1 个不稳定版本

0.1.0 2023年11月25日

#165FFI

MPL-2.0 许可证

145KB
2.5K SLoC

Kotlin 2K SLoC // 0.1% comments Rust 1K SLoC // 0.1% comments Jinja2 66 SLoC // 0.2% comments

UniFFI Kotlin Multiplatform 绑定

该项目包含用于 UniFFI 的 Kotlin Multiplatform 绑定生成。

目前仅支持 JVM 和 Native 目标。JS 支持将是极佳的,但需要在 uniffi 中支持 WASM。

您可以在 tests 目录中找到如何使用绑定的示例。

如何使用

由于 bindgen crate 和 Gradle 插件尚未发布,您需要进行一些额外的 Gradle 配置。(见下文 本地使用

使用 Gradle 插件

plugins {
    kotlin("multiplatform")
    id("io.gitlab.trixnity.uniffi.kotlin.multiplatform") version "0.1.0"
}

uniffi {
    // The directory containing the Rust crate.
    crateDirectory = layout.projectDirectory.dir("rust")
    
    // The name of the crate as in Cargo.toml's package.name.
    crateName = "my_crate"
    
    // The name of the library as in Cargo.toml's library.name. Defaults to "${crateName}".
    libraryName = "my_crate"
    
    // The UDL file. Defaults to "${crateDirectory}/src/${crateName}.udl".
    udlFile = layout.projectDirectory.file("rust/src/my_crate.udl")
    
    // The UDL namespace as in the UDL file. Defaults to "${crateName}".
    namespace = "my_crate"
    
    // The profile to build the crate. Defaults to "debug".
    profile = "debug"
}

kotlin {
    // The plugin will react to the targets definition
    jvm()
    linuxX64()
}

直接使用 bindgen CLI

待办事项

本地使用

由于 bindgen crate 和 Gradle 插件尚未发布,您需要进行一些额外的 Gradle 配置。

在本地仓库中发布 Gradle 插件

克隆仓库并构建它。在 build-logic/gradle-plugin/build.gradle.kts 的末尾添加发布仓库定义

publishing {
    repositories {
        maven {
            name = "local"
            url = uri("<path-to-local-plugin-repository>")
        }
    }
}

然后调用 ./gradlew :build-logic:gradle-plugin:publishAllPublicationsToLocalRepository

指定本地仓库

在您的 settings.gradle.kts 中添加本地仓库

pluginManagement {
    repositories {
        maven {
            name = "local"
            url = uri("<path-to-local-plugin-repository>")
        }
        // ...
    }
}

最后,使用此仓库中 bindgen 的确切路径配置 uniffi 扩展。

uniffi {
    // ...
    bindgenCratePath = "<path-to-our-bindgen>"
}

依赖关系

~7MB
~133K SLoC