1

我正在尝试在 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].
``
4

1 回答 1

3

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

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

基本上,使用 Kotlin1.3.20时,需要将 Ktor 升级到1.1.2然后 Gradle4.10才能使用最新的 Kotlin 本机。那是因为 Gradle 元数据

在此更新之前,我开始有一个新问题来构建有关 Kotlin 序列化器的 iOS 工件:

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

为了解决这个问题,我@Serializable从类中删除了注释并提供了KSerializer分离的接口。

于 2019-02-02T05:22:35.010 回答