Delete temp files on exit and avoid kotlin daemon

Change-Id: I7474dcecb9f16756aa1e61efb409c5d19ab73956
diff --git a/gradle.properties b/gradle.properties
index a12fbef..0e9eb37 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -9,6 +9,7 @@
 
 org.gradle.jvmargs=-Xmx2048M -Dfile.encoding=UTF-8
 kotlin.daemon.jvmargs=-Xmx3g -Dkotlin.js.compiler.legacy.force_enabled=true
+kotlin.compiler.execution.strategy=in-process
 systemProp.file.encoding=UTF-8
 
 org.gradle.parallel=true
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FileChannelTest.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FileChannelTest.java
index 6c5c149..59bd5dc 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FileChannelTest.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FileChannelTest.java
@@ -107,6 +107,7 @@
     @SuppressWarnings("all")
     private static void instanceTest() throws IOException {
       Path tmp = Files.createTempFile("tmp", ".txt");
+      tmp.toFile().deleteOnExit();
       System.out.println(
           new FileInputStream(tmp.toFile()).getChannel() instanceof SeekableByteChannel);
       System.out.println(
@@ -120,6 +121,7 @@
     private static void fosTest() throws IOException {
       String toWrite = "The monkey eats...";
       Path tmp = Files.createTempFile("fos", ".txt");
+      tmp.toFile().deleteOnExit();
 
       ByteBuffer byteBuffer = ByteBuffer.wrap(toWrite.getBytes(StandardCharsets.UTF_8));
       FileOutputStream fos = new FileOutputStream(tmp.toFile());
@@ -138,6 +140,7 @@
 
     private static void fileChannelOpenLockTest() throws IOException {
       Path tmp = Files.createTempFile("lock", ".txt");
+      tmp.toFile().deleteOnExit();
       String contents = "Bananas!";
       Files.write(tmp, contents.getBytes(StandardCharsets.UTF_8));
       FileChannel fc = FileChannel.open(tmp, StandardOpenOption.READ);
@@ -149,6 +152,7 @@
 
     private static void fileChannelOpenTest() throws IOException {
       Path tmp = Files.createTempFile("a", ".txt");
+      tmp.toFile().deleteOnExit();
       String contents = "Bananas!";
       Files.write(tmp, contents.getBytes(StandardCharsets.UTF_8));
       FileChannel fc = FileChannel.open(tmp, StandardOpenOption.READ, StandardOpenOption.WRITE);
@@ -161,6 +165,7 @@
 
     private static void fileChannelOpenSetTest() throws IOException {
       Path tmp = Files.createTempFile("b", ".txt");
+      tmp.toFile().deleteOnExit();
       String contents = "Bananas!";
       Files.write(tmp, contents.getBytes(StandardCharsets.UTF_8));
       Set<OpenOption> options = new HashSet<>();
@@ -183,6 +188,7 @@
       String toWrite = "Hello World! ";
       String toWriteFIS = "Bye bye. ";
       Path tmp = Files.createTempFile("tmp", ".txt");
+      tmp.toFile().deleteOnExit();
       Files.write(tmp, (toWrite + toWriteFIS).getBytes(StandardCharsets.UTF_8));
 
       ByteBuffer byteBuffer = ByteBuffer.allocate(toWrite.length());
@@ -208,6 +214,7 @@
     private static void fisOwner() throws IOException {
       String toWrite = "Hello World! ";
       Path tmp = Files.createTempFile("tmp", ".txt");
+      tmp.toFile().deleteOnExit();
       Files.write(tmp, toWrite.getBytes(StandardCharsets.UTF_8));
 
       ByteBuffer byteBuffer = ByteBuffer.allocate(toWrite.length());
@@ -221,6 +228,7 @@
     private static void fisOwnerTryResources() throws IOException {
       String toWrite = "Hello World! ";
       Path tmp = Files.createTempFile("tmp", ".txt");
+      tmp.toFile().deleteOnExit();
       Files.write(tmp, toWrite.getBytes(StandardCharsets.UTF_8));
 
       ByteBuffer byteBuffer = ByteBuffer.allocate(toWrite.length());
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesBlogTest.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesBlogTest.java
index a154da3..4a59c63 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesBlogTest.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesBlogTest.java
@@ -74,15 +74,22 @@
   public static class TestClass {
 
     public static void main(String[] args) throws Throwable {
-      Path tempDirectory = Files.createTempDirectory("tempFile");
-      Path tempFile = tempDirectory.resolve("tempFile");
-      Files.write(tempFile, "first ".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
-      Files.write(tempFile, "second".getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
-      System.out.println("Content: " + Files.readAllLines(tempFile).get(0));
-      System.out.println("Size: " + Files.getAttribute(tempFile, "basic:size"));
-      System.out.println("Exists (before deletion): " + Files.exists(tempFile));
-      Files.deleteIfExists(tempFile);
-      System.out.println("Exists (after deletion): " + Files.exists(tempFile));
+      Path tempDirectory = null;
+      try {
+        tempDirectory = Files.createTempDirectory("tempFile");
+        Path tempFile = tempDirectory.resolve("tempFile");
+        Files.write(tempFile, "first ".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
+        Files.write(tempFile, "second".getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
+        System.out.println("Content: " + Files.readAllLines(tempFile).get(0));
+        System.out.println("Size: " + Files.getAttribute(tempFile, "basic:size"));
+        System.out.println("Exists (before deletion): " + Files.exists(tempFile));
+        Files.deleteIfExists(tempFile);
+        System.out.println("Exists (after deletion): " + Files.exists(tempFile));
+      } finally {
+        if (tempDirectory != null) {
+          Files.deleteIfExists(tempDirectory);
+        }
+      }
     }
   }
 }
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesCreateTest.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesCreateTest.java
index 2a301f1..a1faf48 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesCreateTest.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesCreateTest.java
@@ -139,7 +139,9 @@
     }
 
     public static void main(String[] args) throws Throwable {
-      Path root = Files.createTempDirectory("tmp_test");
+      Path root = null;
+      try {
+        root = Files.createTempDirectory("tmp_test");
       Files.createDirectories(root.resolve("ind1s/dir"));
       Files.createDirectories(root.resolve("ind2s/dir"), getFileAttribute());
       try {
@@ -201,16 +203,32 @@
       Files.createFile(root.resolve("f3.txt"));
       Files.createFile(root.resolve("f4.txt"), getFileAttribute());
 
-      Files.walk(root)
-          .sorted(Comparator.reverseOrder())
-          .map(
-              f -> {
-                if (f != root) {
-                  System.out.println(f.subpath(2, f.getNameCount()));
-                }
-                return f.toFile();
-              })
-          .forEach(File::delete);
+        Path finalRoot = root;
+        Files.walk(root)
+            .sorted(Comparator.reverseOrder())
+            .map(
+                f -> {
+                  if (f != finalRoot) {
+                    System.out.println(f.subpath(2, f.getNameCount()));
+                  }
+                  return f.toFile();
+                })
+            .forEach(File::delete);
+      } finally {
+        if (root != null) {
+          deleteDirectory(root.toFile());
+        }
+      }
+    }
+
+    private static void deleteDirectory(java.io.File directoryToBeDeleted) {
+      java.io.File[] allContents = directoryToBeDeleted.listFiles();
+      if (allContents != null) {
+        for (java.io.File file : allContents) {
+          deleteDirectory(file);
+        }
+      }
+      directoryToBeDeleted.delete();
     }
 
     public static FileAttribute<Set<PosixFilePermission>> getFileAttribute() {
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesTest.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesTest.java
index 347e0a9..85d6290 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesTest.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesTest.java
@@ -141,6 +141,7 @@
 
     public static void main(String[] args) throws Throwable {
       Path path = Files.createTempFile("example", ".txt");
+      path.toFile().deleteOnExit();
       readWriteThroughFilesAPI(path);
       readThroughFileChannelAPI(path);
       Files.setAttribute(path, "basic:lastModifiedTime", FileTime.from(Instant.EPOCH));
@@ -220,7 +221,9 @@
 
     private static void pathGeneric() throws IOException {
       Path tmpDict = Files.createTempDirectory("tmpDict");
+      tmpDict.toFile().deleteOnExit();
       Path tmpFile = Files.createFile(tmpDict.resolve("tmpFile"));
+      tmpFile.toFile().deleteOnExit();
       Iterator<Path> iterator = tmpDict.iterator();
       System.out.println(iterator.next());
       Iterable<Path> rootDirectories = tmpFile.getFileSystem().getRootDirectories();
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesVisitTest.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesVisitTest.java
index 50f1721..7b5eb9a 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesVisitTest.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesVisitTest.java
@@ -105,10 +105,26 @@
     private static Path root;
 
     public static void main(String[] args) throws IOException {
-      createDirStructure();
-      findTest();
-      listTest();
-      walkTest();
+      try {
+        createDirStructure();
+        findTest();
+        listTest();
+        walkTest();
+      } finally {
+        if (root != null) {
+          deleteDirectory(root.toFile());
+        }
+      }
+    }
+
+    private static void deleteDirectory(java.io.File directoryToBeDeleted) {
+      java.io.File[] allContents = directoryToBeDeleted.listFiles();
+      if (allContents != null) {
+        for (java.io.File file : allContents) {
+          deleteDirectory(file);
+        }
+      }
+      directoryToBeDeleted.delete();
     }
 
     /** Creates the following structure root | f1 | f2 | f3 | innerDir | f4 */
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesWatchEventTest.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesWatchEventTest.java
index a78018e..95d50fa 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesWatchEventTest.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesWatchEventTest.java
@@ -94,33 +94,45 @@
   public static class TestClass {
 
     public static void main(String[] args) throws IOException, InterruptedException {
-      Path dir = Files.createTempDirectory("tmpDictWatch");
-      FileSystem fs = FileSystems.getDefault();
+      Path dir = null;
+      try {
+        dir = Files.createTempDirectory("tmpDictWatch");
+        FileSystem fs = FileSystems.getDefault();
 
-      try (WatchService watcher = fs.newWatchService()) {
-        WatchKey myKey = dir.register(watcher, ENTRY_CREATE);
-        System.out.println(myKey.isValid());
-        System.out.println(myKey.watchable().equals(dir));
+        try (WatchService watcher = fs.newWatchService()) {
+          WatchKey myKey = dir.register(watcher, ENTRY_CREATE);
+          System.out.println(myKey.isValid());
+          System.out.println(myKey.watchable().equals(dir));
 
-        Path file = dir.resolve("foo");
-        Files.createFile(file);
+          Path file = dir.resolve("foo");
+          Files.createFile(file);
 
-        WatchKey key = watcher.take();
-        System.out.println(key.equals(myKey));
+          WatchKey key = watcher.take();
+          System.out.println(key.equals(myKey));
 
-        WatchEvent<?> event = myKey.pollEvents().iterator().next();
-        System.out.println(event.kind());
-        System.out.println(event.kind().type().getSimpleName());
-        System.out.println(event.context());
-        System.out.println(((Path) event.context()).getFileName());
+          WatchEvent<?> event = myKey.pollEvents().iterator().next();
+          System.out.println(event.kind());
+          System.out.println(event.kind().type().getSimpleName());
+          System.out.println(event.context());
+          System.out.println(((Path) event.context()).getFileName());
 
-        System.out.println(myKey.reset());
-        Files.delete(file);
-      } catch (UnsupportedOperationException e) {
-        System.out.println(e.getClass() + " :: " + e.getMessage());
+          System.out.println(myKey.reset());
+          Files.delete(file);
+        } catch (UnsupportedOperationException e) {
+          System.out.println(e.getClass() + " :: " + e.getMessage());
+        }
+      } finally {
+        if (dir != null) {
+          try {
+            Files.deleteIfExists(dir.resolve("foo"));
+          } catch (Exception ignored) {
+          }
+          try {
+            Files.deleteIfExists(dir);
+          } catch (Exception ignored) {
+          }
+        }
       }
-
-      Files.delete(dir);
     }
   }
 }
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/kotlin/Blog.kt b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/kotlin/Blog.kt
index bb671dc..50da31d 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/kotlin/Blog.kt
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/kotlin/Blog.kt
@@ -10,12 +10,16 @@
 
 fun main() {
   val tempDirectory = Files.createTempDirectory("tempFile")
-  val tempFile = tempDirectory.resolve("tempFile")
-  Files.write(tempFile, "first ".toByteArray(StandardCharsets.UTF_8), StandardOpenOption.CREATE)
-  Files.write(tempFile, "second".toByteArray(StandardCharsets.UTF_8), StandardOpenOption.APPEND)
-  println("Content: " + Files.readAllLines(tempFile).get(0))
-  println("Size: " + Files.getAttribute(tempFile, "basic:size"))
-  println("Exists (before deletion): " + Files.exists(tempFile))
-  Files.deleteIfExists(tempFile)
-  println("Exists (after deletion): " + Files.exists(tempFile))
+  try {
+    val tempFile = tempDirectory.resolve("tempFile")
+    Files.write(tempFile, "first ".toByteArray(StandardCharsets.UTF_8), StandardOpenOption.CREATE)
+    Files.write(tempFile, "second".toByteArray(StandardCharsets.UTF_8), StandardOpenOption.APPEND)
+    println("Content: " + Files.readAllLines(tempFile).get(0))
+    println("Size: " + Files.getAttribute(tempFile, "basic:size"))
+    println("Exists (before deletion): " + Files.exists(tempFile))
+    Files.deleteIfExists(tempFile)
+    println("Exists (after deletion): " + Files.exists(tempFile))
+  } finally {
+    Files.deleteIfExists(tempDirectory)
+  }
 }
diff --git a/tools/utils.py b/tools/utils.py
index b72a242..12c441d 100644
--- a/tools/utils.py
+++ b/tools/utils.py
@@ -577,7 +577,7 @@
 
 class TempDir(object):
 
-    def __init__(self, prefix='', delete=True):
+    def __init__(self, prefix='r8-tmp-', delete=True):
         self._temp_dir = None
         self._prefix = prefix
         self._delete = delete