如何在 Kotlin 中排除测试类的explicitApi 警告?

Luk*_*ruk 6 android kotlin

我有 Android 库模块,在 gradle 中启用了explicitApi kotlin 功能

android {
    kotlinOptions {
        freeCompilerArgs += '-Xexplicit-api=warning'
    }
}
Run Code Online (Sandbox Code Playgroud)

一切都很好,但问题是,包 src/test 和 src/androidTest 中的测试类也会报告警告。

如何从显式 API 控制中排除测试类?

谢谢

Nic*_*las 6

据我所知,你不能!我本想在本周打开一份错误报告,但从未付诸实践。同时,我建议您在构建脚本中添加类似的内容,这至少可以修复 Kotlin 编译器的问题(但您仍然会看到 IDE 警告):

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
    if (!it.name.contains("Test")) {
        kotlinOptions.freeCompilerArgs += "-Xexplicit-api=strict"
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您使用 Gradle Kotlin DSL:

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
    if (!it.name.contains("Test")) {
        kotlinOptions.freeCompilerArgs += "-Xexplicit-api=strict"
    }
}
Run Code Online (Sandbox Code Playgroud)

并且不要android.kotlinOptions仅在该块中设置编译器参数,以便它仅应用于非测试源集。

编辑:我刚刚检查过,警告错误已在 IntelliJ 中修复,因此应该在几个月到一年内在 Android Studio 中修复。