Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.support.multidex; |
| 18 | |
Maurice Chu | 1f8c349 | 2013-11-20 17:14:31 -0800 | [diff] [blame] | 19 | import android.content.pm.ApplicationInfo; |
| 20 | import android.util.Log; |
| 21 | |
Maurice Chu | a159fd5 | 2013-11-28 12:59:37 -0800 | [diff] [blame^] | 22 | import java.io.BufferedOutputStream; |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 23 | import java.io.Closeable; |
| 24 | import java.io.File; |
Yohann Roussel | d9eda55 | 2013-11-12 18:13:55 +0100 | [diff] [blame] | 25 | import java.io.FileFilter; |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 26 | import java.io.FileNotFoundException; |
| 27 | import java.io.FileOutputStream; |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 28 | import java.io.IOException; |
| 29 | import java.io.InputStream; |
| 30 | import java.util.ArrayList; |
| 31 | import java.util.List; |
| 32 | import java.util.zip.ZipEntry; |
Maurice Chu | f6d1f23 | 2013-11-27 12:56:14 -0800 | [diff] [blame] | 33 | import java.util.zip.ZipException; |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 34 | import java.util.zip.ZipFile; |
| 35 | import java.util.zip.ZipOutputStream; |
| 36 | |
| 37 | /** |
| 38 | * Exposes application secondary dex files as files in the application data |
| 39 | * directory. |
| 40 | */ |
| 41 | final class MultiDexExtractor { |
| 42 | |
| 43 | private static final String TAG = MultiDex.TAG; |
| 44 | |
| 45 | /** |
| 46 | * We look for additional dex files named {@code classes2.dex}, |
| 47 | * {@code classes3.dex}, etc. |
| 48 | */ |
| 49 | private static final String DEX_PREFIX = "classes"; |
| 50 | private static final String DEX_SUFFIX = ".dex"; |
| 51 | |
| 52 | private static final String EXTRACTED_NAME_EXT = ".classes"; |
| 53 | private static final String EXTRACTED_SUFFIX = ".zip"; |
Maurice Chu | f6d1f23 | 2013-11-27 12:56:14 -0800 | [diff] [blame] | 54 | private static final int MAX_EXTRACT_ATTEMPTS = 3; |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 55 | |
| 56 | private static final int BUFFER_SIZE = 0x4000; |
| 57 | |
| 58 | /** |
| 59 | * Extracts application secondary dexes into files in the application data |
| 60 | * directory. |
| 61 | * |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 62 | * @return a list of files that were created. The list may be empty if there |
| 63 | * are no secondary dex files. |
| 64 | * @throws IOException if encounters a problem while reading or writing |
| 65 | * secondary dex files |
| 66 | */ |
Yohann Roussel | 52eafa0 | 2013-11-21 11:46:53 +0100 | [diff] [blame] | 67 | static List<File> load(ApplicationInfo applicationInfo, File dexDir) |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 68 | throws IOException { |
| 69 | |
Yohann Roussel | d9eda55 | 2013-11-12 18:13:55 +0100 | [diff] [blame] | 70 | File sourceApk = new File(applicationInfo.sourceDir); |
| 71 | long lastModified = sourceApk.lastModified(); |
| 72 | String extractedFilePrefix = sourceApk.getName() |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 73 | + EXTRACTED_NAME_EXT; |
| 74 | |
Yohann Roussel | d9eda55 | 2013-11-12 18:13:55 +0100 | [diff] [blame] | 75 | prepareDexDir(dexDir, extractedFilePrefix, lastModified); |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 76 | |
| 77 | final List<File> files = new ArrayList<File>(); |
| 78 | ZipFile apk = new ZipFile(applicationInfo.sourceDir); |
| 79 | try { |
| 80 | |
| 81 | int secondaryNumber = 2; |
| 82 | |
| 83 | ZipEntry dexFile = apk.getEntry(DEX_PREFIX + secondaryNumber + DEX_SUFFIX); |
| 84 | while (dexFile != null) { |
| 85 | String fileName = extractedFilePrefix + secondaryNumber + EXTRACTED_SUFFIX; |
| 86 | File extractedFile = new File(dexDir, fileName); |
| 87 | files.add(extractedFile); |
| 88 | |
| 89 | if (!extractedFile.isFile()) { |
Maurice Chu | f6d1f23 | 2013-11-27 12:56:14 -0800 | [diff] [blame] | 90 | int numAttempts = 0; |
| 91 | boolean isExtractionSuccessful = false; |
| 92 | while (numAttempts < MAX_EXTRACT_ATTEMPTS && !isExtractionSuccessful) { |
| 93 | numAttempts++; |
| 94 | |
| 95 | // Create a zip file (extractedFile) containing only the secondary dex file |
| 96 | // (dexFile) from the apk. |
| 97 | extract(apk, dexFile, extractedFile, extractedFilePrefix, |
| 98 | lastModified); |
| 99 | |
| 100 | // Verify that the extracted file is indeed a zip file. |
| 101 | isExtractionSuccessful = verifyZipFile(extractedFile); |
| 102 | |
| 103 | // Log the sha1 of the extracted zip file |
| 104 | Log.i(TAG, "Extraction " + (isExtractionSuccessful ? "success" : "failed") + |
Maurice Chu | 48cd040 | 2013-11-27 15:59:39 -0800 | [diff] [blame] | 105 | " - length " + extractedFile.getAbsolutePath() + ": " + |
| 106 | extractedFile.length()); |
Maurice Chu | f6d1f23 | 2013-11-27 12:56:14 -0800 | [diff] [blame] | 107 | if (!isExtractionSuccessful) { |
| 108 | // Delete the extracted file |
| 109 | extractedFile.delete(); |
| 110 | } |
| 111 | } |
| 112 | if (!isExtractionSuccessful) { |
| 113 | throw new IOException("Could not create zip file " + |
| 114 | extractedFile.getAbsolutePath() + " for secondary dex (" + |
| 115 | secondaryNumber + ")"); |
| 116 | } |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 117 | } |
| 118 | secondaryNumber++; |
| 119 | dexFile = apk.getEntry(DEX_PREFIX + secondaryNumber + DEX_SUFFIX); |
| 120 | } |
| 121 | } finally { |
| 122 | try { |
| 123 | apk.close(); |
| 124 | } catch (IOException e) { |
| 125 | Log.w(TAG, "Failed to close resource", e); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | return files; |
| 130 | } |
| 131 | |
Yohann Roussel | d9eda55 | 2013-11-12 18:13:55 +0100 | [diff] [blame] | 132 | private static void prepareDexDir(File dexDir, final String extractedFilePrefix, |
| 133 | final long sourceLastModified) throws IOException { |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 134 | dexDir.mkdir(); |
| 135 | if (!dexDir.isDirectory()) { |
| 136 | throw new IOException("Failed to create dex directory " + dexDir.getPath()); |
| 137 | } |
| 138 | |
| 139 | // Clean possible old files |
Yohann Roussel | d9eda55 | 2013-11-12 18:13:55 +0100 | [diff] [blame] | 140 | FileFilter filter = new FileFilter() { |
| 141 | |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 142 | @Override |
Yohann Roussel | d9eda55 | 2013-11-12 18:13:55 +0100 | [diff] [blame] | 143 | public boolean accept(File pathname) { |
| 144 | return (!pathname.getName().startsWith(extractedFilePrefix)) |
| 145 | || (pathname.lastModified() < sourceLastModified); |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 146 | } |
| 147 | }; |
| 148 | File[] files = dexDir.listFiles(filter); |
| 149 | if (files == null) { |
| 150 | Log.w(TAG, "Failed to list secondary dex dir content (" + dexDir.getPath() + ")."); |
| 151 | return; |
| 152 | } |
| 153 | for (File oldFile : files) { |
| 154 | if (!oldFile.delete()) { |
| 155 | Log.w(TAG, "Failed to delete old file " + oldFile.getPath()); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
Yohann Roussel | 52eafa0 | 2013-11-21 11:46:53 +0100 | [diff] [blame] | 160 | private static void extract(ZipFile apk, ZipEntry dexFile, File extractTo, |
Yohann Roussel | d9eda55 | 2013-11-12 18:13:55 +0100 | [diff] [blame] | 161 | String extractedFilePrefix, long sourceLastModified) |
| 162 | throws IOException, FileNotFoundException { |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 163 | |
| 164 | InputStream in = apk.getInputStream(dexFile); |
| 165 | ZipOutputStream out = null; |
| 166 | File tmp = File.createTempFile(extractedFilePrefix, EXTRACTED_SUFFIX, |
| 167 | extractTo.getParentFile()); |
| 168 | Log.i(TAG, "Extracting " + tmp.getPath()); |
| 169 | try { |
Maurice Chu | a159fd5 | 2013-11-28 12:59:37 -0800 | [diff] [blame^] | 170 | out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(tmp))); |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 171 | try { |
| 172 | ZipEntry classesDex = new ZipEntry("classes.dex"); |
Yohann Roussel | edf0717 | 2013-11-12 18:11:02 +0100 | [diff] [blame] | 173 | // keep zip entry time since it is the criteria used by Dalvik |
| 174 | classesDex.setTime(dexFile.getTime()); |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 175 | out.putNextEntry(classesDex); |
| 176 | |
| 177 | byte[] buffer = new byte[BUFFER_SIZE]; |
| 178 | int length = in.read(buffer); |
Maurice Chu | 1f8c349 | 2013-11-20 17:14:31 -0800 | [diff] [blame] | 179 | while (length != -1) { |
Yohann Roussel | 52eafa0 | 2013-11-21 11:46:53 +0100 | [diff] [blame] | 180 | out.write(buffer, 0, length); |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 181 | length = in.read(buffer); |
| 182 | } |
Maurice Chu | a159fd5 | 2013-11-28 12:59:37 -0800 | [diff] [blame^] | 183 | out.closeEntry(); |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 184 | } finally { |
Maurice Chu | a0c1a85 | 2013-11-27 19:01:53 -0800 | [diff] [blame] | 185 | out.close(); |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 186 | } |
Yohann Roussel | d9eda55 | 2013-11-12 18:13:55 +0100 | [diff] [blame] | 187 | if (!tmp.setLastModified(sourceLastModified)) { |
| 188 | Log.e(TAG, "Failed to set time of \"" + tmp.getAbsolutePath() + "\"." + |
| 189 | " This may cause problems with later updates of the apk."); |
| 190 | } |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 191 | Log.i(TAG, "Renaming to " + extractTo.getPath()); |
Maurice Chu | a159fd5 | 2013-11-28 12:59:37 -0800 | [diff] [blame^] | 192 | if (!tmp.renameTo(extractTo)) { |
| 193 | throw new IOException("Failed to rename \"" + tmp.getAbsolutePath() + |
| 194 | "\" to \"" + extractTo.getAbsolutePath() + "\""); |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 195 | } |
| 196 | } finally { |
| 197 | closeQuietly(in); |
| 198 | tmp.delete(); // return status ignored |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /** |
Maurice Chu | f6d1f23 | 2013-11-27 12:56:14 -0800 | [diff] [blame] | 203 | * Returns whether the file is a valid zip file. |
| 204 | */ |
| 205 | private static boolean verifyZipFile(File file) { |
| 206 | try { |
| 207 | ZipFile zipFile = new ZipFile(file); |
| 208 | try { |
| 209 | zipFile.close(); |
Maurice Chu | a0c1a85 | 2013-11-27 19:01:53 -0800 | [diff] [blame] | 210 | return true; |
Maurice Chu | f6d1f23 | 2013-11-27 12:56:14 -0800 | [diff] [blame] | 211 | } catch (IOException e) { |
| 212 | Log.w(TAG, "Failed to close zip file: " + file.getAbsolutePath()); |
| 213 | } |
Maurice Chu | f6d1f23 | 2013-11-27 12:56:14 -0800 | [diff] [blame] | 214 | } catch (ZipException ex) { |
| 215 | Log.w(TAG, "File " + file.getAbsolutePath() + " is not a valid zip file.", ex); |
| 216 | } catch (IOException ex) { |
| 217 | Log.w(TAG, "Got an IOException trying to open zip file: " + file.getAbsolutePath(), ex); |
| 218 | } |
| 219 | return false; |
| 220 | } |
| 221 | |
| 222 | /** |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 223 | * Closes the given {@code Closeable}. Suppresses any IO exceptions. |
| 224 | */ |
| 225 | private static void closeQuietly(Closeable closeable) { |
| 226 | try { |
| 227 | closeable.close(); |
| 228 | } catch (IOException e) { |
| 229 | Log.w(TAG, "Failed to close resource", e); |
| 230 | } |
| 231 | } |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 232 | } |