第一步,我试图构建针对linuxX64的Kotlin Multiplatform命令行应用程序。因此,我想基于通用模块中使用的Ktor构建客户端。
版本概述
build.gradle.kts
plugins {
kotlin("multiplatform") version "1.3.31"
}
repositories {
mavenCentral()
maven { url = uri("https://kotlin.bintray.com/ktor") }
}
kotlin {
linuxX64("linux") {
binaries {
executable()
}
}
sourceSets {
val commonMain by getting {
dependencies {
implementation(kotlin("stdlib-common"))
api("io.ktor:ktor-client-core:1.1.5")
}
}
val linuxMain by getting {
dependsOn(commonMain)
dependencies {
api("io.ktor:ktor-client-curl:1.1.5")
}
}
}
}
src / linuxMain / kotlin / Main.kt
fun main(){
val client = MyClient()
client.execute()
}
src / commonMain / kotlin / MyClient.kt
import io.ktor.client.*
class MyClient {
private val client = HttpClient()
fun execute() {
//do something with Ktor client
}
}
构建项目时,出现以下构建问题:
11:15:21: Executing task 'build'...
> Configure project :
Kotlin Multiplatform Projects are an experimental feature.
> Task :wrapper
BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
> Configure project :
Kotlin Multiplatform Projects are an experimental feature.
> Task :compileKotlinLinux FAILED
e: .../src/commonMain/kotlin/MyClient.kt: (1, 8): Unresolved reference: io
e: .../src/commonMain/kotlin/MyClient.kt: (5, 26): Unresolved reference: HttpClient
我是Kotlin Native / Mutliplatform和Ktor的新手。所以,如果这是我的设置错误,请忍受...
答案 0 :(得分:0)
在{strong> settings.gradle.kts 中添加enableFeaturePreview("GRADLE_METADATA")
解决了构建问题。
我刚刚使用ktor-client-curl用linuxX64二进制文件成功进行了HTTP调用:-)