Kotlin Ktor 客户端多平台 gradle 配置

Kotlin Ktor Client mltiplatform gradle configuration

我想使用 kotlin multiplatform 在 Windows 上使用 ktor 发出 http 请求。我的 gradle 配置如下所示:

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 {
                    ...
                }
            }
        }
    }
}

我的commonMain/kotlin/main.kt

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

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

但是,在编译过程中出现以下错误:

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.

gradle 同步失败,因为 CIO 引擎仅支持 java,并且公共部分中包含的任何依赖项都应该是多平台(本机)。

只需将 implementation("io.ktor:ktor-client-cio:$ktorVersion") 移动到 jvmMain 依赖项中。

没有可以添加到通用代码中的引擎,因此您需要为您支持的每个平台添加合适的引擎,HttpClient() 将自行选择其中之一。查看 documentation 了解更多详情