project:build.gradle
plugins {
id 'com.android.application' version '7.4.0' apply false
id 'com.android.library' version '7.4.0' apply false
id 'org.jetbrains.kotlin.android' version '1.8.10' apply false
}
app:build.gradle
def ktor_version = "2.2.4"
implementation("io.ktor:ktor-client-core:$ktor_version")
implementation("io.ktor:ktor-client-cio:$ktor_version")
implementation "io.ktor:ktor-serialization-kotlinx-json:$ktor_version"
implementation 'io.ktor:ktor-client-logging:$ktor_version'
implementation "io.ktor:ktor-client-content-negotiation:$ktor_version"
按照文档中的方式进行设置:
val instance = HttpClient(CIO) {
install(Logging) {
logger = Logger.DEFAULT
level = LogLevel.HEADERS
filter { request ->
request.url.host.contains("ktor.io")
}
}
}
但我得到了错误(至少,我是这么认为的):
W/System.err: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
W/System.err: SLF4J: Defaulting to no-operation (NOP) logger implementation
W/System.err: SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
implementation("io.ktor:ktor-client-logging-jvm:$ktor_version")
它不起作用,同样的错误。
implementation 'ch.qos.logback:logback-classic:1.4.5'
然后出现了新的错误:
* What went wrong:
Execution failed for task ':app:mergeDebugJavaResource'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.MergeJavaResWorkAction
> 2 files found with path 'META-INF/INDEX.LIST' from inputs:
- D:\Software\.gradle\caches\modules-2\files-2.1\ch.qos.logback\logback-classic\1.4.5\28e7dc0b208d6c3f15beefd73976e064b4ecfa9b\logback-classic-1.4.5.jar
- D:\Software\.gradle\caches\modules-2\files-2.1\ch.qos.logback\logback-core\1.4.5\e9bb2ea70f84401314da4300343b0a246c8954da\logback-core-1.4.5.jar
Adding a packagingOptions block may help, please refer to
https://developer.android.com/reference/tools/gradle-api/7.4/com/android/build/api/dsl/ResourcesPackagingOptions
for more information
我不太确定这个
greadle cache
是做什么用的。但从逻辑上讲,我很确定我的项目在添加 logback
之前没有引入 ch.qos.logback:logback-classic:1.4.5
相关依赖。
logback
之后,就导致了它自己的错误?这不合逻辑,我错过了什么?ktor-client-logging
吗?它到底有什么作用?我在Android
平台上使用它。它会打印每个请求的结果吗?如果您处于多平台项目中,而不是在实现 Ktor 客户端工作的模块中,则在模块级 gradle 文件中,我们必须使用下一个设置(采用 Kotlin Gradle DSL 语法):
kotlin {
...
sourceSets {
commonMain.dependencies {
implementation(libs.ktor.client.core)
implementation(libs.ktor.client.logging)
}
androidMain.dependencies {
implementation("org.slf4j:slf4j-android:1.7.36")
}
}
}
感谢@AlekseiTirman,他在对此问题的评论中提到了这一点。
您可以添加
debugImplementation "org.slf4j:slf4j-simple:2.0.7"
我将 Logger.DEFAULT 更改为 Logger。SIMPLE 并且能够在我的 Logcat 中查看日志
install(Logging){
logger = Logger.SIMPLE
level = LogLevel.ALL
}