编译器无法解析 io.ktor.client.features.logging 中的类

Ale*_*hov 3 logging android gradle kotlin ktor

我正在尝试为KtorAndroid 应用程序中的 http 请求添加日志记录。根据文档,我必须添加 gradle 依赖项

implementation "io.ktor:ktor-client-logging:$ktor_version"

只需使用此代码段

val client = HttpClient() {
    install(Logging) {
        logger = Logger.DEFAULT
        level = LogLevel.HEADERS
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是编译器“忽略”作为依赖项添加的包“io.ktor.client.features.logging”。奇怪的是 JsonFeature(添加为类似的依赖项)工作得很好。

install(JsonFeature) { // perfectly works
...
}

install(Logging) { // unresolved reference
...
}
Run Code Online (Sandbox Code Playgroud)

我已经检查.jar了 gradle 添加到项目中的文件,它包含所有预期的类,我可以打开它们并查看源代码,但神奇的是不能在我的应用程序中使用。经过数小时的研究,我想这可能与 gradle 元数据有某种关系,或者日志功能是多平台的,并且需要一些额外的 gradle 配置,但不幸的是我不是 gradle 专家。

我尝试添加enableFeaturePreview("GRADLE_METADATA")settings.gradle,但没有效果。甚至试图将“-jvm”添加到依赖项中。

implementation "io.ktor:ktor-client-logging-jvm:$ktor_version"

有了这个依赖,Android Studio 成功找到包,但编译失败,出现以下错误

More than one file was found with OS independent path 'META-INF/ktor-http.kotlin_module'

任何人都可以澄清如何正确配置 Ktor 记录器的依赖项吗?

Rod*_*roz 9

对于ktor-client-logging您必须为每个平台设置依赖项:

commonMain {
    dependencies {
        implementation "ch.qos.logback:logback-classic:1.2.3"
        implementation "io.ktor:ktor-client-logging:$ktor_version"
    }
}

androidMain {
    dependencies {
        implementation "io.ktor:ktor-client-logging-jvm:$ktor_version"
    }
}

iosMain {
    dependencies {
        implementation "io.ktor:ktor-client-logging-native:$ktor_version"
    }
}
Run Code Online (Sandbox Code Playgroud)

至于元META-INF/ktor-http.kotlin_module添加到块app/build.gradle内部android {}

android {
    packagingOptions {
        exclude 'META-INF/common.kotlin_module'
        exclude 'META-INF/*.kotlin_module'
    }
}
Run Code Online (Sandbox Code Playgroud)