1f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel// Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file 2f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel// for details. All rights reserved. Use of this source code is governed by a 3f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel// BSD-style license that can be found in the LICENSE file. 4f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 5f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Rousselpackage multidex004.fakelibrary; 6f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 7f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 8f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Rousselimport multidex004.fakeframeworks.Application; 9f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Rousselimport multidex004.fakeframeworks.Context; 10f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 11f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Rousselimport java.io.File; 12f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Rousselimport java.io.IOException; 13f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Rousselimport java.lang.reflect.Field; 14f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Rousselimport java.lang.reflect.InvocationTargetException; 15f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Rousselimport java.lang.reflect.Method; 16f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Rousselimport java.util.List; 17f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Rousselimport java.util.ListIterator; 18f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Rousselimport java.util.Random; 19f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Rousselimport java.util.Set; 20f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 21f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel/** 22f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * Monkey patches {@link Context#getClassLoader() the application context class 23f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * loader} in order to load classes from more than one dex file. The primary 24f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * {@code classes.dex} must contain the classes necessary for calling this 25f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * class methods. Secondary dex files named classes2.dex, classes3.dex... found 26f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * in the application apk will be added to the classloader after first call to 27f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * {@link #install(Context)}. 28f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * 29f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * <p/> 30f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * This library provides compatibility for platforms with API level 4 through 20. This library does 31f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * nothing on newer versions of the platform which provide built-in support for secondary dex files. 32f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel */ 33f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Rousselpublic final class MultiDex { 34f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 35f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel static final String TAG = "MultiDex"; 36f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 37f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel private static final String SECONDARY_FOLDER_NAME = "secondary-dexes"; 38f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 39f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel private static final int MAX_SUPPORTED_SDK_VERSION = 20; 40f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 41f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel private static final int MIN_SDK_VERSION = 4; 42f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 43f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel private static final int VM_WITH_MULTIDEX_VERSION_MAJOR = 2; 44f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 45f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel private static final int VM_WITH_MULTIDEX_VERSION_MINOR = 1; 46f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 47f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel private static final Set<String> installedApk = null; 48f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 49f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel private static final boolean IS_VM_MULTIDEX_CAPABLE = 50f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel isVMMultidexCapable(System.getProperty("java.vm.version")); 51f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 52f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel private MultiDex() {} 53f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 54f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel /** 55f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * Patches the application context class loader by appending extra dex files 56f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * loaded from the application apk. This method should be called in the 57f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * attachBaseContext of your {@link Application}, see 58f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * {@link MultiDexApplication} for more explanation and an example. 59f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * 60f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * @param context application context. 61f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * @throws RuntimeException if an error occurred preventing the classloader 62f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * extension. 63f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel */ 64f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel public static void install(Context context) { 65f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel if (IS_VM_MULTIDEX_CAPABLE) { 66f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel try { 67f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel clearOldDexDir(context); 68f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel } catch (Throwable t) { 69f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel } 70f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel return; 71f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel } 72f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel } 73f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 74f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel /** 75f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * Identifies if the current VM has a native support for multidex, meaning there is no need for 76f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * additional installation by this library. 77f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * @return true if the VM handles multidex 78f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel */ 79f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel /* package visible for test */ 80f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel static boolean isVMMultidexCapable(String versionString) { 81f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel boolean isMultidexCapable = false; 82f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel if (versionString != null) { 83f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel } 84f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel return isMultidexCapable; 85f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel } 86f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 87f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel private static void installSecondaryDexes(ClassLoader loader, File dexDir, List<File> files) 88f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, 89f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel InvocationTargetException, NoSuchMethodException, IOException { 90f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel int version = new Random().nextInt(23); 91f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel if (!files.isEmpty()) { 92f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel if (version >= 19) { 93f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel V19.install(loader, files, dexDir); 94f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel } else if (version >= 14) { 95f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel V14.install(loader, files, dexDir); 96f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel } else { 97f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel V4.install(loader, files); 98f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel } 99f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel } 100f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel } 101f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 102f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel /** 103f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * Returns whether all files in the list are valid zip files. If {@code files} is empty, then 104f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * returns true. 105f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel */ 106f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel private static boolean checkValidZipFiles(List<File> files) { 107f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel for (File file : files) { 108f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel } 109f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel return true; 110f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel } 111f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 112f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel /** 113f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * Locates a given field anywhere in the class inheritance hierarchy. 114f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * 115f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * @param instance an object to search the field into. 116f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * @param name field name 117f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * @return a field object 118f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * @throws NoSuchFieldException if the field cannot be located 119f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel */ 120f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel private static Field findField(Object instance, String name) throws NoSuchFieldException { 121f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) { 122f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel try { 123f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel Field field = clazz.getDeclaredField(name); 124f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 125f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 126f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel if (!field.isAccessible()) { 127f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel field.setAccessible(true); 128f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel } 129f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 130f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel return field; 131f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel } catch (NoSuchFieldException e) { 132f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel // ignore and search next 133f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel } 134f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel } 135f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 136f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel throw new NoSuchFieldException("Field " + name + " not found in " + instance.getClass()); 137f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel } 138f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 139f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel /** 140f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * Locates a given method anywhere in the class inheritance hierarchy. 141f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * 142f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * @param instance an object to search the method into. 143f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * @param name method name 144f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * @param parameterTypes method parameter types 145f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * @return a method object 146f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * @throws NoSuchMethodException if the method cannot be located 147f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel */ 148f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel private static Method findMethod(Object instance, String name, Class<?>... parameterTypes) 149f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel throws NoSuchMethodException { 150f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) { 151f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel try { 152f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel Method method = clazz.getDeclaredMethod(name, parameterTypes); 153f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 154f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 155f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel if (!method.isAccessible()) { 156f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel method.setAccessible(true); 157f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel } 158f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 159f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel return method; 160f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel } catch (NoSuchMethodException e) { 161f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel // ignore and search next 162f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel } 163f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel } 164f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 165f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel throw new NoSuchMethodException("Method " + name + " with parameters " + 166f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel parameterTypes + " not found in " + instance.getClass()); 167f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel } 168f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 169f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel /** 170f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * Replace the value of a field containing a non null array, by a new array containing the 171f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * elements of the original array plus the elements of extraElements. 172f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * @param instance the instance whose field is to be modified. 173f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * @param fieldName the field to modify. 174f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * @param extraElements elements to append at the end of the array. 175f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel */ 176f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel private static void expandFieldArray(Object instance, String fieldName, 177f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel Object[] extraElements) throws NoSuchFieldException, IllegalArgumentException, 178f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel IllegalAccessException { 179f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel Field jlrField = findField(instance, fieldName); 180f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel Object[] original = (Object[]) jlrField.get(instance); 181f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel Object[] combined = (Object[]) null; 182f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel System.arraycopy(original, 0, combined, 0, original.length); 183f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel System.arraycopy(extraElements, 0, combined, original.length, extraElements.length); 184f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel jlrField.set(instance, combined); 185f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel } 186f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 187f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel private static void clearOldDexDir(Context context) throws Exception { 188f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 189f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel } 190f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 191f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel /** 192f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * Installer for platform versions 19. 193f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel */ 194f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel private static final class V19 { 195f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 196f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel private static void install(ClassLoader loader, List<File> additionalClassPathEntries, 197f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel File optimizedDirectory) 198f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel throws IllegalArgumentException, IllegalAccessException, 199f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel NoSuchFieldException, InvocationTargetException, NoSuchMethodException { 200f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel /* The patched class loader is expected to be a descendant of 201f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * dalvik.system.BaseDexClassLoader. We modify its 202f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * dalvik.system.DexPathList pathList field to append additional DEX 203f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * file entries. 204f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel */ 205f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel Field pathListField = findField(loader, "pathList"); 206f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel Object dexPathList = pathListField.get(loader); 207f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel } 208f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 209f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel } 210f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel /** 211f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * Installer for platform versions 14, 15, 16, 17 and 18. 212f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel */ 213f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel private static final class V14 { 214f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 215f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel private static void install(ClassLoader loader, List<File> additionalClassPathEntries, 216f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel File optimizedDirectory) 217f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel throws IllegalArgumentException, IllegalAccessException, 218f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel NoSuchFieldException, InvocationTargetException, NoSuchMethodException { 219f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel /* The patched class loader is expected to be a descendant of 220f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * dalvik.system.BaseDexClassLoader. We modify its 221f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * dalvik.system.DexPathList pathList field to append additional DEX 222f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * file entries. 223f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel */ 224f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel Field pathListField = findField(loader, "pathList"); 225f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel Object dexPathList = pathListField.get(loader); 226f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel } 227f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 228f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel } 229f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 230f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel /** 231f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * Installer for platform versions 4 to 13. 232f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel */ 233f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel private static final class V4 { 234f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel private static void install(ClassLoader loader, List<File> additionalClassPathEntries) 235f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel throws IllegalArgumentException, IllegalAccessException, 236f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel NoSuchFieldException, IOException { 237f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel /* The patched class loader is expected to be a descendant of 238f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * dalvik.system.DexClassLoader. We modify its 239f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * fields mPaths, mFiles, mZips and mDexs to append additional DEX 240f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel * file entries. 241f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel */ 242f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel int extraSize = additionalClassPathEntries.size(); 243f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 244f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel Field pathField = findField(loader, "path"); 245f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 246f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel StringBuilder path = new StringBuilder((String) pathField.get(loader)); 247f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel String[] extraPaths = new String[extraSize]; 248f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel File[] extraFiles = new File[extraSize]; 249f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel for (ListIterator<File> iterator = additionalClassPathEntries.listIterator(); 250f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel iterator.hasNext();) { 251f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel File additionalEntry = iterator.next(); 252f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel String entryPath = additionalEntry.getAbsolutePath(); 253f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel path.append(':').append(entryPath); 254f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel int index = iterator.previousIndex(); 255f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel extraPaths[index] = entryPath; 256f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel extraFiles[index] = additionalEntry; 257f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel } 258f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 259f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel pathField.set(loader, path.toString()); 260f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel expandFieldArray(loader, "mPaths", extraPaths); 261f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel expandFieldArray(loader, "mFiles", extraFiles); 262f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel } 263f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel } 264f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel 265f820a57655a3b5712825de91bf9d95b9d88e84bfYohann Roussel} 266