【问题标题】:Kotlin/Native can't import io.ktor.network.selector.ActorSelectorManagerKotlin/Native 无法导入 io.ktor.network.selector.ActorSelectorManager
【发布时间】:2021-07-16 23:41:20
【问题描述】:

我想使用 KTOR 在 Kotlin/Native 中使用 TCP 套接字,我遵循了 tutorial。但不知何故,我无法从io.ktor.network.selector 导入ActorSelectorManager 并得到Unresolved reference: ActorSelectorManager

我的build.gradle.kts 看起来像这样:

plugins {
    kotlin("multiplatform") version "1.4.32"
    kotlin("plugin.serialization") version "1.4.32"
}
repositories {
    mavenCentral()
    jcenter()
    maven { url = uri("https://plugins.gradle.org/m2/") }
    maven { url = uri("http://dl.bintray.com/kotlin/kotlin-dev") }
    maven { url = uri("https://kotlin.bintray.com/kotlinx") }
}

kotlin {
    val hostOs = System.getProperty("os.name")
    val isMingwX64 = hostOs.startsWith("Windows")
    val target = when {
        hostOs == "Mac OS X" -> macosX64("SocketStuff")
        hostOs == "Linux" -> linuxX64("SocketStuff")
        isMingwX64 -> mingwX64("SocketStuff")
        else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
    }

    target.apply {
        binaries {
            executable {
                entryPoint = "main"
            }
        }
    }

    sourceSets {
        val Main by getting
        val Test by getting
        commonMain {
            sourceSets["commonMain"].dependencies {
                implementation("io.ktor:ktor-network:1.5.3")
            }
        }
    }
}

有谁知道我是不是忘记添加依赖项之类的吗?

【问题讨论】:

    标签: kotlin ktor kotlin-native


    【解决方案1】:

    在 JVM 上有 ActorSelectorManager,在本机目标上有 WorkerSelectorManager。您可以使用构造函数SelectorManager() 来实例化适当的选择器管理器实例:

    import io.ktor.network.selector.*
    
    fun main() {
        val manager = SelectorManager()
    }
    

    【讨论】:

    • 首先:感谢您的回答。不幸的是,我得到This API is internal in ktor and should not be used. It could be removed or changed without notice. 有没有其他方法可以做到这一点?我可以添加@OptIn(InternalAPI::class),但我不想这样做
    • 此注释没有任何意义,将在 2.0.0 版本中删除,因此请随意使用该功能。
    • 我只有最后一个问题:我收到Compilation failed: Deserializer for declaration public kotlinx.coroutines/SingleThreadDispatcher|null[0] is not found 这是什么意思?
    • 尝试在原生目标上使用支持多线程的协程库:implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.3-native-mt"
    • 我现在收到Unfinished workers detected, 1 workers leaked!
    猜你喜欢
    • 1970-01-01
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    • 1970-01-01
    • 1970-01-01
    • 2022-08-11
    • 1970-01-01
    相关资源
    最近更新 更多