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 | 7e267a3 | 2014-01-15 19:02:18 -0800 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.content.SharedPreferences; |
Maurice Chu | 1f8c349 | 2013-11-20 17:14:31 -0800 | [diff] [blame] | 21 | import android.content.pm.ApplicationInfo; |
Maurice Chu | 994fa84 | 2014-01-17 10:42:54 -0800 | [diff] [blame] | 22 | import android.os.Build; |
Maurice Chu | 1f8c349 | 2013-11-20 17:14:31 -0800 | [diff] [blame] | 23 | import android.util.Log; |
| 24 | |
Maurice Chu | a159fd5 | 2013-11-28 12:59:37 -0800 | [diff] [blame] | 25 | import java.io.BufferedOutputStream; |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 26 | import java.io.Closeable; |
| 27 | import java.io.File; |
Yohann Roussel | d9eda55 | 2013-11-12 18:13:55 +0100 | [diff] [blame] | 28 | import java.io.FileFilter; |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 29 | import java.io.FileNotFoundException; |
| 30 | import java.io.FileOutputStream; |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 31 | import java.io.IOException; |
| 32 | import java.io.InputStream; |
Andrew Johnson | 048fbf7 | 2016-03-09 10:26:37 -0500 | [diff] [blame^] | 33 | import java.io.RandomAccessFile; |
| 34 | import java.nio.channels.FileChannel; |
| 35 | import java.nio.channels.FileLock; |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 36 | import java.util.ArrayList; |
| 37 | import java.util.List; |
| 38 | import java.util.zip.ZipEntry; |
Maurice Chu | f6d1f23 | 2013-11-27 12:56:14 -0800 | [diff] [blame] | 39 | import java.util.zip.ZipException; |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 40 | import java.util.zip.ZipFile; |
| 41 | import java.util.zip.ZipOutputStream; |
| 42 | |
| 43 | /** |
| 44 | * Exposes application secondary dex files as files in the application data |
| 45 | * directory. |
| 46 | */ |
| 47 | final class MultiDexExtractor { |
| 48 | |
| 49 | private static final String TAG = MultiDex.TAG; |
| 50 | |
| 51 | /** |
| 52 | * We look for additional dex files named {@code classes2.dex}, |
| 53 | * {@code classes3.dex}, etc. |
| 54 | */ |
| 55 | private static final String DEX_PREFIX = "classes"; |
| 56 | private static final String DEX_SUFFIX = ".dex"; |
| 57 | |
| 58 | private static final String EXTRACTED_NAME_EXT = ".classes"; |
| 59 | private static final String EXTRACTED_SUFFIX = ".zip"; |
Maurice Chu | f6d1f23 | 2013-11-27 12:56:14 -0800 | [diff] [blame] | 60 | private static final int MAX_EXTRACT_ATTEMPTS = 3; |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 61 | |
Maurice Chu | 7e267a3 | 2014-01-15 19:02:18 -0800 | [diff] [blame] | 62 | private static final String PREFS_FILE = "multidex.version"; |
Yohann Roussel | 602c6ca | 2014-03-28 17:35:02 +0100 | [diff] [blame] | 63 | private static final String KEY_TIME_STAMP = "timestamp"; |
| 64 | private static final String KEY_CRC = "crc"; |
| 65 | private static final String KEY_DEX_NUMBER = "dex.number"; |
| 66 | |
| 67 | /** |
| 68 | * Size of reading buffers. |
| 69 | */ |
| 70 | private static final int BUFFER_SIZE = 0x4000; |
| 71 | /* Keep value away from 0 because it is a too probable time stamp value */ |
| 72 | private static final long NO_VALUE = -1L; |
Maurice Chu | 7e267a3 | 2014-01-15 19:02:18 -0800 | [diff] [blame] | 73 | |
Andrew Johnson | 048fbf7 | 2016-03-09 10:26:37 -0500 | [diff] [blame^] | 74 | private static final String LOCK_FILENAME = "MultiDex.lock"; |
| 75 | |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 76 | /** |
| 77 | * Extracts application secondary dexes into files in the application data |
| 78 | * directory. |
| 79 | * |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 80 | * @return a list of files that were created. The list may be empty if there |
Andrew Johnson | 048fbf7 | 2016-03-09 10:26:37 -0500 | [diff] [blame^] | 81 | * are no secondary dex files. Never return null. |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 82 | * @throws IOException if encounters a problem while reading or writing |
| 83 | * secondary dex files |
| 84 | */ |
Maurice Chu | 7e267a3 | 2014-01-15 19:02:18 -0800 | [diff] [blame] | 85 | static List<File> load(Context context, ApplicationInfo applicationInfo, File dexDir, |
| 86 | boolean forceReload) throws IOException { |
Yohann Roussel | 602c6ca | 2014-03-28 17:35:02 +0100 | [diff] [blame] | 87 | Log.i(TAG, "MultiDexExtractor.load(" + applicationInfo.sourceDir + ", " + forceReload + ")"); |
Maurice Chu | 7e267a3 | 2014-01-15 19:02:18 -0800 | [diff] [blame] | 88 | final File sourceApk = new File(applicationInfo.sourceDir); |
Yohann Roussel | 602c6ca | 2014-03-28 17:35:02 +0100 | [diff] [blame] | 89 | |
Yohann Roussel | 590a07e | 2014-07-21 17:47:26 +0200 | [diff] [blame] | 90 | long currentCrc = getZipCrc(sourceApk); |
Yohann Roussel | 602c6ca | 2014-03-28 17:35:02 +0100 | [diff] [blame] | 91 | |
Andrew Johnson | 048fbf7 | 2016-03-09 10:26:37 -0500 | [diff] [blame^] | 92 | // Validity check and extraction must be done only while the lock file has been taken. |
| 93 | File lockFile = new File(dexDir, LOCK_FILENAME); |
| 94 | RandomAccessFile lockRaf = new RandomAccessFile(lockFile, "rw"); |
| 95 | FileChannel lockChannel = null; |
| 96 | FileLock cacheLock = null; |
Yohann Roussel | 602c6ca | 2014-03-28 17:35:02 +0100 | [diff] [blame] | 97 | List<File> files; |
Andrew Johnson | 048fbf7 | 2016-03-09 10:26:37 -0500 | [diff] [blame^] | 98 | IOException releaseLockException = null; |
| 99 | try { |
| 100 | lockChannel = lockRaf.getChannel(); |
| 101 | Log.i(TAG, "Blocking on lock " + lockFile.getPath()); |
| 102 | cacheLock = lockChannel.lock(); |
| 103 | Log.i(TAG, lockFile.getPath() + " locked"); |
| 104 | |
| 105 | if (!forceReload && !isModified(context, sourceApk, currentCrc)) { |
| 106 | try { |
| 107 | files = loadExistingExtractions(context, sourceApk, dexDir); |
| 108 | } catch (IOException ioe) { |
| 109 | Log.w(TAG, "Failed to reload existing extracted secondary dex files," |
| 110 | + " falling back to fresh extraction", ioe); |
| 111 | files = performExtractions(sourceApk, dexDir); |
| 112 | putStoredApkInfo(context, |
| 113 | getTimeStamp(sourceApk), currentCrc, files.size() + 1); |
| 114 | |
| 115 | } |
| 116 | } else { |
| 117 | Log.i(TAG, "Detected that extraction must be performed."); |
Yohann Roussel | 602c6ca | 2014-03-28 17:35:02 +0100 | [diff] [blame] | 118 | files = performExtractions(sourceApk, dexDir); |
| 119 | putStoredApkInfo(context, getTimeStamp(sourceApk), currentCrc, files.size() + 1); |
Yohann Roussel | 602c6ca | 2014-03-28 17:35:02 +0100 | [diff] [blame] | 120 | } |
Andrew Johnson | 048fbf7 | 2016-03-09 10:26:37 -0500 | [diff] [blame^] | 121 | } finally { |
| 122 | if (cacheLock != null) { |
| 123 | try { |
| 124 | cacheLock.release(); |
| 125 | } catch (IOException e) { |
| 126 | Log.e(TAG, "Failed to release lock on " + lockFile.getPath()); |
| 127 | // Exception while releasing the lock is bad, we want to report it, but not at |
| 128 | // the price of overriding any already pending exception. |
| 129 | releaseLockException = e; |
| 130 | } |
| 131 | } |
| 132 | if (lockChannel != null) { |
| 133 | closeQuietly(lockChannel); |
| 134 | } |
| 135 | closeQuietly(lockRaf); |
| 136 | } |
| 137 | |
| 138 | if (releaseLockException != null) { |
| 139 | throw releaseLockException; |
Yohann Roussel | 602c6ca | 2014-03-28 17:35:02 +0100 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | Log.i(TAG, "load found " + files.size() + " secondary dex files"); |
| 143 | return files; |
| 144 | } |
| 145 | |
Andrew Johnson | 048fbf7 | 2016-03-09 10:26:37 -0500 | [diff] [blame^] | 146 | /** |
| 147 | * Load previously extracted secondary dex files. Should be called only while owning the lock on |
| 148 | * {@link #LOCK_FILENAME}. |
| 149 | */ |
Yohann Roussel | 602c6ca | 2014-03-28 17:35:02 +0100 | [diff] [blame] | 150 | private static List<File> loadExistingExtractions(Context context, File sourceApk, File dexDir) |
| 151 | throws IOException { |
| 152 | Log.i(TAG, "loading existing secondary dex files"); |
| 153 | |
| 154 | final String extractedFilePrefix = sourceApk.getName() + EXTRACTED_NAME_EXT; |
| 155 | int totalDexNumber = getMultiDexPreferences(context).getInt(KEY_DEX_NUMBER, 1); |
| 156 | final List<File> files = new ArrayList<File>(totalDexNumber); |
| 157 | |
| 158 | for (int secondaryNumber = 2; secondaryNumber <= totalDexNumber; secondaryNumber++) { |
| 159 | String fileName = extractedFilePrefix + secondaryNumber + EXTRACTED_SUFFIX; |
| 160 | File extractedFile = new File(dexDir, fileName); |
| 161 | if (extractedFile.isFile()) { |
| 162 | files.add(extractedFile); |
| 163 | if (!verifyZipFile(extractedFile)) { |
| 164 | Log.i(TAG, "Invalid zip file: " + extractedFile); |
| 165 | throw new IOException("Invalid ZIP file."); |
| 166 | } |
| 167 | } else { |
| 168 | throw new IOException("Missing extracted secondary dex file '" + |
| 169 | extractedFile.getPath() + "'"); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | return files; |
| 174 | } |
| 175 | |
Andrew Johnson | 048fbf7 | 2016-03-09 10:26:37 -0500 | [diff] [blame^] | 176 | |
| 177 | /** |
| 178 | * Compare current archive and crc with values stored in {@link SharedPreferences}. Should be |
| 179 | * called only while owning the lock on {@link #LOCK_FILENAME}. |
| 180 | */ |
Yohann Roussel | 602c6ca | 2014-03-28 17:35:02 +0100 | [diff] [blame] | 181 | private static boolean isModified(Context context, File archive, long currentCrc) { |
| 182 | SharedPreferences prefs = getMultiDexPreferences(context); |
| 183 | return (prefs.getLong(KEY_TIME_STAMP, NO_VALUE) != getTimeStamp(archive)) |
| 184 | || (prefs.getLong(KEY_CRC, NO_VALUE) != currentCrc); |
| 185 | } |
| 186 | |
| 187 | private static long getTimeStamp(File archive) { |
| 188 | long timeStamp = archive.lastModified(); |
| 189 | if (timeStamp == NO_VALUE) { |
| 190 | // never return NO_VALUE |
| 191 | timeStamp--; |
| 192 | } |
| 193 | return timeStamp; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | private static long getZipCrc(File archive) throws IOException { |
| 198 | long computedValue = ZipUtil.getZipCrc(archive); |
| 199 | if (computedValue == NO_VALUE) { |
| 200 | // never return NO_VALUE |
| 201 | computedValue--; |
| 202 | } |
| 203 | return computedValue; |
| 204 | } |
| 205 | |
| 206 | private static List<File> performExtractions(File sourceApk, File dexDir) |
| 207 | throws IOException { |
| 208 | |
Maurice Chu | 7e267a3 | 2014-01-15 19:02:18 -0800 | [diff] [blame] | 209 | final String extractedFilePrefix = sourceApk.getName() + EXTRACTED_NAME_EXT; |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 210 | |
Maurice Chu | 994fa84 | 2014-01-17 10:42:54 -0800 | [diff] [blame] | 211 | // Ensure that whatever deletions happen in prepareDexDir only happen if the zip that |
| 212 | // contains a secondary dex file in there is not consistent with the latest apk. Otherwise, |
| 213 | // multi-process race conditions can cause a crash loop where one process deletes the zip |
| 214 | // while another had created it. |
| 215 | prepareDexDir(dexDir, extractedFilePrefix); |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 216 | |
Yohann Roussel | 602c6ca | 2014-03-28 17:35:02 +0100 | [diff] [blame] | 217 | List<File> files = new ArrayList<File>(); |
Maurice Chu | 7e267a3 | 2014-01-15 19:02:18 -0800 | [diff] [blame] | 218 | |
Yohann Roussel | 602c6ca | 2014-03-28 17:35:02 +0100 | [diff] [blame] | 219 | final ZipFile apk = new ZipFile(sourceApk); |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 220 | try { |
| 221 | |
| 222 | int secondaryNumber = 2; |
| 223 | |
| 224 | ZipEntry dexFile = apk.getEntry(DEX_PREFIX + secondaryNumber + DEX_SUFFIX); |
| 225 | while (dexFile != null) { |
| 226 | String fileName = extractedFilePrefix + secondaryNumber + EXTRACTED_SUFFIX; |
| 227 | File extractedFile = new File(dexDir, fileName); |
| 228 | files.add(extractedFile); |
| 229 | |
Yohann Roussel | 602c6ca | 2014-03-28 17:35:02 +0100 | [diff] [blame] | 230 | Log.i(TAG, "Extraction is needed for file " + extractedFile); |
| 231 | int numAttempts = 0; |
| 232 | boolean isExtractionSuccessful = false; |
| 233 | while (numAttempts < MAX_EXTRACT_ATTEMPTS && !isExtractionSuccessful) { |
| 234 | numAttempts++; |
Maurice Chu | f6d1f23 | 2013-11-27 12:56:14 -0800 | [diff] [blame] | 235 | |
Yohann Roussel | 602c6ca | 2014-03-28 17:35:02 +0100 | [diff] [blame] | 236 | // Create a zip file (extractedFile) containing only the secondary dex file |
| 237 | // (dexFile) from the apk. |
| 238 | extract(apk, dexFile, extractedFile, extractedFilePrefix); |
Maurice Chu | f6d1f23 | 2013-11-27 12:56:14 -0800 | [diff] [blame] | 239 | |
Yohann Roussel | 602c6ca | 2014-03-28 17:35:02 +0100 | [diff] [blame] | 240 | // Verify that the extracted file is indeed a zip file. |
| 241 | isExtractionSuccessful = verifyZipFile(extractedFile); |
Maurice Chu | f6d1f23 | 2013-11-27 12:56:14 -0800 | [diff] [blame] | 242 | |
Yohann Roussel | 602c6ca | 2014-03-28 17:35:02 +0100 | [diff] [blame] | 243 | // Log the sha1 of the extracted zip file |
| 244 | Log.i(TAG, "Extraction " + (isExtractionSuccessful ? "success" : "failed") + |
| 245 | " - length " + extractedFile.getAbsolutePath() + ": " + |
| 246 | extractedFile.length()); |
| 247 | if (!isExtractionSuccessful) { |
| 248 | // Delete the extracted file |
| 249 | extractedFile.delete(); |
| 250 | if (extractedFile.exists()) { |
| 251 | Log.w(TAG, "Failed to delete corrupted secondary dex '" + |
| 252 | extractedFile.getPath() + "'"); |
Maurice Chu | f6d1f23 | 2013-11-27 12:56:14 -0800 | [diff] [blame] | 253 | } |
| 254 | } |
Yohann Roussel | 602c6ca | 2014-03-28 17:35:02 +0100 | [diff] [blame] | 255 | } |
| 256 | if (!isExtractionSuccessful) { |
| 257 | throw new IOException("Could not create zip file " + |
| 258 | extractedFile.getAbsolutePath() + " for secondary dex (" + |
| 259 | secondaryNumber + ")"); |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 260 | } |
| 261 | secondaryNumber++; |
| 262 | dexFile = apk.getEntry(DEX_PREFIX + secondaryNumber + DEX_SUFFIX); |
| 263 | } |
| 264 | } finally { |
| 265 | try { |
| 266 | apk.close(); |
| 267 | } catch (IOException e) { |
| 268 | Log.w(TAG, "Failed to close resource", e); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | return files; |
| 273 | } |
| 274 | |
Andrew Johnson | 048fbf7 | 2016-03-09 10:26:37 -0500 | [diff] [blame^] | 275 | /** |
| 276 | * Save {@link SharedPreferences}. Should be called only while owning the lock on |
| 277 | * {@link #LOCK_FILENAME}. |
| 278 | */ |
Yohann Roussel | 602c6ca | 2014-03-28 17:35:02 +0100 | [diff] [blame] | 279 | private static void putStoredApkInfo(Context context, long timeStamp, long crc, |
| 280 | int totalDexNumber) { |
Maurice Chu | 994fa84 | 2014-01-17 10:42:54 -0800 | [diff] [blame] | 281 | SharedPreferences prefs = getMultiDexPreferences(context); |
Maurice Chu | 7e267a3 | 2014-01-15 19:02:18 -0800 | [diff] [blame] | 282 | SharedPreferences.Editor edit = prefs.edit(); |
Yohann Roussel | 602c6ca | 2014-03-28 17:35:02 +0100 | [diff] [blame] | 283 | edit.putLong(KEY_TIME_STAMP, timeStamp); |
| 284 | edit.putLong(KEY_CRC, crc); |
Yohann Roussel | 602c6ca | 2014-03-28 17:35:02 +0100 | [diff] [blame] | 285 | edit.putInt(KEY_DEX_NUMBER, totalDexNumber); |
Andrew Johnson | 048fbf7 | 2016-03-09 10:26:37 -0500 | [diff] [blame^] | 286 | /* Use commit() and not apply() as advised by the doc because we need synchronous writing of |
| 287 | * the editor content and apply is doing an "asynchronous commit to disk". |
| 288 | */ |
| 289 | edit.commit(); |
Maurice Chu | 7e267a3 | 2014-01-15 19:02:18 -0800 | [diff] [blame] | 290 | } |
| 291 | |
Andrew Johnson | 048fbf7 | 2016-03-09 10:26:37 -0500 | [diff] [blame^] | 292 | /** |
| 293 | * Get the MuliDex {@link SharedPreferences} for the current application. Should be called only |
| 294 | * while owning the lock on {@link #LOCK_FILENAME}. |
| 295 | */ |
Maurice Chu | 994fa84 | 2014-01-17 10:42:54 -0800 | [diff] [blame] | 296 | private static SharedPreferences getMultiDexPreferences(Context context) { |
| 297 | return context.getSharedPreferences(PREFS_FILE, |
Yohann Roussel | e99daea | 2014-09-17 16:49:21 +0200 | [diff] [blame] | 298 | Build.VERSION.SDK_INT < 11 /* Build.VERSION_CODES.HONEYCOMB */ |
Maurice Chu | 994fa84 | 2014-01-17 10:42:54 -0800 | [diff] [blame] | 299 | ? Context.MODE_PRIVATE |
Yohann Roussel | e99daea | 2014-09-17 16:49:21 +0200 | [diff] [blame] | 300 | : Context.MODE_PRIVATE | 0x0004 /* Context.MODE_MULTI_PROCESS */); |
Maurice Chu | 994fa84 | 2014-01-17 10:42:54 -0800 | [diff] [blame] | 301 | } |
| 302 | |
Maurice Chu | 7e267a3 | 2014-01-15 19:02:18 -0800 | [diff] [blame] | 303 | /** |
Andrew Johnson | 048fbf7 | 2016-03-09 10:26:37 -0500 | [diff] [blame^] | 304 | * This removes old files. |
Maurice Chu | cc63eda | 2013-12-02 15:39:59 -0800 | [diff] [blame] | 305 | */ |
Yohann Roussel | 606af94 | 2015-05-12 17:40:52 +0200 | [diff] [blame] | 306 | private static void prepareDexDir(File dexDir, final String extractedFilePrefix) { |
Yohann Roussel | d9eda55 | 2013-11-12 18:13:55 +0100 | [diff] [blame] | 307 | FileFilter filter = new FileFilter() { |
| 308 | |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 309 | @Override |
Yohann Roussel | d9eda55 | 2013-11-12 18:13:55 +0100 | [diff] [blame] | 310 | public boolean accept(File pathname) { |
Andrew Johnson | 048fbf7 | 2016-03-09 10:26:37 -0500 | [diff] [blame^] | 311 | String name = pathname.getName(); |
| 312 | return !(name.startsWith(extractedFilePrefix) |
| 313 | || name.equals(LOCK_FILENAME)); |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 314 | } |
| 315 | }; |
| 316 | File[] files = dexDir.listFiles(filter); |
| 317 | if (files == null) { |
| 318 | Log.w(TAG, "Failed to list secondary dex dir content (" + dexDir.getPath() + ")."); |
| 319 | return; |
| 320 | } |
| 321 | for (File oldFile : files) { |
Yohann Roussel | d79604b | 2014-07-08 16:50:10 +0200 | [diff] [blame] | 322 | Log.i(TAG, "Trying to delete old file " + oldFile.getPath() + " of size " + |
Maurice Chu | 7e267a3 | 2014-01-15 19:02:18 -0800 | [diff] [blame] | 323 | oldFile.length()); |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 324 | if (!oldFile.delete()) { |
| 325 | Log.w(TAG, "Failed to delete old file " + oldFile.getPath()); |
Yohann Roussel | 88117c3 | 2013-11-28 23:22:11 +0100 | [diff] [blame] | 326 | } else { |
Yohann Roussel | d79604b | 2014-07-08 16:50:10 +0200 | [diff] [blame] | 327 | Log.i(TAG, "Deleted old file " + oldFile.getPath()); |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
Yohann Roussel | 52eafa0 | 2013-11-21 11:46:53 +0100 | [diff] [blame] | 332 | private static void extract(ZipFile apk, ZipEntry dexFile, File extractTo, |
Maurice Chu | 7e267a3 | 2014-01-15 19:02:18 -0800 | [diff] [blame] | 333 | String extractedFilePrefix) throws IOException, FileNotFoundException { |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 334 | |
| 335 | InputStream in = apk.getInputStream(dexFile); |
| 336 | ZipOutputStream out = null; |
| 337 | File tmp = File.createTempFile(extractedFilePrefix, EXTRACTED_SUFFIX, |
| 338 | extractTo.getParentFile()); |
| 339 | Log.i(TAG, "Extracting " + tmp.getPath()); |
| 340 | try { |
Maurice Chu | a159fd5 | 2013-11-28 12:59:37 -0800 | [diff] [blame] | 341 | out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(tmp))); |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 342 | try { |
| 343 | ZipEntry classesDex = new ZipEntry("classes.dex"); |
Yohann Roussel | edf0717 | 2013-11-12 18:11:02 +0100 | [diff] [blame] | 344 | // keep zip entry time since it is the criteria used by Dalvik |
| 345 | classesDex.setTime(dexFile.getTime()); |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 346 | out.putNextEntry(classesDex); |
| 347 | |
| 348 | byte[] buffer = new byte[BUFFER_SIZE]; |
| 349 | int length = in.read(buffer); |
Maurice Chu | 1f8c349 | 2013-11-20 17:14:31 -0800 | [diff] [blame] | 350 | while (length != -1) { |
Yohann Roussel | 52eafa0 | 2013-11-21 11:46:53 +0100 | [diff] [blame] | 351 | out.write(buffer, 0, length); |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 352 | length = in.read(buffer); |
| 353 | } |
Maurice Chu | a159fd5 | 2013-11-28 12:59:37 -0800 | [diff] [blame] | 354 | out.closeEntry(); |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 355 | } finally { |
Maurice Chu | a0c1a85 | 2013-11-27 19:01:53 -0800 | [diff] [blame] | 356 | out.close(); |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 357 | } |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 358 | Log.i(TAG, "Renaming to " + extractTo.getPath()); |
Maurice Chu | a159fd5 | 2013-11-28 12:59:37 -0800 | [diff] [blame] | 359 | if (!tmp.renameTo(extractTo)) { |
| 360 | throw new IOException("Failed to rename \"" + tmp.getAbsolutePath() + |
| 361 | "\" to \"" + extractTo.getAbsolutePath() + "\""); |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 362 | } |
| 363 | } finally { |
| 364 | closeQuietly(in); |
| 365 | tmp.delete(); // return status ignored |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | /** |
Maurice Chu | f6d1f23 | 2013-11-27 12:56:14 -0800 | [diff] [blame] | 370 | * Returns whether the file is a valid zip file. |
| 371 | */ |
Maurice Chu | cc63eda | 2013-12-02 15:39:59 -0800 | [diff] [blame] | 372 | static boolean verifyZipFile(File file) { |
Maurice Chu | f6d1f23 | 2013-11-27 12:56:14 -0800 | [diff] [blame] | 373 | try { |
| 374 | ZipFile zipFile = new ZipFile(file); |
| 375 | try { |
| 376 | zipFile.close(); |
Maurice Chu | a0c1a85 | 2013-11-27 19:01:53 -0800 | [diff] [blame] | 377 | return true; |
Maurice Chu | f6d1f23 | 2013-11-27 12:56:14 -0800 | [diff] [blame] | 378 | } catch (IOException e) { |
| 379 | Log.w(TAG, "Failed to close zip file: " + file.getAbsolutePath()); |
| 380 | } |
Maurice Chu | f6d1f23 | 2013-11-27 12:56:14 -0800 | [diff] [blame] | 381 | } catch (ZipException ex) { |
| 382 | Log.w(TAG, "File " + file.getAbsolutePath() + " is not a valid zip file.", ex); |
| 383 | } catch (IOException ex) { |
| 384 | Log.w(TAG, "Got an IOException trying to open zip file: " + file.getAbsolutePath(), ex); |
| 385 | } |
| 386 | return false; |
| 387 | } |
| 388 | |
| 389 | /** |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 390 | * Closes the given {@code Closeable}. Suppresses any IO exceptions. |
| 391 | */ |
| 392 | private static void closeQuietly(Closeable closeable) { |
| 393 | try { |
| 394 | closeable.close(); |
| 395 | } catch (IOException e) { |
| 396 | Log.w(TAG, "Failed to close resource", e); |
| 397 | } |
| 398 | } |
Maurice Chu | 667f9a8 | 2013-10-16 13:12:22 -0700 | [diff] [blame] | 399 | } |