我想使用像getTimeMillis()这样的系统函数,它应该是kotlin.system的一部分:https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.system/index.html
但是编译器说这样的模块不能导入。gradle配置如下(kotlin多平台项目):
commonMain.dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-common:1.3.10"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.10.0"
implementation "io.ktor:ktor-client:1.0.0"
implementation "io.ktor:ktor-client-logging:1.1.0"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.1.0"
}此外,我找不到任何用法或此模块的示例。
发布于 2019-04-07 21:33:48
getTimeMillis()仅适用于JVM和Native,不适用于Common和JS。
如果您只在Native模块的源目录中调用getTimeMillis(),编译器就可以找到该函数。
如果需要在Common中调用,则必须自己实现一个Common包装器函数,并在每个平台上自己实现包装器。
为此,创建一个stub函数和一个在您的公共模块中使用它的函数。例如:
expect fun getSystemTimeInMillis(): Long
fun printSystemTimeMillis() {
println("System time in millis: ${getSystemTimeInMillis()}")
}然后实现你的平台特定模块的函数。例如,在JVM模块中:
actual fun getSystemTimeInMillis() = System.currentTimeMillis()或者在本机模块中,如:
actual fun getSystemTimeInMillis() = getTimeMillis()另请参阅:https://github.com/eggeral/kotlin-native-system-package
https://stackoverflow.com/questions/55455127
复制相似问题