Ford O.
Ford O.

Reputation: 1498

Kotlin Ktor Client mltiplatform gradle configuration

I would like to make an http request with ktor on Windows with kotlin multiplatform. My gradle configuration looks like this:

plugins {
    kotlin("multiplatform") version("1.5.0")
}

group = "me.me"
version = "1.0-SNAPSHOT"
val ktorVersion = "1.6.0"


repositories {
    mavenCentral()
    maven("https://dl.bintray.com/jetbrains/kotlin-native-dependencies")
}


kotlin {
    val hostOs = System.getProperty("os.name")
    val osName = when {
        HostManager.hostIsLinux -> LINUX
        HostManager.hostIsMac -> MACOS
        HostManager.hostIsMingw -> WINDOWS
        else -> error("unknown host")
    }

    jvm {
        withJava()

        attributes {
            attribute(OPERATING_SYSTEM_ATTRIBUTE, objects.named(osName))
            attribute(ARCHITECTURE_ATTRIBUTE, objects.named(X86_64))
        }
    }
    if (HostManager.hostIsLinux) linuxX64()
    if (HostManager.hostIsMac) macosX64()
    if (HostManager.hostIsMingw) mingwX64()

    targets.withType<KotlinNativeTarget> {
        binaries {
            executable {
                entryPoint = "hello.main"
            }
        }
    }

    sourceSets {
        commonMain {
            dependencies {
                implementation(kotlin("stdlib-common"))
                implementation("io.ktor:ktor-client-core:$ktorVersion")
                implementation("io.ktor:ktor-client-cio:$ktorVersion")
            }
        }

        named("jvmMain") {
            dependencies {
                ...
            }
        }

        targets.withType<KotlinNativeTarget> {
            named("${name}Main") {
                kotlin.srcDir("src/nativeMain/kotlin")
                resources.srcDir("src/nativeMain/resources")
                dependencies {
                    ...
                }
            }
        }
    }
}

And my commonMain/kotlin/main.kt is

import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.request.*

fun main(args: Array<String>) {
  println("Hello")
}

However, during compilation I get the following error:

