Reapply "Add a separate tool for computing the main-dex list"

This reverts commit 34247e0126ab7b46c3c7d8e1fc491ca1ad7cfb84
and fixes merge issues.

Bug: 65043103
Change-Id: Ie12e031390a735f6ca133739ef2c2bdabc130353
diff --git a/build.gradle b/build.gradle
index 7362238..82feb00 100644
--- a/build.gradle
+++ b/build.gradle
@@ -435,6 +435,20 @@
     }
 }
 
+task maindex(type: Jar) {
+    from sourceSets.main.output
+    baseName 'maindex'
+    manifest {
+      attributes 'Main-Class': 'com.android.tools.r8.GenerateMainDexList'
+    }
+    // In order to build without dependencies, pass the exclude_deps property using:
+    // gradle -Pexclude_deps maindex
+    if (!project.hasProperty('exclude_deps')) {
+        // Also include dependencies
+        from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
+    }
+}
+
 task ExtractMarker(type: Jar) {
     from sourceSets.main.output
     baseName 'extractmarker'