dalvik.system.DexFile Java Examples

The following examples show how to use dalvik.system.DexFile. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AndroidPrototypeFactorySetup.java    From commcare-android with Apache License 2.0 6 votes vote down vote up
/**
 * Scans all classes accessible from the context class loader which belong to the given package and subpackages.
 */
public static List<String> getClasses(Context c)
        throws IOException {
    ArrayList<String> classNames = new ArrayList<>();

    String zpath = c.getApplicationInfo().sourceDir;


    if (zpath == null) {
        zpath = "/data/app/org.commcare.android.apk";
    }

    DexFile df = new DexFile(new File(zpath));
    for (Enumeration<String> en = df.entries(); en.hasMoreElements(); ) {
        String cn = en.nextElement();
        loadClass(cn, classNames);
    }
    df.close();

    return classNames;
}
 
Example #2
Source File: ModelRegistryUtils.java    From framework with GNU Affero General Public License v3.0 6 votes vote down vote up
public void makeReady(Context context) {
    try {
        DexFile dexFile = new DexFile(context.getPackageCodePath());
        for (Enumeration<String> item = dexFile.entries(); item.hasMoreElements(); ) {
            String element = item.nextElement();
            if (element.startsWith(App.class.getPackage().getName())) {
                Class<? extends OModel> clsName = (Class<? extends OModel>) Class.forName(element);
                if (clsName != null && clsName.getSuperclass() != null &&
                        OModel.class.isAssignableFrom(clsName.getSuperclass())) {
                    String modelName = getModelName(context, clsName);
                    if (modelName != null) {
                        this.models.put(modelName, clsName);
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #3
Source File: RocooFix.java    From RocooFix with MIT License 6 votes vote down vote up
private static void install(ClassLoader loader, List<File> additionalClassPathEntries,
                            File optimizedDirectory)
        throws IllegalArgumentException, IllegalAccessException,
        NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException {

    Field pathListField = RocooUtils.findField(loader, "pathList");
    Object dexPathList = pathListField.get(loader);
    Field dexElement = RocooUtils.findField(dexPathList, "dexElements");
    Class<?> elementType = dexElement.getType().getComponentType();
    Method loadDex = RocooUtils.findMethod(dexPathList, "loadDexFile", File.class, File.class);
    loadDex.setAccessible(true);

    Object dex = loadDex.invoke(null, additionalClassPathEntries.get(0), optimizedDirectory);
    Constructor<?> constructor = elementType.getConstructor(File.class, boolean.class, File.class, DexFile.class);
    constructor.setAccessible(true);
    Object element = constructor.newInstance(new File(""), false, additionalClassPathEntries.get(0), dex);

    Object[] newEles = new Object[1];
    newEles[0] = element;
    RocooUtils.expandFieldArray(dexPathList, "dexElements", newEles);
}
 
Example #4
Source File: RocooFix.java    From RocooFix with MIT License 6 votes vote down vote up
private static void install(ClassLoader loader, List<File> additionalClassPathEntries,
                            File optimizedDirectory)
        throws IllegalArgumentException, IllegalAccessException,
        NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, ClassNotFoundException {

    Field pathListField = RocooUtils.findField(loader, "pathList");
    Object dexPathList = pathListField.get(loader);
    Field dexElement = RocooUtils.findField(dexPathList, "dexElements");
    Class<?> elementType = dexElement.getType().getComponentType();
    Method loadDex = RocooUtils.findMethod(dexPathList, "loadDexFile", File.class, File.class, ClassLoader.class, dexElement.getType());
    loadDex.setAccessible(true);

    Object dex = loadDex.invoke(null, additionalClassPathEntries.get(0), optimizedDirectory, loader, dexElement.get(dexPathList));
    Constructor<?> constructor = elementType.getConstructor(File.class, boolean.class, File.class, DexFile.class);
    constructor.setAccessible(true);
    Object element = constructor.newInstance(new File(""), false, additionalClassPathEntries.get(0), dex);

    Object[] newEles = new Object[1];
    newEles[0] = element;
    RocooUtils.expandFieldArray(dexPathList, "dexElements", newEles);
}
 
Example #5
Source File: DelegatingClassLoader.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Clear the existing delegate and return the new one, populated with the given dex files
 *
 * @param dexJars the .dex.jar files which will be managed by our delegate
 */
void resetDelegate(List<File> dexJars) {
  mDelegate = new PathClassLoader("", "", this);
  mManagedClassesToDexFile.clear();
  for (File dexJar : dexJars) {
    try {
      final File optFile = new File(mDexOptDir, dexJar.getName());
      DexFile dexFile = DexFile.loadDex(dexJar.getCanonicalPath(), optFile.getCanonicalPath(), 0);
      final Enumeration<String> entries = dexFile.entries();
      while (entries.hasMoreElements()) {
        mManagedClassesToDexFile.put(entries.nextElement(), dexFile);
      }
    } catch (IOException e) {
      // Pass for now
    }
  }
}
 
Example #6
Source File: AndroidComponentScanner.java    From mini2Dx with Apache License 2.0 6 votes vote down vote up
@Override
public void scan(String[] packageNames) throws IOException {
	if (applicationContext == null) {
		throw new NullPointerException(
				AndroidComponentScanner.class.getSimpleName()
						+ ".APPLICATION_CONTEXT needs to be set before initialising "
						+ DependencyInjection.class.getSimpleName());
	}

	DexFile dex = new DexFile(
			applicationContext.getApplicationInfo().sourceDir);
	ClassLoader classLoader = Thread.currentThread()
			.getContextClassLoader();
	Enumeration<String> entries = dex.entries();
	while (entries.hasMoreElements()) {
		String className = entries.nextElement();
		if (classNameContainsPackage(packageNames, className)) {
			try {
				checkClassForAnnotations(classLoader.loadClass(className));
			} catch (Exception e) {
				Log.wtf(TAG, e);
			}
		}
	}
}
 
Example #7
Source File: MultiDex.java    From letv with Apache License 2.0 6 votes vote down vote up
private static void install(ClassLoader loader, List<File> additionalClassPathEntries) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, IOException {
    int extraSize = additionalClassPathEntries.size();
    Field pathField = MultiDex.findField(loader, "path");
    StringBuilder path = new StringBuilder((String) pathField.get(loader));
    String[] extraPaths = new String[extraSize];
    File[] extraFiles = new File[extraSize];
    ZipFile[] extraZips = new ZipFile[extraSize];
    DexFile[] extraDexs = new DexFile[extraSize];
    ListIterator<File> iterator = additionalClassPathEntries.listIterator();
    while (iterator.hasNext()) {
        File additionalEntry = (File) iterator.next();
        String entryPath = additionalEntry.getAbsolutePath();
        path.append(':').append(entryPath);
        int index = iterator.previousIndex();
        extraPaths[index] = entryPath;
        extraFiles[index] = additionalEntry;
        extraZips[index] = new ZipFile(additionalEntry);
        extraDexs[index] = DexFile.loadDex(entryPath, entryPath + ".dex", 0);
    }
    pathField.set(loader, path.toString());
    MultiDex.expandFieldArray(loader, "mPaths", extraPaths);
    MultiDex.expandFieldArray(loader, "mFiles", extraFiles);
    MultiDex.expandFieldArray(loader, "mZips", extraZips);
    MultiDex.expandFieldArray(loader, "mDexs", extraDexs);
}
 
Example #8
Source File: ObjectHelper.java    From ParcelCheck with Apache License 2.0 6 votes vote down vote up
/**
 * Gets all of the classes within a package
 * @param context the context
 * @param packageName the package name to fetch classes from
 * @return an list of named classes within package
 */
public static ArrayList<String> getClassesOfPackage(Context context, String packageName) {
    ArrayList<String> classes = new ArrayList<>();
    try {
        String packageCodePath = context.getPackageCodePath();
        DexFile df = new DexFile(packageCodePath);
        for (Enumeration<String> iter = df.entries(); iter.hasMoreElements(); ) {
            String className = iter.nextElement();
            if (className.contains(packageName)) {
                classes.add(className.substring(className.lastIndexOf(".") + 1, className.length()));
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return classes;
}
 
Example #9
Source File: ReflectAccelerator.java    From Small with Apache License 2.0 6 votes vote down vote up
public static boolean expandDexPathList(ClassLoader cl,
                                        String[] dexPaths, DexFile[] dexFiles) {
    try {
        int N = dexPaths.length;
        Object[] elements = new Object[N];
        for (int i = 0; i < N; i++) {
            String dexPath = dexPaths[i];
            File pkg = new File(dexPath);
            DexFile dexFile = dexFiles[i];
            elements[i] = makeDexElement(pkg, dexFile);
        }

        fillDexPathList(cl, elements);
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}
 
Example #10
Source File: InterceptingClassLoader.java    From FuzzDroid with Apache License 2.0 6 votes vote down vote up
public static Class<?> loadClass(DexFile dexFile, String className, ClassLoader classLoader) throws ClassNotFoundException {
	try {
		Log.i(SharedClassesSettings.TAG, "Loading class " + className);
		// Try the default class loader
		return Class.forName(className);
	}
	catch (ClassNotFoundException ex) {
		try {
			// Try the given class loader
			return classLoader.loadClass(className);
		}
		catch (ClassNotFoundException ex2) {
			// We have no other choice than using the original class loading
			Log.w(SharedClassesSettings.TAG, "Could not intercept class loading");
			return dexFile.loadClass(className, classLoader);
		}
	}
}
 
Example #11
Source File: Utils.java    From QNotified with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 仅仅为群发器而使用本模块的用户往往有两个鲜明的特征
 * 1.使用某个虚拟框架
 * 2.显而易见的昵称,见{@link #isBadNick(String)}
 * 仍然提供本模块的全部功能
 * 只是隐藏我的联系方式
 * 未必是完全正确的方法, but just do it.
 **/
private static boolean isExp() {
    try {
        Object pathList = iget_object_or_null(XposedBridge.class.getClassLoader(), "pathList");
        Object[] dexElements = (Object[]) iget_object_or_null(pathList, "dexElements");
        for (Object entry : dexElements) {
            DexFile dexFile = (DexFile) iget_object_or_null(entry, "dexFile");
            Enumeration<String> entries = dexFile.entries();
            while (entries.hasMoreElements()) {
                String className = entries.nextElement();
                if (className.matches(".+?(epic|weishu).+")) {
                    return true;
                }
            }
        }
    } catch (Throwable e) {
        if (!(e instanceof NullPointerException) &&
                !(e instanceof NoClassDefFoundError)) {
            log(e);
        }
    }
    return false;
}
 
Example #12
Source File: DroidNubeKit.java    From DroidNubeKit with MIT License 6 votes vote down vote up
private Set<Class<?>> getClasspathClasses() throws IOException, ClassNotFoundException {
    Set<Class<?>> classes = new HashSet<Class<?>>();
    DexFile dex = new DexFile(getContext().getApplicationInfo().sourceDir);
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    Enumeration<String> entries = dex.entries();
    while (entries.hasMoreElements()) {
        String entry = entries.nextElement();
        if (entry.toLowerCase().startsWith(getContext().getPackageName().toLowerCase())) {

            Class<?> clazz = classLoader.loadClass(entry);
            if(clazz.isAnnotationPresent(RecordType.class)) {
                modelClasses.add(clazz);
            }
            classes.add(clazz);
        }
    }
    return classes;
}
 
Example #13
Source File: DexFileCompat.java    From atlas with Apache License 2.0 6 votes vote down vote up
static public DexFile loadDex(Context context, String sourcePathName, String outputPathName,
                              int flags) throws Exception {
    if(Build.VERSION.SDK_INT<=15) {
        return DexFile.loadDex(sourcePathName,outputPathName,flags);
    }else{
        DexFile dexFile = DexFile.loadDex(context.getApplicationInfo().sourceDir,null,0);
        try {
            int cookie = (int)openDexFile.invoke(null,sourcePathName,outputPathName,flags);
            mFileName.set(dexFile,sourcePathName);
            mCookie.set(dexFile,cookie);
        } catch (Exception e) {
            throw  e;
        }
        return dexFile;
    }
}
 
Example #14
Source File: Smirk.java    From Smirk with MIT License 6 votes vote down vote up
private <T> List<Class<T>> findSubClassesFromDexFile(Map.Entry<DexFile, ExtensionClassLoader> dexEntry, Class<T> clazz) {
    List<Class<T>> list = new ArrayList<>();
    Enumeration<String> entries = dexEntry.getKey().entries();
    while (entries.hasMoreElements()) {
        String name = entries.nextElement();
        Class cla = null;
        try {
            cla = dexEntry.getValue().loadClass(name);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        if (cla == null)
            continue;
        if (clazz.isAssignableFrom(cla)) {
            list.add(cla);
        }
    }
    return list;
}
 
Example #15
Source File: BundleReleaser.java    From atlas with Apache License 2.0 6 votes vote down vote up
public boolean checkDexValid(DexFile odexFile) throws IOException {
    if (DexReleaser.isArt()) {
        String applicationName = KernalConstants.RAW_APPLICATION_NAME;
        try {
            Enumeration<String> enumeration = odexFile.entries();
            while (enumeration.hasMoreElements()) {
                if (enumeration.nextElement().replace("/", ".").equals(applicationName)) {
                    return true;
                }
            }
            return false;
        } catch (Throwable e) {
            e.printStackTrace();
            return false;
        }
    }
    return true;
}
 
Example #16
Source File: Smirk.java    From Smirk with MIT License 6 votes vote down vote up
private Map<DexFile, ExtensionClassLoader> loadDex(File dexPath) {

            if (dexPath == null || !dexPath.exists()) {
                return null;
            }

            Map<DexFile, ExtensionClassLoader> dexMap = new HashMap<>();

            if (dexPath.isDirectory()) {
                File[] files = dexPath.listFiles();
                for (File file : files) {
                    if (file.isDirectory()) {
                        dexMap.putAll(loadDex(file));
                        continue;
                    }
                    if (file.getName().endsWith(".dex")) {
                        putDex(file.getAbsoluteFile(), dexMap);
                    }
                }
            } else {
                if (dexPath.getName().endsWith(".dex")) {
                    putDex(dexPath, dexMap);
                }
            }
            return dexMap;
        }
 
Example #17
Source File: ClassUtils.java    From Cangol-appcore with Apache License 2.0 6 votes vote down vote up
/**
 * 获取dexFile所有类
 *
 * @param context
 * @return
 */
public static List<String> getAllClassNameFromDexFile(Context context, String packageName) {
    final List<String> classList = new ArrayList<>();
    try {
        final DexFile df = new DexFile(context.getPackageCodePath());
        String str;
        for (final Enumeration<String> iter = df.entries(); iter.hasMoreElements(); ) {
            str = iter.nextElement();
            if ((packageName != null && str.startsWith(packageName))
                    || (packageName == null || "".equals(packageName))) {
                classList.add(str);
            }
        }
        df.close();
    } catch (IOException e) {
        Log.e("IOException " + e.getMessage());
    }
    return classList;
}
 
Example #18
Source File: Smirk.java    From Smirk with MIT License 5 votes vote down vote up
private void putDex(File dexPath, Map<DexFile, ExtensionClassLoader> dexMap) {
    try {
        File outPath = mContext.getDir("smirk", 0);
        DexFile dexFile = DexFile.loadDex(dexPath.getAbsolutePath(), new File(outPath, dexPath.getName()).getAbsolutePath(), 0);
        ExtensionClassLoader classLoader = new ExtensionClassLoader(dexPath.getAbsolutePath(), outPath.getAbsolutePath(), null, mContext.getClassLoader());
        dexMap.put(dexFile, classLoader);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #19
Source File: Smirk.java    From Smirk with MIT License 5 votes vote down vote up
public Builder addDexPath(File dexPath) {
    Map<DexFile, ExtensionClassLoader> map = loadDex(dexPath);
    if (map != null) {
        mDexFiles.putAll(map);
    }
    return this;
}
 
Example #20
Source File: CommonUtil.java    From Aria with Apache License 2.0 5 votes vote down vote up
/**
 * 获取指定包名下的所有类
 *
 * @param path dex路径
 * @param filterClass 需要过滤的类
 */
public static List<String> getPkgClassName(String path, String filterClass) {
  List<String> list = new ArrayList<>();
  try {
    File file = new File(path);
    if (!file.exists()) {
      ALog.w(TAG, String.format("路径【%s】下的Dex文件不存在", path));
      return list;
    }

    DexFile df = new DexFile(path);//通过DexFile查找当前的APK中可执行文件
    Enumeration<String> enumeration = df.entries();//获取df中的元素  这里包含了所有可执行的类名 该类名包含了包名+类名的方式
    while (enumeration.hasMoreElements()) {
      String _className = enumeration.nextElement();
      if (!_className.contains(filterClass)) {
        continue;
      }
      if (_className.contains(filterClass)) {
        list.add(_className);
      }
    }
    df.close();
  } catch (IOException e) {
    e.printStackTrace();
  }
  return list;
}
 
Example #21
Source File: ReflectAccelerator.java    From Small with Apache License 2.0 5 votes vote down vote up
public static boolean expandDexPathList(ClassLoader cl, String[] dexPaths, DexFile[] dexFiles) {
    if (Build.VERSION.SDK_INT < 14) {
        return V9_13.expandDexPathList(cl, dexPaths, dexFiles);
    } else {
        return V14_.expandDexPathList(cl, dexPaths, dexFiles);
    }
}
 
Example #22
Source File: DexArchive.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void initDexFile() {
  String dexopt = new File(bundleDir, "dexopt" + revision + (subId > 0 ? "_" + subId : "")).getAbsolutePath();
  if (jar != null) {
    if (jar.getEntry(CLASSES_DEX) != null) {
      dexFile = DexFile.loadDex(file.getAbsolutePath(),  dexopt, 0);
    }
  } else {
    dexFile = DexFile.loadDex(new File(file, CLASSES_DEX).getAbsolutePath(), dexopt, 0);
  }
}
 
Example #23
Source File: Smirk.java    From Smirk with MIT License 5 votes vote down vote up
private <T> List<Class<T>> findSubClasses(Class<T> clazz) {
    List<Class<T>> list = new ArrayList<>();
    Set<Map.Entry<DexFile, ExtensionClassLoader>> entries = mDexFiles.entrySet();
    for (Map.Entry<DexFile, ExtensionClassLoader> dexEntry : entries) {
        list.addAll(findSubClassesFromDexFile(dexEntry, clazz));
    }
    return list;
}
 
Example #24
Source File: BundleReleaser.java    From atlas with Apache License 2.0 5 votes vote down vote up
private boolean verifyDexFile(DexFile dexFile,String optimizedPath) throws IOException {
    if (dexFile != null) {
        if(externalStorage){
            return true;
        }
        if (!checkDexValid(dexFile)) {
            return false;
        }

        return true;
    }
    return false;
}
 
Example #25
Source File: DynamicDexClassLoder.java    From ApkToolPlus with Apache License 2.0 5 votes vote down vote up
/**
 * 直接从内存加载 新构造
 *
 * @param dexBytes
 * @param libraryPath
 * @param parent
 * @throws Exception
 */

public DynamicDexClassLoder(Context context, byte[] dexBytes,
                            String libraryPath, ClassLoader parent, String oriPath,
                            String fakePath) {
    super(oriPath, fakePath, libraryPath, parent);
    setContext(context);
    // FIXME 解密dex和openDexFile放在native层实现
    Integer cookie = (Integer) Reflect.invokeMethod(DexFile.class, null, "openDexFile", new Object[]{dexBytes}, byte[].class);
    setCookie(cookie);
}
 
Example #26
Source File: NClassLoader.java    From atlas with Apache License 2.0 5 votes vote down vote up
public static void replacePathClassLoader(Context base,ClassLoader original,NClassLoader target) throws Exception {
        NClassLoader loader = target;
        Field pathListField = findField(original, "pathList");
        pathListField.setAccessible(true);
        Object originPathListObject = pathListField.get(original);
//
        Field definingContextField = findField(originPathListObject, "definingContext");
        definingContextField.set(originPathListObject, loader);

        Field loadPathList = findField(loader, "pathList");
        //just use PathClassloader's pathList
        loadPathList.set(loader, originPathListObject);

        List<File> additionalClassPathEntries = new ArrayList<File>();
        Field dexElement = findField(originPathListObject, "dexElements");
        Object[] originDexElements = (Object[]) dexElement.get(originPathListObject);
        for (Object element : originDexElements) {
            DexFile dexFile = (DexFile) findField(element, "dexFile").get(element);
            additionalClassPathEntries.add(new File(dexFile.getName()));
            //protect for java.lang.AssertionError: Failed to close dex file in finalizer.
//            oldDexFiles.add(dexFile);
        }
        Method makePathElements = findMethod(originPathListObject, "makePathElements", List.class, File.class,
                List.class);
        ArrayList<IOException> suppressedExceptions = new ArrayList<IOException>();
        Object[] newDexElements = (Object[]) makePathElements.invoke(originPathListObject, additionalClassPathEntries, null, suppressedExceptions);
        dexElement.set(originPathListObject, newDexElements);

        Field mPackageInfoField = base.getClass().getDeclaredField("mPackageInfo");
        mPackageInfoField.setAccessible(true);
        Object loadedApk = mPackageInfoField.get(base);
        Field classloaderField = loadedApk.getClass().getDeclaredField("mClassLoader");
        classloaderField.setAccessible(true);
        classloaderField.set(loadedApk,loader);
        Thread.currentThread().setContextClassLoader(loader);
    }
 
Example #27
Source File: SSJDescriptor.java    From ssj with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @param dexFile DexFile
 */
private void scanDex(DexFile dexFile)
{
	Enumeration<String> classNames = dexFile.entries();
	while (classNames.hasMoreElements())
	{
		String className = classNames.nextElement();
		if (className.startsWith("hcm.ssj.") && !className.contains("$"))
		{
			hsClassNames.add(className);
		}
	}
}
 
Example #28
Source File: VirtualCore.java    From container with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Optimize the Dalvik-Cache for the specified package.
 *
 * @param pkg package name
 * @throws IOException
 */
public void preOpt(String pkg) throws IOException {
    AppSetting info = findApp(pkg);
    if (info != null && !info.dependSystem) {
        DexFile.loadDex(info.apkPath, info.getOdexFile().getPath(), 0).close();
    }
}
 
Example #29
Source File: MultiDex.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
private static void install(ClassLoader loader, List<File> additionalClassPathEntries)
                throws IllegalArgumentException, IllegalAccessException,
                NoSuchFieldException, IOException {
    /* The patched class loader is expected to be a descendant of
     * dalvik.system.DexClassLoader. We modify its
     * fields mPaths, mFiles, mZips and mDexs to append additional DEX
     * file entries.
     */
    int extraSize = additionalClassPathEntries.size();

    Field pathField = findField(loader, "path");

    StringBuilder path = new StringBuilder((String) pathField.get(loader));
    String[] extraPaths = new String[extraSize];
    File[] extraFiles = new File[extraSize];
    ZipFile[] extraZips = new ZipFile[extraSize];
    DexFile[] extraDexs = new DexFile[extraSize];
    for (ListIterator<File> iterator = additionalClassPathEntries.listIterator();
            iterator.hasNext();) {
        File additionalEntry = iterator.next();
        String entryPath = additionalEntry.getAbsolutePath();
        path.append(':').append(entryPath);
        int index = iterator.previousIndex();
        extraPaths[index] = entryPath;
        extraFiles[index] = additionalEntry;
        extraZips[index] = new ZipFile(additionalEntry);
        extraDexs[index] = DexFile.loadDex(entryPath, entryPath + ".dex", 0);
    }

    pathField.set(loader, path.toString());
    expandFieldArray(loader, "mPaths", extraPaths);
    expandFieldArray(loader, "mFiles", extraFiles);
    expandFieldArray(loader, "mZips", extraZips);
    expandFieldArray(loader, "mDexs", extraDexs);
}
 
Example #30
Source File: MultiDex.java    From atlas with Apache License 2.0 5 votes vote down vote up
private static void install(ClassLoader loader,
                            List<? extends File> additionalClassPathEntries)
        throws IllegalArgumentException, IllegalAccessException,
        NoSuchFieldException, IOException {
    /* The patched class loader is expected to be a descendant of
     * dalvik.system.DexClassLoader. We modify its
     * fields mPaths, mFiles, mZips and mDexs to append additional DEX
     * file entries.
     */
    int extraSize = additionalClassPathEntries.size();

    Field pathField = findField(loader, "path");

    StringBuilder path = new StringBuilder((String) pathField.get(loader));
    String[] extraPaths = new String[extraSize];
    File[] extraFiles = new File[extraSize];
    ZipFile[] extraZips = new ZipFile[extraSize];
    DexFile[] extraDexs = new DexFile[extraSize];
    for (ListIterator<? extends File> iterator = additionalClassPathEntries.listIterator();
         iterator.hasNext(); ) {
        File additionalEntry = iterator.next();
        String entryPath = additionalEntry.getAbsolutePath();
        path.append(':').append(entryPath);
        int index = iterator.previousIndex();
        extraPaths[index] = entryPath;
        extraFiles[index] = additionalEntry;
        extraZips[index] = new ZipFile(additionalEntry);
        extraDexs[index] = DexFile.loadDex(entryPath, entryPath + ".dex", 0);
    }

    pathField.set(loader, path.toString());
    expandFieldArray(loader, "mPaths", extraPaths);
    expandFieldArray(loader, "mFiles", extraFiles);
    expandFieldArray(loader, "mZips", extraZips);
    expandFieldArray(loader, "mDexs", extraDexs);
}