NullAway: Does not work with Java + Kotlin Android based projects

Repro:

buildscript {
  repositories {
    google()
    maven { url "https://plugins.gradle.org/m2/" }
  }

  dependencies {
    classpath "com.android.tools.build:gradle:2.3.3"
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.0"
    classpath "net.ltgt.gradle:gradle-errorprone-plugin:0.0.11"
  }
}

repositories {
  google()
  maven { url "https://plugins.gradle.org/m2/" }
}

apply plugin: "com.android.application"
apply plugin: "com.kotlin-android"
apply plugin: "com.kotlin-kapt"
apply plugin: "net.ltgt.errorprone"

android {
  ...
}

dependencies {
  kapt "com.uber.nullaway:nullaway:0.2.2"

  errorprone "com.google.errorprone:error_prone_core:2.1.1"
}

tasks.withType(JavaCompile) {
  // remove the if condition if you want to run NullAway on test code
  if (!name.toLowerCase().contains("test")) {
    options.compilerArgs += ["-Xep:NullAway:ERROR", "-XepOpt:NullAway:AnnotatedPackages=com.uber"]
  }
}

Error:

> Task :app:compileDebugJavaWithJavac FAILED
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
com.google.errorprone.InvalidCommandLineOptionException: NullAway is not a valid checker name

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed with exit code 2; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

* Get more help at https://help.gradle.org

BUILD FAILED in 1m 25s
77 actionable tasks: 77 executed

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 23 (1 by maintainers)

Most upvoted comments

Ok, dug at this a bit. It seems that when enabling both kotlin-android and kotlin-kapt plugins, Kotlin tries hard to prevent javac from running any annotation processors by passing -proc:none as a compiler arg and not passing the annotationProcessor dependencies on the processor path. This kills Error Prone plugins, as they need to be on the processor path. After much experimentation, I seem to have a hackish workaround. Here’s a modified build file for the sample app that seems to work:

buildscript {
    repositories {
      google()
    }
    dependencies {
        classpath deps.build.gradlePlugins.android
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.20"
        classpath "net.ltgt.gradle:gradle-errorprone-plugin:0.0.13"
    }
}
apply plugin: 'com.android.application'
apply plugin: 'net.ltgt.errorprone'
apply plugin: "kotlin-android"
apply plugin: "kotlin-kapt"
android {
  // Android stuff
}
configurations.maybeCreate("nullawayAnnotProc")
dependencies {
    compile deps.support.appcompat

    testCompile deps.test.junit4

    nullawayAnnotProc "com.uber.nullaway:nullaway:0.3.2"

    errorprone "com.google.errorprone:error_prone_core:2.1.3"
}

afterEvaluate {
  tasks.withType(JavaCompile) {
    // remove the if condition if you want to run NullAway on test code
    if (!name.toLowerCase().contains("test")) {
      options.annotationProcessorPath = configurations.nullawayAnnotProc
      options.compilerArgs.removeAll { it.equals("-proc:none") }
      options.compilerArgs += ["-Xep:NullAway:ERROR", "-XepOpt:NullAway:AnnotatedPackages=com.uber"]
    }
  }
}

Don’t ask me exactly why it works 😄 If you have “real” annotation processors, you can hopefully stick them in the kapt configuration and they will still run for your Java code as well. If anyone has luck with this let me know.

@ArazAbishov I can’t repro your exact error, but I think the issue is that if you want to run NullAway on your test code, you also need to add it to the testAnnotationProcessor configuration. I can update the docs to clarify that. Also, you should tweak your NullAway settings to pass "-XepOpt:NullAway:AnnotatedPackages=com.abishov"; if you leave it at com.uber it won’t find anything 😄

I’ll try to look into #25 soon. Beyond that I don’t know any particular reason why Java + Kotlin would cause a problem, as long as Error Prone runs correctly on the Java code and NullAway shows up as an annotation processor.