我在 gradle 中有启用了 explicitApi kotlin 功能的 Android 库模块
android {
kotlinOptions {
freeCompilerArgs += '-Xexplicit-api=warning'
}
}
一切都很好,但问题是包 src/test 和 src/androidTest 中的测试类也会报告警告。如何从显式 api 控制中排除测试类?
谢谢
最佳答案
据我所知,你不能!我正考虑在这周打开一个错误报告,但从未得到它。同时,我建议您在构建脚本中添加类似这样的内容,这至少可以为 Kotlin 编译器修复它(但您仍然会看到 IDE 警告):
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
if (!it.name.contains("Test")) {
kotlinOptions.freeCompilerArgs += "-Xexplicit-api=strict"
}
}
如果您使用 Gradle Kotlin DSL:tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
if ("UnitTest" !in name) {
kotlinOptions.freeCompilerArgs += "-Xexplicit-api=strict"
}
}
并且不要在 android.kotlinOptions
中设置编译器参数,仅在该 block 中,因此它仅应用于非测试源集。编辑:我刚刚检查并在 IntelliJ 中修复了警告错误,因此它应该在几个月到一年内在 Android Studio 中修复。
关于android - 如何排除 Kotlin 中测试类的显式Api 警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64267076/