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

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

我知道依赖于为某些 OS 源集安装 JVM 是一个很奇怪的用例,请允许我完成我的用例。

我正在编写一个简单的实用程序来包装对 steamCMD (https://developer.valvesoftware.com/wiki/SteamCMD) 的调用,它具有依赖于平台的安装过程。所以,我自然应该

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

另一方面,我的实用程序还需要处理文件存储(例如,下载并检查存储中是否存在客户端),所以我也可以使用文件 class.

// commonMain / File.kt
expect interface File

我知道 JB 团队对其教程有明确的推荐。

We recommend that you use expected and actual declarations only for Kotlin declarations that have platform-specific dependencies. It is better to implement as much functionality as possible in the shared module even if doing so takes more time.

然而,尽管有警告,我不希望编写 MyFile 实现,以节省为此类常见任务重新发明轮子的工作量,但 java.io.File 在场景中占据主导地位,以至于我在 Gradle / Maven 上找不到任何 Kotlin 替代品。

这是否意味着我最后不得不写MyFile?或者是否有将 Java 库导入 Kotlin MPP 平台源集的解决方法?

首先,Java 库只能用于 jvmandroid 目标,不能使用 Kotlin/Multiplatform 提供的其他目标。事实上,这正是使用 Kotlin/JVM 的目标子集。 Kotlin/JS 和 Kotlin/Native 都不提供与 Java 的互操作性,它们有自己的互操作能力。看到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,即使会显得更C-stylish.


P.S。通过网络快速搜索,我找到了这个社区图书馆 list, maybe it would help. Also, kotlinlang Slack community(find link here) 可能有一些有趣的解决方案可以分享。