Android 如何在数据绑定的基础上使构建过程失败';s safeUnbox警告
解释“safeUnbox警告”是什么 我的build.gradle中包含以下内容:Android 如何在数据绑定的基础上使构建过程失败';s safeUnbox警告,android,data-binding,warnings,Android,Data Binding,Warnings,解释“safeUnbox警告”是什么 我的build.gradle中包含以下内容: lintOptions { quiet false abortOnError true warningsAsErrors true baseline file("lint-baseline.xml") } 后来: tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) { kotlinOptions
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警告”后使整个构建过程失败 我找到了解决办法,耶 将以下咒语放到根
gradle.build
解决了这个问题
subprojects {
afterEvaluate {
if (project.plugins.hasPlugin("kotlin-kapt")) {
kapt {
javacOptions {
option("-Xmaxerrs", 1000)
option("-Werror")
}
}
}
}
}
此外,该咒语还增加了记录错误数量的限制(默认值:100),这在使用数据绑定时非常有用。补充Alexander的答案,您也可以在模块
build.gradle
中定义,这可能更具可读性:
android {
...
kapt {
javacOptions {
option("-Xmaxerrs", 1000)
option("-Werror")
}
}
}