带有iOS的Kotlin多平台:“使用未声明的类型'KotlinCoroutineContext'”

时间:2019-01-21 14:37:02

标签: kotlin ktor kotlin-multiplatform

我正在尝试在iOS上使用Kotlin多平台代码(使用Ktor和Kotlin协程)。该框架是正确生成的,可以毫无问题地调用某些公开的类/方法。但是,如果我尝试添加以下内容(例如,在https://github.com/JetBrains/kotlinconf-app/blob/master/konfios/konfswift/ui/UI.swift中这样做)。我得到“使用未声明的类型'KotlinCoroutineContext”(并且在SharedCode.h中看到它不存在)

import Foundation
import UIKit
import SharedCode


public class CoroutineDispatcher: Kotlinx_coroutines_core_nativeCoroutineScope {
    override public func dispatch(context: KotlinCoroutineContext, block: Kotlinx_coroutines_core_nativeRunnable) {
        DispatchQueue.main.async {
            block.run()
        }
    }
}

共享代码的gradle文件包括以下内容(使用Kotlin 1.3.11,Ktor 1.0.1和Coroutines 1.0.1以及Gradle 4.7)

    commonMain.dependencies {
        implementation "org.jetbrains.kotlin:kotlin-stdlib-common:${Versions.kotlin}"
        implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:${Versions.kotlinCoroutines}"
        implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:${Versions.kotlinxSerialization}"

        implementation "io.ktor:ktor-client-core:${Versions.ktor}"
        implementation "io.ktor:ktor-client-json:${Versions.ktor}"

    }

    androidMain.dependencies {
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${Versions.kotlin}"
        implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:${Versions.kotlinCoroutines}"

        implementation "io.ktor:ktor-client-core-jvm:${Versions.ktor}"
        implementation "io.ktor:ktor-client-json-jvm:${Versions.ktor}"
    }

    iOSMain.dependencies {
        implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:${Versions.kotlinCoroutines}"
        implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:${Versions.kotlinxSerialization}"

        implementation "io.ktor:ktor-client-ios:${Versions.ktor}"
        implementation "io.ktor:ktor-client-core-ios:${Versions.ktor}"
        implementation "io.ktor:ktor-client-json-ios:${Versions.ktor}"
    }

我怀疑这个问题可能是那些符号未显式公开的(对于协程依赖性,也尝试使用api而不是implementation来解决,但这无济于事)。

这是我到目前为止的内容:https://github.com/joreilly/galway-bus-android/tree/kotlin_native

更新

尝试了新发布的Kotlin v1.3.20,但现在受到关注

> Task :SharedCode:linkMainDebugFrameworkIOS
warning: skipping /Users/jooreill/.gradle/caches/modules-2/files-2.1/io.ktor/ktor-client-ios_debug_ios_x64/1.0.1/7ac4ac71743dbff041cc51a117e1732a9133d3b8/ktor-client-ios.klib. The abi versions don't match. Expected '[5]', found '2'
warning: the compiler versions don't match either. Expected '[1.1.1]', found '1.0.2-release-4769'
error: compilation failed: Could not find "/Users/jooreill/.gradle/caches/modules-2/files-2.1/io.ktor/ktor-client-ios_debug_ios_x64/1.0.1/7ac4ac71743dbff041cc51a117e1732a9133d3b8/ktor-client-ios.klib" in [/Users/jooreill/devroot/github/galway-bus-android/SharedCode, /Users/jooreill/.konan/klib, /Users/jooreill/.konan/kotlin-native-macos-1.1.1/klib/common, /Users/jooreill/.konan/kotlin-native-macos-1.1.1/klib/platform/ios_x64].
``

1 个答案:

答案 0 :(得分:1)

我在更新中遇到了相同的问题

The abi versions don't match. Expected '[5]', found '2'1

基本上,科特林1.3.20,Ktor需要升级到1.1.2,然后摇篮4.10是需要使用最新科特林天然。这是因为摇篮元数据的

在进行此更新之前,我开始遇到有关构建Kotlin序列化程序的iOS工件的新问题:

error: compilation failed: Can’t locate polymorphic serializer definition

要解决这个问题,我除去@Serializable从类注释和设置在KSerializer界面分离。