OLD | NEW |
(Empty) | |
| 1 package org.chromium.deconstructed; |
| 2 |
| 3 import android.content.Context; |
| 4 import android.content.pm.ApplicationInfo; |
| 5 import android.util.Log; |
| 6 |
| 7 import java.io.File; |
| 8 import java.io.IOException; |
| 9 import java.lang.reflect.InvocationTargetException; |
| 10 import java.util.Arrays; |
| 11 import java.util.ArrayList; |
| 12 import java.util.List; |
| 13 |
| 14 public class JavaInstaller { |
| 15 private static final String TAG = "org.chromium.deconstructed.JavaInstaller"
; |
| 16 private static final String SECONDARY_FOLDER_NAME = |
| 17 "code_cache" + File.separator + "deconstructed-dexes"; |
| 18 |
| 19 public static void install(Context context, File dexDir) |
| 20 throws NoSuchFieldException, NoSuchMethodException, InvocationTarget
Exception { |
| 21 File[] dexFiles = dexDir.listFiles(); |
| 22 for (File f : dexFiles) { |
| 23 if (!isValidDex(f)) { |
| 24 return; |
| 25 } |
| 26 } |
| 27 ApplicationInfo appInfo = context.getApplicationInfo(); |
| 28 File optimizedDir = new File(appInfo.dataDir, SECONDARY_FOLDER_NAME); |
| 29 install(context.getClassLoader(), Arrays.asList(dexFiles), optimizedDir)
; |
| 30 } |
| 31 |
| 32 private static boolean isValidDex(File f) { |
| 33 // TODO |
| 34 return true; |
| 35 } |
| 36 |
| 37 private static void install(ClassLoader loader, List<File> additionalClassPa
thEntries, |
| 38 File optimizedDirectory) throws IllegalArgumentException, NoSuchFiel
dException, |
| 39 NoSuchMethodException, InvocationTar
getException { |
| 40 optimizedDirectory.mkdirs(); |
| 41 Object dexPathList = Reflect.getField(loader, "pathList"); |
| 42 ArrayList<IOException> suppressedExceptions = new ArrayList<IOException>
(); |
| 43 Object[] dexElements = (Object[]) Reflect.getField(dexPathList, "dexElem
ents"); |
| 44 Object[] injectedElements = makeDexElements(dexPathList, new ArrayList<F
ile>(additionalClassPathEntries), optimizedDirectory); |
| 45 Reflect.setField( |
| 46 dexPathList, "dexElements", Reflect.concatArrays(dexElements, in
jectedElements)); |
| 47 } |
| 48 |
| 49 private static Object[] makeDexElements(Object dexPathList, ArrayList<File>
files, |
| 50 File optimizedDirectory) throws NoSuchMethodException, InvocationTar
getException { |
| 51 ArrayList<IOException> suppressedExceptions = new ArrayList<IOException>
(); |
| 52 return (Object[]) Reflect.invokeMethod(dexPathList, "makeDexElements", f
iles, optimizedDirectory, suppressedExceptions); |
| 53 } |
| 54 |
| 55 } |
OLD | NEW |