Kotlin Ktor 客户端 mltiplatform gradle 配置
Posted
技术标签:
【中文标题】Kotlin Ktor 客户端 mltiplatform gradle 配置【英文标题】:Kotlin Ktor Client mltiplatform gradle configuration 【发布时间】:2021-09-12 08:07:05 【问题描述】:我想在带有 kotlin 多平台的 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("$nameMain")
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.
【问题讨论】:
【参考方案1】:由于 CIO 引擎仅支持 java,gradle 同步失败,并且公共部分中包含的任何依赖项都应该是 multiplatform(native)。
只需将 implementation("io.ktor:ktor-client-cio:$ktorVersion")
移动到 jvmMain
依赖项中即可。
没有可以添加到通用代码中的引擎,因此您需要为您支持的每个平台添加适当的引擎,HttpClient()
将自行选择其中一个。查看documentation了解更多详情
【讨论】:
换句话说,不能将http-client与mingwX64一起使用,是吗。 mingwX64 有 curl 引擎:ktor.io/docs/http-client-engines.html#desktop以上是关于Kotlin Ktor 客户端 mltiplatform gradle 配置的主要内容,如果未能解决你的问题,请参考以下文章
Ktor 1.0 发布:Kotlin Web 框架;GoLand 2018.3 正式版发布!| 更新
如何确保清除 ktor websocket 客户端创建的所有 Kotlin 协程?
如何使用 ktor kotlin 通过 POST 发送 JSON 字符串?