Allow version 9 toolchains for EXPERIMENTAL_USE_OPENJDK9

By default, the Android build enforces an OpenJDK 8 toolchain,
whose name contains the strings "openjdk" and "1.8".

After this CL, the check can be changed to enforce a toolchain
name starting with "9" and without the need for "openjdk" having
to occur in the name.

This experimental new check can be enabled by running:
  export EXPERIMENTAL_USE_OPENJDK9=true
To switch back to the standard check, run:
  unset EXPERIMENTAL_USE_OPENJDK9

Test: make ANDROID_COMPILE_WITH_JACK=false checkbuild tests \
      && make checkbuild tests
      (with OpenJDK 8u45 toolchain on the PATH)
Test: make EXPERIMENTAL_USE_OPENJDK9=true \
      ANDROID_COMPILE_WITH_JACK=false checkbuild
      (with jdk 9-ea+170 toolchain on the PATH)
Bug: 38177295

Change-Id: I75de3e23fe0b7f41eb6dd3f55dadd3fa3c3383bd
diff --git a/ui/build/java.go b/ui/build/java.go
index 481a680..473af01 100644
--- a/ui/build/java.go
+++ b/ui/build/java.go
@@ -66,9 +66,19 @@
 	var required_java_version string
 	var java_version_regexp *regexp.Regexp
 	var javac_version_regexp *regexp.Regexp
-	required_java_version = "1.8"
-	java_version_regexp = regexp.MustCompile(`[ "]1\.8[\. "$]`)
-	javac_version_regexp = java_version_regexp
+
+	oj9_env, _ := config.Environment().Get("EXPERIMENTAL_USE_OPENJDK9")
+	experimental_use_openjdk9 := oj9_env != ""
+
+	if experimental_use_openjdk9 {
+		required_java_version = "9"
+		java_version_regexp = regexp.MustCompile(`^java .* "9.*"`)
+		javac_version_regexp = regexp.MustCompile(`^javac 9`)
+	} else {
+		required_java_version = "1.8"
+		java_version_regexp = regexp.MustCompile(`[ "]1\.8[\. "$]`)
+		javac_version_regexp = java_version_regexp
+	}
 
 	java_version := javaVersionInfo.java_version_output
 	javac_version := javaVersionInfo.javac_version_output
@@ -95,7 +105,10 @@
 	}
 
 	if runtime.GOOS == "linux" {
-		if !strings.Contains(java_version, "openjdk") {
+		// Early access builds of OpenJDK 9 do not contain the string "openjdk" in the
+		// version name. TODO(tobiast): Reconsider once the OpenJDK 9 toolchain is stable.
+		// http://b/62123342
+		if !strings.Contains(java_version, "openjdk") && !experimental_use_openjdk9 {
 			ctx.Println("*******************************************************")
 			ctx.Println("You are attempting to build with an unsupported JDK.")
 			ctx.Println()