Task to compile R8 with Java 11

- Add task r8WithRelocatedDeps11 to compile R8
  with Java 11 (working).
- Add task R8Lib11 to shrink R811 using R8
  compiled with Java 11

Bug: 133608609
Change-Id: I76a43ec3c773bad30bcbad1abdeda64cc950bd1e
diff --git a/build.gradle b/build.gradle
index 12ef47a..b9a6506 100644
--- a/build.gradle
+++ b/build.gradle
@@ -21,7 +21,7 @@
         jcenter()
     }
     dependencies {
-        classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.4'
+        classpath 'com.github.jengelman.gradle.plugins:shadow:4.0.2'
     }
 }
 
@@ -250,10 +250,12 @@
 }
 
 def r8LibPath = "$buildDir/libs/r8lib.jar"
+def r8LibPath11 = "$buildDir/libs/r8lib11.jar"
 def r8LibExludeDepsPath = "$buildDir/libs/r8lib-exclude-deps.jar"
 def r8LibGeneratedKeepRulesPath = "$buildDir/generated/keep.txt"
 def r8LibGeneratedKeepRulesExcludeDepsPath = "$buildDir/generated/keep-exclude-deps.txt"
 def r8LibTestPath = "$buildDir/classes/r8libtest"
+def java11ClassFiles = "build/classes/java/mainJava11"
 
 def osString = OperatingSystem.current().isLinux() ? "linux" :
         OperatingSystem.current().isMacOsX() ? "mac" : "windows"
@@ -528,6 +530,23 @@
     targetCompatibility = JavaVersion.VERSION_11
 }
 
+task compileMainWithJava11 (type: JavaCompile) {
+    def jdkDir = 'third_party/openjdk/jdk-11/'
+    options.fork = true
+    options.forkOptions.jvmArgs = []
+    if (OperatingSystem.current().isLinux()) {
+        options.forkOptions.javaHome = file(jdkDir + 'Linux')
+    } else if (OperatingSystem.current().isMacOsX()) {
+        options.forkOptions.javaHome = file(jdkDir + 'Mac')
+    } else {
+        options.forkOptions.javaHome = file(jdkDir + 'Windows')
+    }
+    source = sourceSets.main.allSource
+    destinationDir = file(java11ClassFiles)
+    sourceCompatibility = JavaVersion.VERSION_11
+    targetCompatibility = JavaVersion.VERSION_11
+    classpath = sourceSets.main.compileClasspath
+}
 
 if (!project.hasProperty('without_error_prone') &&
         // Don't enable error prone on Java 8 as the plugin setup does not support it.
@@ -663,6 +682,16 @@
     baseName 'sources'
 }
 
+task repackageSources11(type: ShadowJar) {
+    dependsOn compileMainWithJava11
+    from file(java11ClassFiles)
+    mergeServiceFiles(it)
+    if (!project.hasProperty('lib_no_relocate')) {
+        configureRelocations(it)
+    }
+    baseName 'sources'
+}
+
 task r8WithRelocatedDeps(type: ShadowJar) {
     from consolidatedLicense.outputs.files
     baseName 'r8_with_relocated_deps'
@@ -677,6 +706,20 @@
     exclude "META-INF/*.kotlin_module"
 }
 
+task r8WithRelocatedDeps11(type: ShadowJar) {
+    from consolidatedLicense.outputs.files
+    baseName 'r8_with_relocated_deps_11'
+    classifier = null
+    version = null
+    manifest {
+        attributes 'Main-Class': 'com.android.tools.r8.SwissArmyKnife'
+    }
+    from repackageSources11.outputs.files
+    from repackageDeps.outputs.files
+    configureRelocations(it)
+    exclude "META-INF/*.kotlin_module"
+}
+
 task r8WithoutDeps(type: ShadowJar) {
     from consolidatedLicense.outputs.files
     baseName 'r8_without_deps'
@@ -784,6 +827,46 @@
     }
 }
 
+def baseR8CommandLine11(args = []) {
+    // Execute r8 commands against a stable r8 with relocated dependencies.
+    def jdkDir = 'third_party/openjdk/jdk-11/'
+    def javaExecutable
+    if (OperatingSystem.current().isLinux()) {
+        javaExecutable = file(jdkDir + 'Linux/bin/java')
+    } else if (OperatingSystem.current().isMacOsX()) {
+        javaExecutable = file(jdkDir + 'Mac/bin/java')
+    } else {
+        javaExecutable = file(jdkDir + 'Windows/bin/java')
+    }
+    return [javaExecutable,
+            "-ea", "-jar", r8WithRelocatedDeps11.outputs.files[0]] + args
+}
+
+
+def r8CfCommandLine11(input, output, pgconf, args = [], libs = []) {
+    return baseR8CommandLine11([
+            "--classfile", "--release",
+            input,
+            "--output", output,
+            "--pg-conf", pgconf,
+            "--pg-map-output", output + ".map",
+            "--lib", "third_party/openjdk/openjdk-rt-1.8/rt.jar"
+    ] + args + libs.collectMany { ["--lib", it] })
+}
+
+def r8LibCreateTask11(name, pgConf, r8Task, output, args = [], libs = []) {
+    // Execute r8 11 commands against a stable r8 11 with relocated dependencies.
+    return tasks.create("r8Lib${name}", Exec) {
+        inputs.files ([pgConf, r8WithRelocatedDeps.outputs, r8Task.outputs])
+        outputs.file output
+        dependsOn downloadOpenJDKrt
+        dependsOn r8WithRelocatedDeps11
+        dependsOn r8Task
+        commandLine r8CfCommandLine11(r8Task.outputs.files[0], output, pgConf, args, libs)
+        workingDir = projectDir
+    }
+}
+
 task testJar(type: ShadowJar, dependsOn: testClasses) {
     baseName = "r8tests"
     from sourceSets.test.output
@@ -836,6 +919,22 @@
     outputs.file r8LibPath
 }
 
+task R8Lib11 {
+    def genRulesTask = generateR8LibKeepRules(
+            "Main11",
+            r8WithRelocatedDeps11,
+            testJar,
+            r8LibGeneratedKeepRulesPath)
+    dependsOn r8LibCreateTask(
+            "Main11",
+            "src/main/keep.txt",
+            r8WithRelocatedDeps11,
+            r8LibPath11,
+            ["--pg-conf", genRulesTask.outputs.files[0]]
+    ).dependsOn(genRulesTask)
+    outputs.file r8LibPath11
+}
+
 task R8LibNoDeps {
     def genRulesTask = generateR8LibKeepRules(
             "NoDeps",