Execution failed for task ':compileKotlinMingwX64'.
> Could not resolve all files for configuration ':mingwX64CompileKlibraries'.
   > Could not resolve io.ktor:ktor-client-cio:1.6.0.
     Required by:
         project :
      > No matching variant of io.ktor:ktor-client-cio:1.6.0 was found. The consumer was configured to find a usage of 'kotlin-api' of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native', attribute 'org.jetbrains.kotlin.native.target' with value 'mingw_x64' but:
          - Variant 'commonMainMetadataElements' capability io.ktor:ktor-client-cio:1.6.0 declares a usage of 'kotlin-api' of a component:
              - Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'common' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native'
              - Other compatible attribute:
                  - Doesn't say anything about org.jetbrains.kotlin.native.target (required 'mingw_x64')
          - Variant 'iosArm32ApiElements-published' capability io.ktor:ktor-client-cio:1.6.0 declares a usage of 'kotlin-api' of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native':
              - Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'ios_arm32' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'mingw_x64'
          - Variant 'iosArm32MetadataElements-published' capability io.ktor:ktor-client-cio:1.6.0 declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native':
              - Incompatible because this component declares a usage of 'kotlin-metadata' of a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'ios_arm32' and the consumer needed a usage of 'kotlin-api' of a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'mingw_x64'
          - Variant 'iosArm64ApiElements-published' capability io.ktor:ktor-client-cio:1.6.0 declares a usage of 'kotlin-api' of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native':
              - Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'ios_arm64' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'mingw_x64'
          - Variant 'iosArm64MetadataElements-published' capability io.ktor:ktor-client-cio:1.6.0 declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native':
              - Incompatible because this component declares a usage of 'kotlin-metadata' of a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'ios_arm64' and the consumer needed a usage of 'kotlin-api' of a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'mingw_x64'
          - Variant 'iosX64ApiElements-published' capability io.ktor:ktor-client-cio:1.6.0 declares a usage of 'kotlin-api' of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native':
              - Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'ios_x64' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'mingw_x64'
          - Variant 'iosX64MetadataElements-published' capability io.ktor:ktor-client-cio:1.6.0 declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native':
              - Incompatible because this component declares a usage of 'kotlin-metadata' of a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'ios_x64' and the consumer needed a usage of 'kotlin-api' of a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'mingw_x64'
          - Variant 'jvmApiElements-published' capability io.ktor:ktor-client-cio:1.6.0 declares an API of a component:
              - Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native'
              - Other compatible attribute:
                  - Doesn't say anything about org.jetbrains.kotlin.native.target (required 'mingw_x64')
          - Variant 'jvmRuntimeElements-published' capability io.ktor:ktor-client-cio:1.6.0 declares a runtime of a component:
              - Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native'
              - Other compatible attribute:
                  - Doesn't say anything about org.jetbrains.kotlin.native.target (required 'mingw_x64')
          - Variant 'linuxX64ApiElements-published' capability io.ktor:ktor-client-cio:1.6.0 declares a usage of 'kotlin-api' of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native':
              - Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'linux_x64' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'mingw_x64'
          - Variant 'macosX64ApiElements-published' capability io.ktor:ktor-client-cio:1.6.0 declares a usage of 'kotlin-api' of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native':
              - Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'macos_x64' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'mingw_x64'
          - Variant 'macosX64MetadataElements-published' capability io.ktor:ktor-client-cio:1.6.0 declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native':
              - Incompatible because this component declares a usage of 'kotlin-metadata' of a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'macos_x64' and the consumer needed a usage of 'kotlin-api' of a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'mingw_x64'
          - Variant 'metadataApiElements' capability io.ktor:ktor-client-cio:1.6.0:
              - Incompatible because this component declares a usage of 'kotlin-metadata' of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'common' and the consumer needed a usage of 'kotlin-api' of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native'
              - Other compatible attribute:
                  - Doesn't say anything about org.jetbrains.kotlin.native.target (required 'mingw_x64')
          - Variant 'tvosArm64ApiElements-published' capability io.ktor:ktor-client-cio:1.6.0 declares a usage of 'kotlin-api' of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native':
              - Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'tvos_arm64' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'mingw_x64'
          - Variant 'tvosArm64MetadataElements-published' capability io.ktor:ktor-client-cio:1.6.0 declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native':
              - Incompatible because this component declares a usage of 'kotlin-metadata' of a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'tvos_arm64' and the consumer needed a usage of 'kotlin-api' of a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'mingw_x64'
          - Variant 'tvosX64ApiElements-published' capability io.ktor:ktor-client-cio:1.6.0 declares a usage of 'kotlin-api' of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native':
              - Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'tvos_x64' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'mingw_x64'
          - Variant 'tvosX64MetadataElements-published' capability io.ktor:ktor-client-cio:1.6.0 declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native':
              - Incompatible because this component declares a usage of 'kotlin-metadata' of a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'tvos_x64' and the consumer needed a usage of 'kotlin-api' of a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'mingw_x64'
          - Variant 'watchosArm32ApiElements-published' capability io.ktor:ktor-client-cio:1.6.0 declares a usage of 'kotlin-api' of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native':
              - Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'watchos_arm32' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'mingw_x64'
          - Variant 'watchosArm32MetadataElements-published' capability io.ktor:ktor-client-cio:1.6.0 declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native':
              - Incompatible because this component declares a usage of 'kotlin-metadata' of a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'watchos_arm32' and the consumer needed a usage of 'kotlin-api' of a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'mingw_x64'
          - Variant 'watchosArm64ApiElements-published' capability io.ktor:ktor-client-cio:1.6.0 declares a usage of 'kotlin-api' of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native':
              - Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'watchos_arm64' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'mingw_x64'
          - Variant 'watchosArm64MetadataElements-published' capability io.ktor:ktor-client-cio:1.6.0 declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native':
              - Incompatible because this component declares a usage of 'kotlin-metadata' of a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'watchos_arm64' and the consumer needed a usage of 'kotlin-api' of a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'mingw_x64'
          - Variant 'watchosX86ApiElements-published' capability io.ktor:ktor-client-cio:1.6.0 declares a usage of 'kotlin-api' of a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native':
              - Incompatible because this component declares a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'watchos_x86' and the consumer needed a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'mingw_x64'
          - Variant 'watchosX86MetadataElements-published' capability io.ktor:ktor-client-cio:1.6.0 declares a component, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'native':
              - Incompatible because this component declares a usage of 'kotlin-metadata' of a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'watchos_x86' and the consumer needed a usage of 'kotlin-api' of a component, as well as attribute 'org.jetbrains.kotlin.native.target' with value 'mingw_x64'

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

Upvotes: 1

Views: 1214

Answers (1)

Phil Dukhov
Phil Dukhov

Reputation: 87635

The gradle sync fails because CIO engine only supports java, and any dependency included in common part should be multiplatform(native).

Just move implementation("io.ktor:ktor-client-cio:$ktorVersion") into jvmMain dependencies.

There's no engine that can be added to common code, so you need to add an appropriate engine for each platform you support, and HttpClient() will pick one of them by itself. Check out documentation for more details

Upvotes: 2

Related Questions