Add additional check for valid Zip file before multidex install

Bug: 11895788
Change-Id: Id4f6e5b09be809eeb29367bebe78c03e49864cbf
diff --git a/library/src/android/support/multidex/MultiDex.java b/library/src/android/support/multidex/MultiDex.java
index 064c66a..7a99142 100644
--- a/library/src/android/support/multidex/MultiDex.java
+++ b/library/src/android/support/multidex/MultiDex.java
@@ -151,14 +151,18 @@
                 }
 
                 File dexDir = new File(context.getFilesDir(), SECONDARY_FOLDER_NAME);
-                List<File> files = MultiDexExtractor.load(applicationInfo, dexDir);
-                if (!files.isEmpty()) {
-                    if (Build.VERSION.SDK_INT >= 19) {
-                        V19.install(loader, files, dexDir);
-                    } else if (Build.VERSION.SDK_INT >= 14) {
-                        V14.install(loader, files, dexDir);
+                List<File> files = MultiDexExtractor.load(applicationInfo, dexDir, false);
+                if (checkValidZipFiles(files)) {
+                    installSecondaryDexes(loader, dexDir, files);
+                } else {
+                    Log.w(TAG, "Files were not valid zip files.  Forcing a reload.");
+                    // Try again, but this time force a reload of the zip file.
+                    files = MultiDexExtractor.load(applicationInfo, dexDir, true);
+                    if (checkValidZipFiles(files)) {
+                        installSecondaryDexes(loader, dexDir, files);
                     } else {
-                        V4.install(loader, files);
+                        // Second time didn't work, give up
+                        throw new RuntimeException("Zip files were not valid.");
                     }
                 }
             }
@@ -169,6 +173,33 @@
         }
     }
 
+    private static void installSecondaryDexes(ClassLoader loader, File dexDir, List<File> files)
+            throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException,
+            InvocationTargetException, NoSuchMethodException, IOException {
+        if (!files.isEmpty()) {
+            if (Build.VERSION.SDK_INT >= 19) {
+                V19.install(loader, files, dexDir);
+            } else if (Build.VERSION.SDK_INT >= 14) {
+                V14.install(loader, files, dexDir);
+            } else {
+                V4.install(loader, files);
+            }
+        }
+    }
+
+    /**
+     * Returns whether all files in the list are valid zip files.  If {@code files} is empty, then
+     * returns true.
+     */
+    private static boolean checkValidZipFiles(List<File> files) {
+        for (File file : files) {
+            if (!MultiDexExtractor.verifyZipFile(file)) {
+                return false;
+            }
+        }
+        return true;
+    }
+
     /**
      * Locates a given field anywhere in the class inheritance hierarchy.
      *