kotlin跨贴:添加kotlin/本机作为普通代码
我想创建一个与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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过具有本机目标,Kotlin Multiplatform应该可以使用。
参见 htttps:// kotlinlang .org/docs/atnation-app-with-c and-curl.html#create-a-a-definition-file
更新:“
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
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