Skip to content

Using Error Prone with Buck and OkBuck

Gautam Korlam edited this page Oct 17, 2019 · 3 revisions

This page describes how OkBuck users can configure Buck to use Error Prone.

Using Error Prone javac on JDK 8

Using Error Prone on JDK 8 requires compiling with the Error Prone javac compiler. (If you are on JDK 11 or higher, you do not need to use Error Prone javac.) To use this compiler with Buck, follow these steps:

  1. Download javac-9+181-r4173-1.jar.
  2. Run tooling/errorprone/createtinyjavaxtools.sh to create a tinyjavaxtools.jar that contains key classes that need to be in Buck's bootclasspath. Usage:
    createtinyjavaxtools.sh path/to/javac-9+181-r4173-1.jar

This creates tinyjavaxtools.jar in the directory from which it is run.

  1. Add the following to your .buckconfig file to use the Error Prone javac:
[tools]
  javac_jar = path/to/javac-9+181-r4173-1.jar
  1. Add -Xbootclasspath/p:path/to/tinyjavaxtools.jar to your .buckjavaargs file to place tinyjavaxtools.jar in the Buck bootclasspath.

Once you complete these steps, Buck Java builds will compile with the Error Prone javac, and you are ready to enable Error Prone.

Enabling Error Prone for Buck builds

To enable Error Prone in Buck builds add the following to your Gradle configuration for the relevant modules:

dependencies {
    // use appropriate Error Prone version
    annotationProcessor 'com.google.errorprone:error_prone_core:2.3.3'
}
compileJava {
    options.compilerArgs << '-XDcompilePolicy=simple'
    // Any Error Prone config options should come immediately after -Xplugin:ErrorProne
    options.compilerArgs << '-Xplugin:ErrorProne -Xep:RemoveUnusedImports:ERROR'
}

With this configuration, Error Prone will run in the Buck build. To handle Android builds you can use tasks.withType(JavaCompile) rather than compileJava. Alternately, for greater flexibility you can configure Error Prone compiler arguments inside your Buck configuration (e.g., in custom DEFS), though this won't be synced back to the Gradle build.

At this point, OkBuck does not have integrated support for the Gradle Error Prone plugin, the best way to use Error Prone with Gradle. The instructions above enable Error Prone with Buck builds, but Gradle builds on JDK 8 will break, since Error Prone javac is not being used. To fix Gradle builds, you can add configuration like this to fork Java builds and add the Error Prone javac to the boot classpath.