Modify test of r8lib with no dependencies to use generated keep rules

Also pick the right set of generated tests to run by copy to a
separate directory.

Change-Id: I2b1bdf437a9003d26d8d1c3e8cd07aea684e4f95
diff --git a/build.gradle b/build.gradle
index 0835d5d..671ef8a 100644
--- a/build.gradle
+++ b/build.gradle
@@ -257,6 +257,7 @@
 def r8LibPath = "$buildDir/libs/r8lib.jar"
 def r8LibClassesPath = "$buildDir/classes/r8lib"
 def r8LibGeneratedKeepRulesPath = "$buildDir/generated/keep.txt"
+def r8LibTestPath = "$buildDir/classes/r8libtest"
 
 def osString = OperatingSystem.current().isLinux() ? "linux" :
         OperatingSystem.current().isMacOsX() ? "mac" : "windows"
@@ -700,6 +701,7 @@
 
 task R8Lib {
     dependsOn r8LibCreateTask("Main", "src/main/keep.txt", R8NoManifest, r8LibPath)
+    outputs.file r8LibPath
 }
 
 task CompatDxLib {
@@ -1374,24 +1376,37 @@
     workingDir = projectDir
 }
 
-task configureTestForR8Lib(type: Copy) {
-    def r8LibTask;
+def getR8LibTask() {
     if (project.hasProperty('r8lib')) {
-        r8LibTask = R8Lib
+        return R8Lib
     } else if (project.hasProperty('r8lib_no_deps')) {
-        r8LibTask = R8LibNoDeps
+        return R8LibNoDeps
     }
+    return null
+}
+
+task copyR8LibForTests(type: Copy) {
+    def r8LibTask = getR8LibTask()
     if (r8LibTask != null) {
         dependsOn r8LibTask
         from(zipTree(r8LibPath))
         into(r8LibClassesPath)
-        outputs.file r8LibClassesPath
-        doLast {
-            test {
-                classpath = (classpath - sourceSets.main.output) + files(r8LibClassesPath)
-            }
-        }
     }
+    outputs.dir r8LibClassesPath
+}
+
+task configureTestForR8Lib(type: Copy) {
+    dependsOn testClasses
+    dependsOn copyR8LibForTests
+    // Setting classpath triggers a scan for test files in $buildDir/classes/test that finds all
+    // tests and not just the ones under $test/com/android/tools/r8. That is generally not
+    // something we want so we just copy the desired test files to $r8LibTestPath.
+    if (getR8LibTask() != null) {
+        // Cannot use sourceSets.test.output here since it will copy all tests.
+        from("$buildDir/classes/test/com/android/tools/r8")
+        into(r8LibTestPath + "/com/android/tools/r8")
+    }
+    outputs.dir r8LibTestPath
 }
 
 test {
@@ -1489,7 +1504,13 @@
     if (project.hasProperty('aosp_jar')) {
         dependsOn AospJarTest
     }
-
+    if (project.hasProperty('r8lib') || project.hasProperty('r8lib_no_deps')) {
+        dependsOn configureTestForR8Lib
+        classpath =
+                (sourceSets.test.runtimeClasspath - sourceSets.main.output) +
+                        files(r8LibClassesPath)
+        testClassesDir = new File(r8LibTestPath)
+    }
     if (OperatingSystem.current().isLinux()
             || OperatingSystem.current().isMacOsX()
             || OperatingSystem.current().isWindows()) {
@@ -1516,9 +1537,6 @@
         logger.lifecycle("WARNING: Testing in not supported on your platform. Testing is only fully supported on " +
             "Linux and partially supported on Mac OS and Windows. Art does not run on other platforms.")
     }
-    if (project.hasProperty('r8lib') || project.hasProperty('r8lib_no_deps')) {
-        dependsOn configureTestForR8Lib
-    }
 }
 
 // The Art tests we use for R8 are pre-build and downloaded from Google Cloud Storage.