【问题标题】:How to fix 'Unresolved reference: HttpClient' with ktor-client-core targeting linuxX64如何使用针对 linuxX64 的 ktor-client-core 修复“未解决的参考:HttpClient”
【发布时间】:2019-09-29 00:57:50
【问题描述】:

我正在尝试在第一步中构建一个针对 linuxX64 的 Kotlin Multiplatform 命令行应用程序。因此,我想构建一个基于通用模块中使用的 Ktor 的客户端。

这是我相当简单的设置:

版本概览

  • Ktor 1.1.5
  • Kotlin 多平台 1.3.31
  • Gradle 5.3.1

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 的新手。如果我的设置有误,请多多包涵……

【问题讨论】:

  • enableFeaturePreview("GRADLE_METADATA") 添加到 settings.gradle.kts 修复了构建问题。我刚刚使用 ktor-curl-client 使用 linuxX64 二进制文件进行了成功的 HTTP 调用 :-)

标签: kotlin ktor kotlin-multiplatform


【解决方案1】:

enableFeaturePreview("GRADLE_METADATA") 添加到 settings.gradle.kts 修复了构建问题。 我刚刚使用 ktor-client-curl 使用 linuxX64 二进制文件成功进行了 HTTP 调用 :-)

【讨论】:

    猜你喜欢
    • 2019-10-24
    • 1970-01-01
    • 2021-06-13
    • 2019-12-14
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多