Archive R8'd jars.

Change-Id: Id8519db16b5028cdd99d9b12a88358ba527453e1
diff --git a/build.gradle b/build.gradle
index 0a4f374..ec2ac2d 100644
--- a/build.gradle
+++ b/build.gradle
@@ -582,6 +582,55 @@
     }
 }
 
+def r8CfCommandLine(input, output, pgconf) {
+    return ["java", "-ea", "-jar", R8.outputs.files[0],
+        "--classfile", "--release",
+        input,
+        "--lib", "third_party/openjdk/openjdk-rt-1.8/rt.jar",
+        "--output", output,
+        "--pg-conf", pgconf]
+}
+
+task R8R8(type: Exec) {
+    def pgconf = "pgconfs/r8.cfg"
+    def output = "build/libs/r8-r8.jar"
+    inputs.files files(pgconf, "build/libs/r8.jar")
+    outputs.file output
+    dependsOn R8
+    commandLine r8CfCommandLine(R8.outputs.files[0], output, pgconf)
+    workingDir = projectDir
+}
+
+task D8R8(type: Exec) {
+    def pgconf = "pgconfs/d8.cfg"
+    def output = "build/libs/d8-r8.jar"
+    inputs.files files(pgconf, "build/libs/d8.jar")
+    outputs.file output
+    dependsOn D8
+    commandLine r8CfCommandLine(R8.outputs.files[0], output, pgconf)
+    workingDir = projectDir
+}
+
+task CompatDxR8(type: Exec) {
+    def pgconf = "pgconfs/compatdx.cfg"
+    def output = "build/libs/compatdx-r8.jar"
+    inputs.files files(pgconf, "build/libs/compatdx.jar")
+    outputs.file output
+    dependsOn CompatDx
+    commandLine r8CfCommandLine(R8.outputs.files[0], output, pgconf)
+    workingDir = projectDir
+}
+
+task CompatProguardR8(type: Exec) {
+    def pgconf = "pgconfs/compatproguard.cfg"
+    def output = "build/libs/compatproguard-r8.jar"
+    inputs.files files(pgconf, "build/libs/compatproguard.jar")
+    outputs.file output
+    dependsOn CompatProguard
+    commandLine r8CfCommandLine(R8.outputs.files[0], output, pgconf)
+    workingDir = projectDir
+}
+
 task sourceJar(type: Jar, dependsOn: classes) {
     classifier = 'src'
     from sourceSets.main.allSource
diff --git a/pgconfs/compatdx.cfg b/pgconfs/compatdx.cfg
new file mode 100644
index 0000000..51b401c
--- /dev/null
+++ b/pgconfs/compatdx.cfg
@@ -0,0 +1,5 @@
+# Copyright (c) 2018, 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.
+
+-keep public class com.android.tools.r8.compatdx.CompatDx { public static void main(java.lang.String[]); }
diff --git a/pgconfs/compatproguard.cfg b/pgconfs/compatproguard.cfg
new file mode 100644
index 0000000..f508bea
--- /dev/null
+++ b/pgconfs/compatproguard.cfg
@@ -0,0 +1,5 @@
+# Copyright (c) 2018, 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.
+
+-keep public class com.android.tools.r8.compatproguard.CompatProguard { public static void main(java.lang.String[]); }
diff --git a/pgconfs/d8.cfg b/pgconfs/d8.cfg
new file mode 100644
index 0000000..2d74e47
--- /dev/null
+++ b/pgconfs/d8.cfg
@@ -0,0 +1,6 @@
+# Copyright (c) 2018, 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.
+
+-keep @com.android.tools.r8.Keep class * { public *; }
+-keep @com.android.tools.r8.KeepForSubclassing class * { public *; protected *; }
diff --git a/pgconfs/r8.cfg b/pgconfs/r8.cfg
new file mode 100644
index 0000000..d1ae99b
--- /dev/null
+++ b/pgconfs/r8.cfg
@@ -0,0 +1,7 @@
+# Copyright (c) 2018, 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.
+
+-keep public class com.android.tools.r8.SwissArmyKnife { public static void main(java.lang.String[]); }
+-keep @com.android.tools.r8.Keep class * { public *; }
+-keep @com.android.tools.r8.KeepForSubclassing class * { public *; protected *; }
diff --git a/src/main/java/com/android/tools/r8/ApiLevelException.java b/src/main/java/com/android/tools/r8/ApiLevelException.java
index 6d110a4..0ad75c7 100644
--- a/src/main/java/com/android/tools/r8/ApiLevelException.java
+++ b/src/main/java/com/android/tools/r8/ApiLevelException.java
@@ -6,9 +6,7 @@
 import com.android.tools.r8.errors.CompilationError;
 import com.android.tools.r8.utils.AndroidApiLevel;
 
-/**
- * Exception to signal features that are not supported until a given API level.
- */
+/** Exception to signal features that are not supported until a given API level. */
 public class ApiLevelException extends CompilationError {
 
   public ApiLevelException(
diff --git a/src/main/java/com/android/tools/r8/errors/CompilationError.java b/src/main/java/com/android/tools/r8/errors/CompilationError.java
index 1104b29..466bf8a 100644
--- a/src/main/java/com/android/tools/r8/errors/CompilationError.java
+++ b/src/main/java/com/android/tools/r8/errors/CompilationError.java
@@ -9,9 +9,9 @@
 
 /**
  * Exception to signal an compilation error.
- * <p>
- * This is always an expected error and considered a user input issue. A user-understandable message
- * must be provided.
+ *
+ * <p>This is always an expected error and considered a user input issue. A user-understandable
+ * message must be provided.
  */
 public class CompilationError extends RuntimeException implements Diagnostic {
 
diff --git a/src/main/java/com/android/tools/r8/utils/ExceptionDiagnostic.java b/src/main/java/com/android/tools/r8/utils/ExceptionDiagnostic.java
index 99149ff..3e4a44d 100644
--- a/src/main/java/com/android/tools/r8/utils/ExceptionDiagnostic.java
+++ b/src/main/java/com/android/tools/r8/utils/ExceptionDiagnostic.java
@@ -4,6 +4,7 @@
 
 package com.android.tools.r8.utils;
 
+import com.android.tools.r8.Keep;
 import com.android.tools.r8.ResourceException;
 import com.android.tools.r8.origin.Origin;
 import com.android.tools.r8.position.Position;
@@ -11,6 +12,7 @@
 import java.nio.file.FileAlreadyExistsException;
 import java.nio.file.NoSuchFileException;
 
+@Keep
 public class ExceptionDiagnostic extends DiagnosticWithThrowable {
 
   private final Origin origin;
diff --git a/tools/archive.py b/tools/archive.py
index 07f138a..0fa811b 100755
--- a/tools/archive.py
+++ b/tools/archive.py
@@ -88,7 +88,10 @@
   shutil.copyfile(utils.R8_JAR, utils.R8_EXCLUDE_DEPS_JAR)
 
   # Ensure all archived artifacts has been built before archiving.
-  gradle.RunGradle([utils.D8, utils.R8, utils.COMPATDX, utils.COMPATPROGUARD])
+  # The target tasks postfixed by 'r8' depend on the actual target task so
+  # building it invokes the original task first.
+  gradle.RunGradle(map((lambda t: t + 'r8'),
+    [utils.D8, utils.R8, utils.COMPATDX, utils.COMPATPROGUARD]))
   version = GetVersion()
   is_master = IsMaster(version)
   if is_master:
@@ -107,12 +110,12 @@
           'releaser=go/r8bot (' + os.environ.get('BUILDBOT_SLAVENAME') + ')\n')
       version_writer.write('version-file.version.code=1\n')
 
-    for file in [utils.D8_JAR,
-                 utils.R8_JAR,
+    for file in [utils.D8_JAR, utils.D8R8_JAR,
+                 utils.R8_JAR, utils.R8R8_JAR,
                  utils.R8_SRC_JAR,
                  utils.R8_EXCLUDE_DEPS_JAR,
-                 utils.COMPATDX_JAR,
-                 utils.COMPATPROGUARD_JAR,
+                 utils.COMPATDX_JAR, utils.COMPATDXR8_JAR,
+                 utils.COMPATPROGUARD_JAR, utils.COMPATPROGUARDR8_JAR,
                  utils.MAVEN_ZIP,
                  utils.GENERATED_LICENSE]:
       file_name = os.path.basename(file)
diff --git a/tools/update_prebuilds_in_android.py b/tools/update_prebuilds_in_android.py
index 7635ba6..395a4b4 100755
--- a/tools/update_prebuilds_in_android.py
+++ b/tools/update_prebuilds_in_android.py
@@ -35,7 +35,8 @@
     copyfile(src, dest)
 
 def copy_jar_targets(root, target_root):
-  srcs = map((lambda t: t + '.jar'), JAR_TARGETS)
+  # With the '-r8' postfix we're using the R8-processed jars.
+  srcs = map((lambda t: t + '-r8.jar'), JAR_TARGETS)
   dests = map((lambda t: t + '-master.jar'), JAR_TARGETS)
   copy_targets(root, target_root, srcs, dests)
 
@@ -61,12 +62,13 @@
   args = parse_arguments()
   target_root = args.android_root[0]
   if args.commit_hash == None and args.version == None:
-    gradle.RunGradle(JAR_TARGETS)
+    gradle.RunGradle(map((lambda t: t + 'r8'), JAR_TARGETS))
     copy_jar_targets(utils.LIBS, target_root)
     copy_other_targets(utils.GENERATED_LICENSE_DIR, target_root)
   else:
     assert args.commit_hash == None or args.version == None
-    targets = map((lambda t: t + '.jar'), JAR_TARGETS) + OTHER_TARGETS
+    # With the '-r8' postfix we're using the R8-processed jars.
+    targets = map((lambda t: t + '-r8.jar'), JAR_TARGETS) + OTHER_TARGETS
     with utils.TempDir() as root:
       for target in targets:
         if args.commit_hash:
diff --git a/tools/utils.py b/tools/utils.py
index 282ddfa..18f8870 100644
--- a/tools/utils.py
+++ b/tools/utils.py
@@ -35,11 +35,15 @@
 COMPATPROGUARD = 'compatproguard'
 
 D8_JAR = os.path.join(LIBS, 'd8.jar')
+D8R8_JAR = os.path.join(LIBS, 'd8-r8.jar')
 R8_JAR = os.path.join(LIBS, 'r8.jar')
+R8R8_JAR = os.path.join(LIBS, 'r8-r8.jar')
 R8_SRC_JAR = os.path.join(LIBS, 'r8-src.jar')
 R8_EXCLUDE_DEPS_JAR = os.path.join(LIBS, 'r8-exclude-deps.jar')
 COMPATDX_JAR = os.path.join(LIBS, 'compatdx.jar')
+COMPATDXR8_JAR = os.path.join(LIBS, 'compatdx-r8.jar')
 COMPATPROGUARD_JAR = os.path.join(LIBS, 'compatproguard.jar')
+COMPATPROGUARDR8_JAR = os.path.join(LIBS, 'compatproguard-r8.jar')
 MAVEN_ZIP = os.path.join(LIBS, 'r8.zip')
 GENERATED_LICENSE = os.path.join(GENERATED_LICENSE_DIR, 'LICENSE')
 RT_JAR = os.path.join(REPO_ROOT, 'third_party/openjdk/openjdk-rt-1.8/rt.jar')