1667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu/* 2667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * Copyright (C) 2013 The Android Open Source Project 3667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * 4667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * Licensed under the Apache License, Version 2.0 (the "License"); 5667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * you may not use this file except in compliance with the License. 6667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * You may obtain a copy of the License at 7667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * 8667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * http://www.apache.org/licenses/LICENSE-2.0 9667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * 10667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * Unless required by applicable law or agreed to in writing, software 11667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * distributed under the License is distributed on an "AS IS" BASIS, 12667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * See the License for the specific language governing permissions and 14667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * limitations under the License. 15667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu */ 16667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 17667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chupackage android.support.multidex; 18667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 19dd3cc22f2fbc8ea4dd5fa88978dc8d1ae5034bd9Yohann Rousselimport android.app.Application; 20667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport android.content.Context; 21667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport android.content.pm.ApplicationInfo; 22667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport android.content.pm.PackageManager; 23d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Rousselimport android.content.pm.PackageManager.NameNotFoundException; 24667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport android.os.Build; 25667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport android.util.Log; 26667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 27602c6ca8cae4718ba8ff9f65e53305d002479359Yohann Rousselimport dalvik.system.DexFile; 28602c6ca8cae4718ba8ff9f65e53305d002479359Yohann Roussel 29667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport java.io.File; 30667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport java.io.IOException; 31667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport java.lang.reflect.Array; 32667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport java.lang.reflect.Field; 33667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport java.lang.reflect.InvocationTargetException; 34667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport java.lang.reflect.Method; 35667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport java.util.ArrayList; 36667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport java.util.Arrays; 37667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport java.util.HashSet; 38667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport java.util.List; 39667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport java.util.ListIterator; 40667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chuimport java.util.Set; 417b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Rousselimport java.util.regex.Matcher; 427b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Rousselimport java.util.regex.Pattern; 4366f379f774e06e650375750d18b1695ddb853371Maurice Chuimport java.util.zip.ZipFile; 44667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 45667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu/** 46667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * Monkey patches {@link Context#getClassLoader() the application context class 47667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * loader} in order to load classes from more than one dex file. The primary 48dd3cc22f2fbc8ea4dd5fa88978dc8d1ae5034bd9Yohann Roussel * {@code classes.dex} must contain the classes necessary for calling this 49dd3cc22f2fbc8ea4dd5fa88978dc8d1ae5034bd9Yohann Roussel * class methods. Secondary dex files named classes2.dex, classes3.dex... found 50dd3cc22f2fbc8ea4dd5fa88978dc8d1ae5034bd9Yohann Roussel * in the application apk will be added to the classloader after first call to 51667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * {@link #install(Context)}. 52667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * 53667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * <p/> 547b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel * This library provides compatibility for platforms with API level 4 through 20. This library does 557b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel * nothing on newer versions of the platform which provide built-in support for secondary dex files. 56667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu */ 57667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chupublic final class MultiDex { 58667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 59667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu static final String TAG = "MultiDex"; 60667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 6158f5bb5e72221b538fbcc55eb6c2a2499f8c2488Yohann Roussel private static final String OLD_SECONDARY_FOLDER_NAME = "secondary-dexes"; 6258f5bb5e72221b538fbcc55eb6c2a2499f8c2488Yohann Roussel 6358f5bb5e72221b538fbcc55eb6c2a2499f8c2488Yohann Roussel private static final String SECONDARY_FOLDER_NAME = "code_cache" + File.separator + 6458f5bb5e72221b538fbcc55eb6c2a2499f8c2488Yohann Roussel "secondary-dexes"; 65667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 667b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel private static final int MAX_SUPPORTED_SDK_VERSION = 20; 67667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 68667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu private static final int MIN_SDK_VERSION = 4; 69667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 707b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel private static final int VM_WITH_MULTIDEX_VERSION_MAJOR = 2; 717b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel 727b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel private static final int VM_WITH_MULTIDEX_VERSION_MINOR = 1; 737b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel 74667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu private static final Set<String> installedApk = new HashSet<String>(); 75667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 767b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel private static final boolean IS_VM_MULTIDEX_CAPABLE = 777b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel isVMMultidexCapable(System.getProperty("java.vm.version")); 787b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel 79667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu private MultiDex() {} 80667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 81667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu /** 82667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * Patches the application context class loader by appending extra dex files 83dd3cc22f2fbc8ea4dd5fa88978dc8d1ae5034bd9Yohann Roussel * loaded from the application apk. This method should be called in the 84dd3cc22f2fbc8ea4dd5fa88978dc8d1ae5034bd9Yohann Roussel * attachBaseContext of your {@link Application}, see 85dd3cc22f2fbc8ea4dd5fa88978dc8d1ae5034bd9Yohann Roussel * {@link MultiDexApplication} for more explanation and an example. 86667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * 87667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * @param context application context. 88667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * @throws RuntimeException if an error occurred preventing the classloader 89667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * extension. 90667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu */ 91667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu public static void install(Context context) { 92602c6ca8cae4718ba8ff9f65e53305d002479359Yohann Roussel Log.i(TAG, "install"); 937b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel if (IS_VM_MULTIDEX_CAPABLE) { 947b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel Log.i(TAG, "VM has multidex support, MultiDex support library is disabled."); 957b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel return; 967b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel } 97667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 98667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu if (Build.VERSION.SDK_INT < MIN_SDK_VERSION) { 99667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu throw new RuntimeException("Multi dex installation failed. SDK " + Build.VERSION.SDK_INT 100667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu + " is unsupported. Min SDK version is " + MIN_SDK_VERSION + "."); 101667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } 102667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 103667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu try { 104d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel ApplicationInfo applicationInfo = getApplicationInfo(context); 105667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu if (applicationInfo == null) { 106d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel // Looks like running on a test Context, so just return without patching. 107667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu return; 108667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } 109667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 110667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu synchronized (installedApk) { 111667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu String apkPath = applicationInfo.sourceDir; 112667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu if (installedApk.contains(apkPath)) { 113667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu return; 114667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } 115667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu installedApk.add(apkPath); 116667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 1177b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel if (Build.VERSION.SDK_INT > MAX_SUPPORTED_SDK_VERSION) { 1187b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel Log.w(TAG, "MultiDex is not guaranteed to work in SDK version " 1197b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel + Build.VERSION.SDK_INT + ": SDK version higher than " 1207b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel + MAX_SUPPORTED_SDK_VERSION + " should be backed by " 1217b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel + "runtime with built-in multidex capabilty but it's not the " 1227b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel + "case here: java.vm.version=\"" 1237b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel + System.getProperty("java.vm.version") + "\""); 124667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } 125667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 126667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu /* The patched class loader is expected to be a descendant of 127667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * dalvik.system.BaseDexClassLoader. We modify its 128667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * dalvik.system.DexPathList pathList field to append additional DEX 129667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * file entries. 130667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu */ 131667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu ClassLoader loader; 132667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu try { 133667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu loader = context.getClassLoader(); 134667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } catch (RuntimeException e) { 135667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu /* Ignore those exceptions so that we don't break tests relying on Context like 136667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * a android.test.mock.MockContext or a android.content.ContextWrapper with a 137667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * null base Context. 138667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu */ 139667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu Log.w(TAG, "Failure while trying to obtain Context class loader. " + 140667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu "Must be running in test mode. Skip patching.", e); 141667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu return; 142667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } 143667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu if (loader == null) { 144667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu // Note, the context class loader is null when running Robolectric tests. 145667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu Log.e(TAG, 146667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu "Context class loader is null. Must be running in test mode. " 147667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu + "Skip patching."); 148667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu return; 149667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } 150667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 15158f5bb5e72221b538fbcc55eb6c2a2499f8c2488Yohann Roussel try { 15258f5bb5e72221b538fbcc55eb6c2a2499f8c2488Yohann Roussel clearOldDexDir(context); 15358f5bb5e72221b538fbcc55eb6c2a2499f8c2488Yohann Roussel } catch (Throwable t) { 15458f5bb5e72221b538fbcc55eb6c2a2499f8c2488Yohann Roussel Log.w(TAG, "Something went wrong when trying to clear old MultiDex extraction, " 15558f5bb5e72221b538fbcc55eb6c2a2499f8c2488Yohann Roussel + "continuing without cleaning.", t); 15658f5bb5e72221b538fbcc55eb6c2a2499f8c2488Yohann Roussel } 15758f5bb5e72221b538fbcc55eb6c2a2499f8c2488Yohann Roussel 15858f5bb5e72221b538fbcc55eb6c2a2499f8c2488Yohann Roussel File dexDir = new File(applicationInfo.dataDir, SECONDARY_FOLDER_NAME); 1597e267a38525afac2a571da186e770a2b86a01846Maurice Chu List<File> files = MultiDexExtractor.load(context, applicationInfo, dexDir, false); 160cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu if (checkValidZipFiles(files)) { 161cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu installSecondaryDexes(loader, dexDir, files); 162cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu } else { 163cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu Log.w(TAG, "Files were not valid zip files. Forcing a reload."); 164cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu // Try again, but this time force a reload of the zip file. 1657e267a38525afac2a571da186e770a2b86a01846Maurice Chu files = MultiDexExtractor.load(context, applicationInfo, dexDir, true); 166602c6ca8cae4718ba8ff9f65e53305d002479359Yohann Roussel 167cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu if (checkValidZipFiles(files)) { 168cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu installSecondaryDexes(loader, dexDir, files); 169667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } else { 170cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu // Second time didn't work, give up 171cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu throw new RuntimeException("Zip files were not valid."); 172667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } 173667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } 174667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } 175667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 176667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } catch (Exception e) { 177667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu Log.e(TAG, "Multidex installation failure", e); 178667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu throw new RuntimeException("Multi dex installation failed (" + e.getMessage() + ")."); 179667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } 180602c6ca8cae4718ba8ff9f65e53305d002479359Yohann Roussel Log.i(TAG, "install done"); 181667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } 182667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 183d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel private static ApplicationInfo getApplicationInfo(Context context) 184d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel throws NameNotFoundException { 185d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel PackageManager pm; 186d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel String packageName; 187d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel try { 188d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel pm = context.getPackageManager(); 189d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel packageName = context.getPackageName(); 190d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel } catch (RuntimeException e) { 191d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel /* Ignore those exceptions so that we don't break tests relying on Context like 192d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel * a android.test.mock.MockContext or a android.content.ContextWrapper with a null 193d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel * base Context. 194d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel */ 195d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel Log.w(TAG, "Failure while trying to obtain ApplicationInfo from Context. " + 196d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel "Must be running in test mode. Skip patching.", e); 197d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel return null; 198d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel } 199d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel if (pm == null || packageName == null) { 200d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel // This is most likely a mock context, so just return without patching. 201d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel return null; 202d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel } 203d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel ApplicationInfo applicationInfo = 204d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA); 205d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel return applicationInfo; 206d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel } 207d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel 2087b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel /** 2097b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel * Identifies if the current VM has a native support for multidex, meaning there is no need for 2107b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel * additional installation by this library. 2117b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel * @return true if the VM handles multidex 2127b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel */ 2137b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel /* package visible for test */ 2147b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel static boolean isVMMultidexCapable(String versionString) { 2157b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel boolean isMultidexCapable = false; 2167b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel if (versionString != null) { 2177b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel Matcher matcher = Pattern.compile("(\\d+)\\.(\\d+)(\\.\\d+)?").matcher(versionString); 2187b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel if (matcher.matches()) { 2197b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel try { 2207b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel int major = Integer.parseInt(matcher.group(1)); 2217b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel int minor = Integer.parseInt(matcher.group(2)); 2227b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel isMultidexCapable = (major > VM_WITH_MULTIDEX_VERSION_MAJOR) 2237b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel || ((major == VM_WITH_MULTIDEX_VERSION_MAJOR) 2247b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel && (minor >= VM_WITH_MULTIDEX_VERSION_MINOR)); 2257b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel } catch (NumberFormatException e) { 2267b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel // let isMultidexCapable be false 2277b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel } 2287b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel } 2297b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel } 2307b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel Log.i(TAG, "VM with version " + versionString + 2317b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel (isMultidexCapable ? 2327b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel " has multidex support" : 2337b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel " does not have multidex support")); 2347b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel return isMultidexCapable; 2357b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel } 2367b86f7e21c70ac06129c05ed137e585a308c6fd1Yohann Roussel 237cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu private static void installSecondaryDexes(ClassLoader loader, File dexDir, List<File> files) 238cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, 239cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu InvocationTargetException, NoSuchMethodException, IOException { 240cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu if (!files.isEmpty()) { 241cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu if (Build.VERSION.SDK_INT >= 19) { 242cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu V19.install(loader, files, dexDir); 243cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu } else if (Build.VERSION.SDK_INT >= 14) { 244cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu V14.install(loader, files, dexDir); 245cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu } else { 246cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu V4.install(loader, files); 247cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu } 248cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu } 249cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu } 250cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu 251cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu /** 252cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu * Returns whether all files in the list are valid zip files. If {@code files} is empty, then 253cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu * returns true. 254cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu */ 255cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu private static boolean checkValidZipFiles(List<File> files) { 256cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu for (File file : files) { 257cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu if (!MultiDexExtractor.verifyZipFile(file)) { 258cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu return false; 259cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu } 260cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu } 261cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu return true; 262cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu } 263cc63eda4e6defe6b0dd5dd3c8fa608cf6ff26011Maurice Chu 264667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu /** 265667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * Locates a given field anywhere in the class inheritance hierarchy. 266667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * 267667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * @param instance an object to search the field into. 268667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * @param name field name 269667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * @return a field object 270667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * @throws NoSuchFieldException if the field cannot be located 271667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu */ 272667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu private static Field findField(Object instance, String name) throws NoSuchFieldException { 273667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) { 274667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu try { 275667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu Field field = clazz.getDeclaredField(name); 276667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 277667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 278667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu if (!field.isAccessible()) { 279667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu field.setAccessible(true); 280667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } 281667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 282667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu return field; 283667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } catch (NoSuchFieldException e) { 284667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu // ignore and search next 285667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } 286667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } 287667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 288667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu throw new NoSuchFieldException("Field " + name + " not found in " + instance.getClass()); 289667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } 290667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 291667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu /** 292667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * Locates a given method anywhere in the class inheritance hierarchy. 293667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * 294667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * @param instance an object to search the method into. 295667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * @param name method name 296667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * @param parameterTypes method parameter types 297667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * @return a method object 298667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * @throws NoSuchMethodException if the method cannot be located 299667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu */ 300667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu private static Method findMethod(Object instance, String name, Class<?>... parameterTypes) 301667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu throws NoSuchMethodException { 302667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) { 303667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu try { 304667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu Method method = clazz.getDeclaredMethod(name, parameterTypes); 305667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 306667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 307667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu if (!method.isAccessible()) { 308667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu method.setAccessible(true); 309667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } 310667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 311667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu return method; 312667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } catch (NoSuchMethodException e) { 313667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu // ignore and search next 314667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } 315667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } 316667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 317667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu throw new NoSuchMethodException("Method " + name + " with parameters " + 318667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu Arrays.asList(parameterTypes) + " not found in " + instance.getClass()); 319667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } 320667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 321667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu /** 322667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * Replace the value of a field containing a non null array, by a new array containing the 323667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * elements of the original array plus the elements of extraElements. 324667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * @param instance the instance whose field is to be modified. 325667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * @param fieldName the field to modify. 326667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * @param extraElements elements to append at the end of the array. 327667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu */ 328667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu private static void expandFieldArray(Object instance, String fieldName, 329667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu Object[] extraElements) throws NoSuchFieldException, IllegalArgumentException, 330667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu IllegalAccessException { 331667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu Field jlrField = findField(instance, fieldName); 332667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu Object[] original = (Object[]) jlrField.get(instance); 333667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu Object[] combined = (Object[]) Array.newInstance( 334667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu original.getClass().getComponentType(), original.length + extraElements.length); 335667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu System.arraycopy(original, 0, combined, 0, original.length); 336667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu System.arraycopy(extraElements, 0, combined, original.length, extraElements.length); 337667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu jlrField.set(instance, combined); 338667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } 339667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 340d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel private static void clearOldDexDir(Context context) throws Exception { 34158f5bb5e72221b538fbcc55eb6c2a2499f8c2488Yohann Roussel File dexDir = new File(context.getFilesDir(), OLD_SECONDARY_FOLDER_NAME); 342d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel if (dexDir.isDirectory()) { 343d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel Log.i(TAG, "Clearing old secondary dex dir (" + dexDir.getPath() + ")."); 344d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel File[] files = dexDir.listFiles(); 345d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel if (files == null) { 346d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel Log.w(TAG, "Failed to list secondary dex dir content (" + dexDir.getPath() + ")."); 347d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel return; 348d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel } 349d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel for (File oldFile : files) { 350d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel Log.i(TAG, "Trying to delete old file " + oldFile.getPath() + " of size " 351d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel + oldFile.length()); 352d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel if (!oldFile.delete()) { 353d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel Log.w(TAG, "Failed to delete old file " + oldFile.getPath()); 354d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel } else { 355d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel Log.i(TAG, "Deleted old file " + oldFile.getPath()); 356d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel } 357d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel } 358d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel if (!dexDir.delete()) { 359d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel Log.w(TAG, "Failed to delete secondary dex dir " + dexDir.getPath()); 360d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel } else { 361d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel Log.i(TAG, "Deleted old secondary dex dir " + dexDir.getPath()); 362d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel } 363d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel } 364d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel } 365d79604bd38c101b54e41745f85ddc2e04d978af2Yohann Roussel 366667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu /** 367667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * Installer for platform versions 19. 368667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu */ 369667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu private static final class V19 { 370667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 371667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu private static void install(ClassLoader loader, List<File> additionalClassPathEntries, 372667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu File optimizedDirectory) 373667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu throws IllegalArgumentException, IllegalAccessException, 374667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu NoSuchFieldException, InvocationTargetException, NoSuchMethodException { 375667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu /* The patched class loader is expected to be a descendant of 376667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * dalvik.system.BaseDexClassLoader. We modify its 377667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * dalvik.system.DexPathList pathList field to append additional DEX 378667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * file entries. 379667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu */ 380667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu Field pathListField = findField(loader, "pathList"); 381667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu Object dexPathList = pathListField.get(loader); 382667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu ArrayList<IOException> suppressedExceptions = new ArrayList<IOException>(); 383667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu expandFieldArray(dexPathList, "dexElements", makeDexElements(dexPathList, 384667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu new ArrayList<File>(additionalClassPathEntries), optimizedDirectory, 385667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu suppressedExceptions)); 386667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu if (suppressedExceptions.size() > 0) { 38788117c37e2c3f8601f295b74a3e804877afb78eeYohann Roussel for (IOException e : suppressedExceptions) { 38888117c37e2c3f8601f295b74a3e804877afb78eeYohann Roussel Log.w(TAG, "Exception in makeDexElement", e); 38988117c37e2c3f8601f295b74a3e804877afb78eeYohann Roussel } 390667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu Field suppressedExceptionsField = 391667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu findField(loader, "dexElementsSuppressedExceptions"); 392667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu IOException[] dexElementsSuppressedExceptions = 393667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu (IOException[]) suppressedExceptionsField.get(loader); 394667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 395667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu if (dexElementsSuppressedExceptions == null) { 396667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu dexElementsSuppressedExceptions = 397667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu suppressedExceptions.toArray( 398667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu new IOException[suppressedExceptions.size()]); 399667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } else { 400667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu IOException[] combined = 401667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu new IOException[suppressedExceptions.size() + 402667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu dexElementsSuppressedExceptions.length]; 403667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu suppressedExceptions.toArray(combined); 404667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu System.arraycopy(dexElementsSuppressedExceptions, 0, combined, 405667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu suppressedExceptions.size(), dexElementsSuppressedExceptions.length); 406667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu dexElementsSuppressedExceptions = combined; 407667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } 408667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 409667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu suppressedExceptionsField.set(loader, dexElementsSuppressedExceptions); 410667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } 411667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } 412667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 413667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu /** 414667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * A wrapper around 415667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * {@code private static final dalvik.system.DexPathList#makeDexElements}. 416667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu */ 417667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu private static Object[] makeDexElements( 418667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu Object dexPathList, ArrayList<File> files, File optimizedDirectory, 419667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu ArrayList<IOException> suppressedExceptions) 420667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu throws IllegalAccessException, InvocationTargetException, 421667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu NoSuchMethodException { 422667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu Method makeDexElements = 423667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu findMethod(dexPathList, "makeDexElements", ArrayList.class, File.class, 424667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu ArrayList.class); 425667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 426667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu return (Object[]) makeDexElements.invoke(dexPathList, files, optimizedDirectory, 427667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu suppressedExceptions); 428667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } 429667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } 430667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 431667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu /** 432667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * Installer for platform versions 14, 15, 16, 17 and 18. 433667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu */ 434667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu private static final class V14 { 435667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 436667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu private static void install(ClassLoader loader, List<File> additionalClassPathEntries, 437667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu File optimizedDirectory) 438667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu throws IllegalArgumentException, IllegalAccessException, 439667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu NoSuchFieldException, InvocationTargetException, NoSuchMethodException { 440667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu /* The patched class loader is expected to be a descendant of 441667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * dalvik.system.BaseDexClassLoader. We modify its 442667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * dalvik.system.DexPathList pathList field to append additional DEX 443667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * file entries. 444667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu */ 445667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu Field pathListField = findField(loader, "pathList"); 446667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu Object dexPathList = pathListField.get(loader); 447667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu expandFieldArray(dexPathList, "dexElements", makeDexElements(dexPathList, 448667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu new ArrayList<File>(additionalClassPathEntries), optimizedDirectory)); 449667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } 450667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 451667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu /** 452667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * A wrapper around 453667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * {@code private static final dalvik.system.DexPathList#makeDexElements}. 454667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu */ 455667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu private static Object[] makeDexElements( 456667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu Object dexPathList, ArrayList<File> files, File optimizedDirectory) 457667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu throws IllegalAccessException, InvocationTargetException, 458667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu NoSuchMethodException { 459667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu Method makeDexElements = 460667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu findMethod(dexPathList, "makeDexElements", ArrayList.class, File.class); 461667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 462667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu return (Object[]) makeDexElements.invoke(dexPathList, files, optimizedDirectory); 463667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } 464667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } 465667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 466667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu /** 467667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * Installer for platform versions 4 to 13. 468667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu */ 469667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu private static final class V4 { 47052eafa01b1e61e410cb4c5609eacee93c2a3e853Yohann Roussel private static void install(ClassLoader loader, List<File> additionalClassPathEntries) 471667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu throws IllegalArgumentException, IllegalAccessException, 472667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu NoSuchFieldException, IOException { 473667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu /* The patched class loader is expected to be a descendant of 474667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * dalvik.system.DexClassLoader. We modify its 47552eafa01b1e61e410cb4c5609eacee93c2a3e853Yohann Roussel * fields mPaths, mFiles, mZips and mDexs to append additional DEX 476667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu * file entries. 477667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu */ 478667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu int extraSize = additionalClassPathEntries.size(); 479667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 480667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu Field pathField = findField(loader, "path"); 481667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 482667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu StringBuilder path = new StringBuilder((String) pathField.get(loader)); 483667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu String[] extraPaths = new String[extraSize]; 484667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu File[] extraFiles = new File[extraSize]; 48566f379f774e06e650375750d18b1695ddb853371Maurice Chu ZipFile[] extraZips = new ZipFile[extraSize]; 486667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu DexFile[] extraDexs = new DexFile[extraSize]; 487667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu for (ListIterator<File> iterator = additionalClassPathEntries.listIterator(); 488667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu iterator.hasNext();) { 489667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu File additionalEntry = iterator.next(); 490667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu String entryPath = additionalEntry.getAbsolutePath(); 491667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu path.append(':').append(entryPath); 492667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu int index = iterator.previousIndex(); 493667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu extraPaths[index] = entryPath; 494667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu extraFiles[index] = additionalEntry; 49566f379f774e06e650375750d18b1695ddb853371Maurice Chu extraZips[index] = new ZipFile(additionalEntry); 496667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu extraDexs[index] = DexFile.loadDex(entryPath, entryPath + ".dex", 0); 497667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } 498667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 499667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu pathField.set(loader, path.toString()); 500667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu expandFieldArray(loader, "mPaths", extraPaths); 501667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu expandFieldArray(loader, "mFiles", extraFiles); 50266f379f774e06e650375750d18b1695ddb853371Maurice Chu expandFieldArray(loader, "mZips", extraZips); 503667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu expandFieldArray(loader, "mDexs", extraDexs); 504667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } 505667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu } 506667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu 507667f9a8a8155c41970a83be1414b57b5e37de336Maurice Chu} 508