简体   繁体   English

为 mingw / ios / linus / 其他源集导入 java lib 的解决方法?

[英]Workarounds to import java lib for mingw / ios / linus / other source sets?

I am aware that it's quite a weird use case to depend on having JVM installed for some OS source sets, allow me to go through my use case.我知道依赖于为某些操作系统源集安装 JVM 是一个非常奇怪的用例,请允许我完成我的用例。

I'm writing a simple utility to wrap calls for the steamCMD ( https://developer.valvesoftware.com/wiki/SteamCMD ), which has platform dependent installation procedures.我正在编写一个简单的实用程序来包装 steamCMD ( https://developer.valvesoftware.com/wiki/SteamCMD ) 的调用,它具有平台相关的安装过程。 So, naturally I should have所以,我自然应该有

// commonMain / steamCmdGetter.kt
expect interface SteamCmdGetter {
    fun installClient()
}
// [OS] / steamCmdGetter.kt
actual interface SteamCmdGetter { /* ... */ }

On the other hand, my utility also needs to do work with the file storage (for example, downloading and checking client existence in storage), so I could also use a file class.另一方面,我的实用程序还需要处理文件存储(例如,下载和检查存储中的客户端是否存在),因此我也可以使用文件类。

// commonMain / File.kt
expect interface File

I am aware that the JB team has an explicit recommendation on its tutorials.我知道 JB 团队对其教程有明确的建议。

We recommend that you use expected and actual declarations only for Kotlin declarations that have platform-specific dependencies.我们建议您仅对具有平台特定依赖项的 Kotlin 声明使用预期和实际声明。 It is better to implement as much functionality as possible in the shared module even if doing so takes more time.最好在共享模块中实现尽可能多的功能,即使这样做需要更多时间。

Yet, against the warnings I wish not to write a MyFile implementation to save efforts from reinventing the wheel for such a common task, but java.io.File has been so dominant in the scene that I could not find any Kotlin alternatives on Gradle / Maven.然而,针对警告,我不希望编写MyFile实现以节省为此类常见任务重新发明轮子的工作,但是java.io.File在场景中占据主导地位,以至于我在 Gradle / 上找不到任何 Kotlin 替代品马文。

Does this means I am forced to write MyFile in the end?这是否意味着我最终被迫写了MyFile Or is there a workaround for importing Java libraries to Kotlin MPP platform sourceSets?或者是否有将 Java 库导入 Kotlin MPP 平台 sourceSets 的解决方法?

First of all, one can use Java libraries only for jvm and android targets, not the others provided by the Kotlin/Multiplatform.首先,只能将 Java 库用于jvmandroid目标,而不能使用 Kotlin/Multiplatform 提供的其他库。 In fact, this is exactly a targets subset that is using Kotlin/JVM.事实上,这正是使用 Kotlin/JVM 的目标子集。 Neither Kotlin/JS nor Kotlin/Native provide interoperability with Java, they has their own interop capabilities. Kotlin/JS 和 Kotlin/Native 都不提供与 Java 的互操作性,它们有自己的互操作能力。 See this page to get some details on the difference.请参阅此页面以获取有关差异的一些详细信息。
About working with files in particular.特别是关于处理文件。 Most probably the answer is yes and you'll have to implement it per-target.答案很可能是肯定的,您必须针对每个目标实施它。 This kind of work is usually platform-specific, as it hardly rely on the OS implementation.这种工作通常是特定于平台的,因为它几乎不依赖于操作系统的实现。 However, part of the functionality you search for should be definitely found in the platform.posix.*platform library , even if it would appear more C-stylish.但是,您搜索的部分功能绝对应该在platform.posix.*platform library 中找到,即使它看起来更 C 风格。


PS Quick search across the Web led me to this community libraries list , maybe it would help. PS 在网上快速搜索让我找到了这个社区图书馆 列表,也许它会有所帮助。 Also, kotlinlang Slack community(find link here ) may have some interesting solutions to share.此外,kotlinlang Slack 社区(在这里找到链接)可能有一些有趣的解决方案可以分享。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM