大概有两个片段:
// Verbose version
val typedArray = context.obtainStyledAttributes(attrs, styleable)
block(typedArray)
typedArray.recycle()
Run Code Online (Sandbox Code Playgroud)
// One-line version
context.obtainStyledAttributes(attrs, styleable).also(block).recycle()
Run Code Online (Sandbox Code Playgroud)
我想知道为什么冗长的代码块在Android Studio中看起来不错,而单行版本突出显示了getStyleStyledAttributes并给出以下警告:
与#recycle()一起使用后,应回收此TypedArray。
有谁知道这仅仅是一个Android Studio的绒毛检查缺陷,还是单行版本实际上有问题?
我正在尝试为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 记录器的依赖项吗?