Do not keep the DexFile field in Main between runs

Bug: 18486731

(cherry picked from commit bb404aa9c531c2e1d27b4f7c6d2b03c2b50fb871)

Change-Id: I048f6064cc49c78df21a1c42316761502a0a5366
diff --git a/dx/src/com/android/dx/command/dexer/Main.java b/dx/src/com/android/dx/command/dexer/Main.java
index 4a3d195..84baa93 100644
--- a/dx/src/com/android/dx/command/dexer/Main.java
+++ b/dx/src/com/android/dx/command/dexer/Main.java
@@ -224,6 +224,7 @@
      * @return 0 if success > 0 otherwise.
      */
     public static int run(Arguments arguments) throws IOException {
+
         // Reset the error count to start fresh.
         errors.set(0);
         // empty the list, so that  tools that load dx and keep it around
@@ -497,7 +498,7 @@
 
                 if (args.minimalMainDex) {
                     // start second pass directly in a secondary dex file.
-                    createDexFile();
+                    rotateDexFile();
                 }
 
                 // remaining files
@@ -572,10 +573,6 @@
     }
 
     private static void createDexFile() {
-        if (outputDex != null) {
-            dexOutputArrays.add(writeDex());
-        }
-
         outputDex = new DexFile(args.dexOptions);
 
         if (args.dumpWidth != 0) {
@@ -583,6 +580,14 @@
         }
     }
 
+    private static void rotateDexFile() {
+        if (outputDex != null) {
+            dexOutputArrays.add(writeDex());
+        }
+
+        createDexFile();
+    }
+
     /**
      * Processes one pathname element.
      *
@@ -718,7 +723,7 @@
             && ((maxMethodIdsInDex > args.maxNumberOfIdxPerDex) ||
                 (maxFieldIdsInDex > args.maxNumberOfIdxPerDex))) {
             DexFile completeDex = outputDex;
-            createDexFile();
+            rotateDexFile();
             assert  (completeDex.getMethodIds().items().size() <= numMethodIds +
                     MAX_METHOD_ADDED_DURING_DEX_CREATION) &&
                     (completeDex.getFieldIds().items().size() <= numFieldIds +