2

This question explains what the "safeUnbox warning" is.

I have the following in my build.gradle:

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

and later:

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

But data-binding safeUnbox warnings don't fail the build process. The output has complains about warnings and that warnings was turned to errors:

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

But at the very end of the building process I have:

BUILD SUCCESSFUL in 46s 

Is there any way to make the whole build process unsuccessful upon "safeUnbox warning"?

Alexander Skvortsov
  • 2,696
  • 2
  • 17
  • 31

2 Answers2

3

I have found the solution, Yay!

Putting the following spell to the root gradle.build solves the problem.

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

Also the spell increases the limit of how many errors are being logged (default value: 100) which is useful if DataBinding is used.

Alexander Skvortsov
  • 2,696
  • 2
  • 17
  • 31
1

Complementing the answer of Alexander, you can define this in your module build.gradle as well, which might be more readable:

android {

    ...

    kapt {
        javacOptions {
            option("-Xmaxerrs", 1000)
            option("-Werror")
        }
    }
}
Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71