John O'Reilly
John O'Reilly

Reputation: 10350

Kotlin Multiplatform with iOS: "Use of undeclared type 'KotlinCoroutineContext'"

I'm attempting to consume Kotlin multiplatform code (that uses Ktor and Kotlin Coroutines) on iOS. The framework is generated correctly and can invoke some of the classes/methods exposed without any problem. However if I try to add following (as is done in https://github.com/JetBrains/kotlinconf-app/blob/master/konfios/konfswift/ui/UI.swift for example). I get "Use of undeclared type 'KotlinCoroutineContext" (and I see in SharedCode.h that it's not present)

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()
        }
    }
}

The gradle file for share code includes following (am using Kotlin 1.3.11, Ktor 1.0.1 and Coroutines 1.0.1 along with 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}"
    }

I'm suspecting that issue might be that those symbols aren't explicitly exposed (also tried using api instead of implementation for coroutine dependencies but that didn't help).

This is what I have so far: https://github.com/joreilly/galway-bus-android/tree/kotlin_native

UPDATE:

Tried newly released Kotlin v1.3.20 but now getting following

> 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].
``

Upvotes: 1

Views: 1714

Answers (1)

Max Cruz
Max Cruz

Reputation: 1375

I had the same issue in your update

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

Basically, with Kotlin 1.3.20, Ktor needs to be upgraded to 1.1.2 and then Gradle 4.10 is required to use the latest Kotlin native. That is because of Gradle Metadata

Before that update, I start to have a new issue to build the iOS artifact regarding the Kotlin Serializer:

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

To fix that issue I removed the @Serializable annotation from classes and provided the KSerializer interface separated.

Upvotes: 3

Related Questions