如何在数据绑定的 safeUnbox 警告时使构建过程失败

How to fail build process upon data-binding's safeUnbox warnings

This question 解释了 "safeUnbox warning" 是什么。

我的 build.gradle 中有以下内容:

lintOptions {
    quiet false
    abortOnError true
    warningsAsErrors true
    baseline file("lint-baseline.xml")
}

及以后:

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
    kotlinOptions {
        jvmTarget = "1.8"
        allWarningsAsErrors = true
    }
}

但是数据绑定 safeUnbox 警告不会使构建过程失败。输出抱怨警告并且警告已变成错误:

w: warning: viewModel.doorsState.getValue().first is a boxed field but needs to be un-boxed to execute android:text. This may cause NPE so Data Binding will safely unbox it. You can change the expression and explicitly wrap viewModel.doorsState.getValue().first with safeUnbox() to prevent the warning
  file:///.../app/src/debug/res/layout/activity_car_connection_debug.xml Line:75
e: warnings found and -Werror specified

但是在构建过程的最后我有:

BUILD SUCCESSFUL in 46s 

有什么方法可以使整个构建过程在 "safeUnbox warning" 时不成功?

我找到了解决方案,耶!

将以下拼写放在根部 gradle.build 即可解决问题。

subprojects {
    afterEvaluate {
        if (project.plugins.hasPlugin("kotlin-kapt")) {
            kapt {
                javacOptions {
                    option("-Xmaxerrs", 1000)
                    option("-Werror")
                }
            }
        }
    }
}

该法术还增加了记录错误数量的限制(默认值:100),这在使用 DataBinding 时很有用。

补充 Alexander 的回答,您也可以在模块 build.gradle 中定义它,这可能更具可读性:

android {

    ...

    kapt {
        javacOptions {
            option("-Xmaxerrs", 1000)
            option("-Werror")
        }
    }
}