Enable error prone only on D8/R8 sources

Change-Id: I6369f1450c433cba5249315e25d09bbda9a3c895
diff --git a/build.gradle b/build.gradle
index 4eb27ef..583ae9e 100644
--- a/build.gradle
+++ b/build.gradle
@@ -1,6 +1,7 @@
 // Copyright (c) 2016, the R8 project authors. Please see the AUTHORS file
 // for details. All rights reserved. Use of this source code is governed by a
 // BSD-style license that can be found in the LICENSE file.
+import net.ltgt.gradle.errorprone.ErrorProneToolChain
 import org.gradle.internal.os.OperatingSystem
 import utils.Utils
 
@@ -8,16 +9,15 @@
 apply plugin: 'idea'
 apply plugin: 'com.google.protobuf'
 apply plugin: 'com.cookpad.android.licensetools'
-if (project.hasProperty('with_error_prone')) {
-    apply plugin: "net.ltgt.errorprone"
-    tasks.withType(JavaCompile) {
-        options.compilerArgs += [
-                '-XepDisableAllChecks',
-                '-Xep:ClassCanBeStatic:ERROR',
-                '-Xep:OperatorPrecedence:ERROR',
-                '-Xep:RemoveUnusedImports:ERROR']
-    }
-}
+apply plugin: 'net.ltgt.errorprone-base'
+
+def errorProneConfiguration = [
+    '-XepDisableAllChecks',
+    // D8 want to use reference equality, thus disable the checker explicitly
+    '-Xep:ReferenceEquality:OFF',
+    '-Xep:ClassCanBeStatic:WARN',
+    '-Xep:OperatorPrecedence:WARN',
+    '-Xep:RemoveUnusedImports:WARN']
 
 apply from: 'copyAdditionalJctfCommonFiles.gradle'
 
@@ -47,9 +47,7 @@
         // support java9.
         // classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.1'
         classpath files("third_party/shadow/shadow-2.0.1.jar")
-        if (project.hasProperty('with_error_prone')) {
-            classpath "net.ltgt.gradle:gradle-errorprone-plugin:0.0.13"
-        }
+        classpath "net.ltgt.gradle:gradle-errorprone-plugin:0.0.13"
     }
 }
 
@@ -352,6 +350,14 @@
     options.compilerArgs << '-Xlint:unchecked'
 }
 
+if (!project.hasProperty('without_error_prone')) {
+    compileJava {
+        // Enable error prone for D8/R8 sources.
+        toolChain ErrorProneToolChain.create(project)
+        options.compilerArgs += errorProneConfiguration
+    }
+}
+
 compileJctfCommonJava {
     dependsOn 'copyAdditionalJctfCommonFiles'
     options.compilerArgs = ['-Xlint:none']