Revert "Revert "Revert "Remove d8-compat-dx dependencies"""

This reverts commit f0b23fb9cbc2e112ea2a1157b28352303004822d.

Reason for revert: vogar fails
Exempt-From-Owner-Approval: revert

Change-Id: I67f64de942372e4ee36de77f3428cadbe1f0e9cf
diff --git a/Android.mk b/Android.mk
index 2eab8eb..9fffc8a 100644
--- a/Android.mk
+++ b/Android.mk
@@ -35,7 +35,7 @@
 
 LOCAL_ADDITIONAL_DEPENDENCIES := \
   $(HOST_OUT_EXECUTABLES)/dx \
-  $(HOST_OUT_EXECUTABLES)/d8 \
+  $(HOST_OUT_EXECUTABLES)/d8-compat-dx \
   $(HOST_OUT_JAVA_LIBRARIES)/desugar.jar
 
 # Vogar uses android.jar.
diff --git a/src/vogar/android/AndroidSdk.java b/src/vogar/android/AndroidSdk.java
index f6d0231..f59c9b9 100644
--- a/src/vogar/android/AndroidSdk.java
+++ b/src/vogar/android/AndroidSdk.java
@@ -47,7 +47,7 @@
  */
 public class AndroidSdk {
 
-    private static final String D8_COMMAND_NAME = "d8";
+    private static final String D8_COMMAND_NAME = "d8-compat-dx";
     private static final String DX_COMMAND_NAME = "dx";
     private static final String ARBITRARY_BUILD_TOOL_NAME = D8_COMMAND_NAME;
 
@@ -321,51 +321,37 @@
         switch (dexer) {
             case DX:
                 builder.args(DX_COMMAND_NAME);
-                builder.args("-JXms16M").args("-JXmx1536M");
-                builder.args("--min-sdk-version=" + language.getMinApiLevel());
-                if (multidex) {
-                    builder.args("--multi-dex");
-                }
-                builder.args("--dex")
-                    .args("--output=" + output)
-                    .args("--core-library")
-                    .args(desugarOutputFilePaths);
-                builder.execute();
                 break;
             case D8:
-                List<String> sanitizedDesugarOutputFilePaths;
-                try {
-                    sanitizedDesugarOutputFilePaths = removeDexFilesForD8(desugarOutputFilePaths);
-                } catch (IOException e) {
-                    throw new RuntimeException("Error while removing dex files from archive", e);
-                }
                 builder.args(D8_COMMAND_NAME);
-                builder.args("-JXms16M").args("-JXmx1536M");
-                builder
-                    .args("--min-api").args(language.getMinApiLevel())
-                    .args("--output").args(output)
-                    .args(sanitizedDesugarOutputFilePaths);
-                builder.execute();
-                if (dexer == Dexer.D8 && output.toString().endsWith(".jar")) {
-                    try {
-                        fixD8JarOutput(output, desugarOutputFilePaths);
-                    } catch (IOException e) {
-                        throw new RuntimeException("Error while fixing d8 output", e);
-                    }
-                }
                 break;
-            default:
-                throw new RuntimeException("Unsupported dexer: " + dexer);
-
         }
+        builder.args("-JXms16M")
+                .args("-JXmx1536M")
+                .args("--min-sdk-version=" + language.getMinApiLevel());
+        if (multidex) {
+            builder.args("--multi-dex");
+        }
+        builder.args("--dex")
+                .args("--output=" + output)
+                .args("--core-library")
+                .args(desugarOutputFilePaths);
+        builder.execute();
 
+        if (dexer == Dexer.D8 && output.toString().endsWith(".jar")) {
+            try {
+                fixD8JarOutput(output, desugarOutputFilePaths);
+            } catch (IOException e) {
+                throw new RuntimeException("Error while fixing d8 output", e);
+            }
+        }
         dexCache.insert(cacheKey, output);
     }
 
     /**
      * Produces an output file like dx does. dx generates jar files containing all resources present
      * in the input files.
-     * d8 only produces a jar file containing dex and none of the input resources, and
+     * d8-compat-dx only produces a jar file containing dex and none of the input resources, and
      * will produce no file at all if there are no .class files to process.
      */
     private static void fixD8JarOutput(File output, List<String> inputs) throws IOException {
@@ -392,41 +378,14 @@
         }
     }
 
-    /**
-      * Removes DEX files from an archive and preserve the rest.
-      */
-    private List<String> removeDexFilesForD8(List<String> fileNames) throws IOException {
-        byte[] buffer = new byte[4096];
-        List<String> processedFiles = new ArrayList<>(fileNames.size());
-        for (String inputFileName : fileNames) {
-            String jarExtension = ".jar";
-            if (inputFileName.endsWith(jarExtension)) {
-                inputFileName =
-                    inputFileName.substring(0, inputFileName.length() - jarExtension.length());
-            }
-            String outputFileName = inputFileName + "-d8.jar";
-            try (JarOutputStream outputJar =
-                    new JarOutputStream(new FileOutputStream(outputFileName))) {
-                copyJarContentExcludingFiles(buffer, inputFileName, outputJar, ".dex");
-            }
-            processedFiles.add(outputFileName);
-        }
-        return processedFiles;
-    }
-
     private static void copyJarContentExcludingClassFiles(byte[] buffer, String inputJarName,
             JarOutputStream outputJar) throws IOException {
-        copyJarContentExcludingFiles(buffer, inputJarName, outputJar, ".class");
-    }
-
-    private static void copyJarContentExcludingFiles(byte[] buffer, String inputJarName,
-            JarOutputStream outputJar, String extensionToExclude) throws IOException {
 
         try (JarInputStream inputJar = new JarInputStream(new FileInputStream(inputJarName))) {
             for (JarEntry entry = inputJar.getNextJarEntry();
                     entry != null;
                     entry = inputJar.getNextJarEntry()) {
-                if (entry.getName().endsWith(extensionToExclude)) {
+                if (entry.getName().endsWith(".class")) {
                     continue;
                 }