kotlin跨贴:添加kotlin/本机作为普通代码

发布于 2025-02-11 05:36:29 字数 331 浏览 1 评论 0原文

我想创建一个与Android和桌面兼容的Kotlin项目。该项目需要绑定到C库。

我的理解方式,我应该创建一个Kotlin多平台项目,并具有使用JNI包装C库的常见代码。

但是,Kotlin/Anitial允许与C库的互动更轻松,因此我想使用它。但是似乎Kotlin/Antial是一个平台(例如JVM或Android),因此不能用作常见代码。

有办法做我想做的事吗?我找不到任何简单的例子。

如果不可能,为什么? Kotlin/本机能够瞄准桌面和Android平台。如果可以在Android上使用Kotlin/本机,为什么不可能在“普通”桌面/Android项目中使用Kotlin/本机库?

I want to create a Kotlin project compatible with Android and Desktop. This project needs to bind to a C library.

The way I understand it, I should create a Kotlin multiplatform project, and have a common code which wraps the C library using JNI.

However, Kotlin/Native allows a way easier interop with C libraries, so I'd like to use that. But it seems like Kotlin/Native is a platform (equal to eg jvm or android), so it can't be used as a common code.

Is there a way to do what I want? I couldn't find any simple example doing that.

If that's not possible, why? Kotlin/Native is able to target desktop and android platforms. If it's possible to use Kotlin/Native on Android, why is it impossible to use a Kotlin/Native library from a "normal" desktop/android project?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

歌枕肩 2025-02-18 05:36:29

通过具有本机目标,Kotlin Multiplatform应该可以使用。

参见 htttps:// kotlinlang .org/docs/atnation-app-with-c and-curl.html#create-a-a-definition-file

kotlin/本机帮助消耗标准的C库,打开整个功能的生态系统,这些功能几乎可以使用您可能需要的任何东西。 Kotlin/本地已经运送了一组预制的平台库,这些库为标准库提供了一些其他共同的功能。

更新:

kotlin Multiplatform”下的“常见”代码。
a)具有平台 - 不合稳定的kotlin代码或
b)使用期望/实际来定义平台抽象
(afaik)

由于不是c库a),因此您必须定义实际的平台定义,最终以JNI结束。

tl; dr kmp不适合您要做的事情

This should be possible with Kotlin Multiplatform, by having a native target.

See https://kotlinlang.org/docs/native-app-with-c-and-libcurl.html#create-a-definition-file

Kotlin/Native helps consume standard C libraries, opening up an entire ecosystem of functionality that exists for pretty much anything you may need. Kotlin/Native is already shipped with a set of prebuilt platform libraries, which provide some additional common functionality to the standard library.

UPDATE:

"common" code in Kotlin Multiplatform under the hood either:
a) has platform-agnostic Kotlin code or
b) uses expect/actual to define platform abstractions
(AFAIK)

Since a C library isn't a), you'll have to define the actual platform definitions, ending up with JNI.

TL;DR KMP isn't suitable for what you're trying to do

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文