问题描述
我正在尝试为Android应用程序中的Ktor http请求添加日志记录.根据文档,我必须添加gradle依赖
I'm trying to add logging for Ktor http requests in Android application. According to docs I have to add gradle dependency
implementation "io.ktor:ktor-client-logging:$ktor_version"
并使用此代码段
val client = HttpClient() { install(Logging) { logger = Logger.DEFAULT level = LogLevel.HEADERS } }
问题是编译器忽略"软件包io.ktor.client.features.logging添加为依赖项.奇怪的是,JsonFeature(作为类似的依赖项添加)工作得很好.
Problem is that compiler "ignores" package 'io.ktor.client.features.logging' added as a dependency. What's strange is that JsonFeature (added as similar dependency) works just fine.
install(JsonFeature) { // perfectly works ... } install(Logging) { // unresolved reference ... }
我已经检查了gradle添加到项目中的.jar文件,它包含所有预期的类,我可以打开它们并查看源代码,但不可思议的是不能在我的应用程序中使用.经过数小时的研究,我想这可能与gradle元数据有关,或者日志记录功能是多平台的,并且需要一些额外的gradle配置,但是不幸的是,我不是gradle专家.
I already checked .jar file that gradle added to the project, it contains all expected classes, I can open them and see the source code, but magically just can't use in my app. After hours of research I guess it may be somehow related to gradle metadata or that logging feature is multiplatform and some additional gradle configuration is required, but unfortunately I'm not a gradle expert.
我尝试将enableFeaturePreview("GRADLE_METADATA")添加到settings.gradle,但是没有效果.甚至尝试将"-jvm"添加到依赖项.
I tried adding enableFeaturePreview("GRADLE_METADATA") to settings.gradle, but no effect. Even tried to add "-jvm" to dependency.
implementation "io.ktor:ktor-client-logging-jvm:$ktor_version"
有了这种依赖关系,Android Studio成功找到了程序包,但是编译失败并出现以下错误
With this dependency Android Studio finding package successfully, but fails to compile with following error
More than one file was found with OS independent path 'META-INF/ktor-http.kotlin_module'
任何人都可以阐明如何为Ktor记录器正确配置依赖项吗?
Can anyone please clarify how to properly configure dependency for Ktor logger?
推荐答案
对于ktor-client-logging,您必须为每个平台设置依赖项:
For the ktor-client-logging you have to have the dependency set for each platform:
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" } }
对于元,将
添加到android {}块内的app/build.gradle中:
android { packagingOptions { exclude 'META-INF/common.kotlin_module' exclude 'META-INF/*.kotlin_module' } }
这篇关于编译器无法解析io.ktor.client.features.logging中的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!