Search in sources :

Example 26 with DexFile

use of dalvik.system.DexFile in project atlas by alibaba.

the class DexFileCompat method loadDex.

public static 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;
    }
}
Also used : DexFile(dalvik.system.DexFile)

Example 27 with DexFile

use of dalvik.system.DexFile in project atlas by alibaba.

the class NClassLoader method replacePathClassLoader.

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);
}
Also used : Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) IOException(java.io.IOException) DexFile(dalvik.system.DexFile) File(java.io.File) DexFile(dalvik.system.DexFile)

Example 28 with DexFile

use of dalvik.system.DexFile in project SmartAndroidSource by jaychou2012.

the class ModelInfo method scanForModel.

private void scanForModel(Context context) throws IOException {
    String packageName = context.getPackageName();
    String sourcePath = context.getApplicationInfo().sourceDir;
    List<String> paths = new ArrayList<String>();
    if (sourcePath != null && !(new File(sourcePath).isDirectory())) {
        DexFile dexfile = new DexFile(sourcePath);
        Enumeration<String> entries = dexfile.entries();
        while (entries.hasMoreElements()) {
            paths.add(entries.nextElement());
        }
    } else // Robolectric fallback
    {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        Enumeration<URL> resources = classLoader.getResources("");
        while (resources.hasMoreElements()) {
            String path = resources.nextElement().getFile();
            if (path.contains("bin") || path.contains("classes")) {
                paths.add(path);
            }
        }
    }
    for (String path : paths) {
        File file = new File(path);
        scanForModelClasses(file, packageName, context.getClassLoader());
    }
}
Also used : ArrayList(java.util.ArrayList) DexFile(dalvik.system.DexFile) File(java.io.File) DexFile(dalvik.system.DexFile) URL(java.net.URL)

Example 29 with DexFile

use of dalvik.system.DexFile in project dexposed by alibaba.

the class PatchMain method loadAllCallbacks.

private static PatchResult loadAllCallbacks(Context context, String apkPath, ClassLoader cl) {
    try {
        // String dexPath = new File(context.getFilesDir(), apkPath.).getAbsolutePath();
        File dexoptFile = new File(apkPath + "odex");
        if (dexoptFile.exists()) {
            dexoptFile.delete();
        }
        ClassLoader mcl = null;
        try {
            mcl = new DexClassLoader(apkPath, context.getFilesDir().getAbsolutePath(), null, cl);
        } catch (Throwable e) {
            return new PatchResult(false, PatchResult.FOUND_PATCH_CLASS_EXCEPTION, "Find patch class exception ", e);
        }
        DexFile dexFile = DexFile.loadDex(apkPath, context.getFilesDir().getAbsolutePath() + File.separator + "patch.odex", 0);
        Enumeration<String> entrys = dexFile.entries();
        // clean old callback
        synchronized (loadedPatchCallbacks) {
            loadedPatchCallbacks.clear();
        }
        while (entrys.hasMoreElements()) {
            String entry = entrys.nextElement();
            Class<?> entryClass = null;
            try {
                entryClass = mcl.loadClass(entry);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                break;
            }
            if (isImplementInterface(entryClass, IPatch.class)) {
                Object moduleInstance = entryClass.newInstance();
                hookLoadPatch(new PatchCallback((IPatch) moduleInstance));
            }
        }
    } catch (Exception e) {
        return new PatchResult(false, PatchResult.FOUND_PATCH_CLASS_EXCEPTION, "Find patch class exception ", e);
    }
    return new PatchResult(true, PatchResult.NO_ERROR, "");
}
Also used : DexClassLoader(dalvik.system.DexClassLoader) DexFile(dalvik.system.DexFile) DexClassLoader(dalvik.system.DexClassLoader) DexFile(dalvik.system.DexFile) File(java.io.File)

Example 30 with DexFile

use of dalvik.system.DexFile in project freeline by alibaba.

the class DexUtils method inject.

public static boolean inject(PathClassLoader classLoader, File dex, File opt) {
    Log.i(TAG, dex.getAbsolutePath() + " dex length: " + dex.length());
    Log.i(TAG, opt.getAbsolutePath() + " opt length: " + opt.length());
    DexFile[] dexFiles = null;
    Field pathListField = null;
    Field fDexElements = null;
    Object dstObject = null;
    try {
        Object newDexElements;
        int dexLength;
        if (VERSION.SDK_INT >= 14) {
            pathListField = ReflectUtil.fieldGetOrg(classLoader, Class.forName("dalvik.system.BaseDexClassLoader"), "pathList");
            fDexElements = ReflectUtil.fieldGetOrg(pathListField.get(classLoader), "dexElements");
            Object e = fDexElements.get(pathListField.get(classLoader));
            dstObject = e;
            dexFiles = new DexFile[Array.getLength(e)];
            for (int i = 0; i < Array.getLength(e); ++i) {
                newDexElements = Array.get(e, i);
                dexFiles[i] = (DexFile) ReflectUtil.fieldGet(newDexElements, "dexFile");
            }
        } else {
            pathListField = ReflectUtil.fieldGetOrg(classLoader, "mDexs");
            dstObject = pathListField.get(classLoader);
            dexFiles = new DexFile[Array.getLength(dstObject)];
            for (dexLength = 0; dexLength < Array.getLength(dstObject); ++dexLength) {
                dexFiles[dexLength] = (DexFile) Array.get(dstObject, dexLength);
            }
        }
        dexLength = Array.getLength(dstObject) + 1;
        newDexElements = Array.newInstance(fDexElements.getType().getComponentType(), dexLength);
        DexClassLoader dynamicDex = new DexClassLoader(dex.getAbsolutePath(), opt.getAbsolutePath(), null, classLoader.getParent());
        Log.i(TAG, "after opt, dex len:" + dex.length() + "; opt len:" + opt.length());
        Object pathList = pathListField.get(dynamicDex);
        Object dexElements = fDexElements.get(pathList);
        Object firstDexElement = Array.get(dexElements, 0);
        Array.set(newDexElements, 0, firstDexElement);
        for (int i = 0; i < dexLength - 1; ++i) {
            Object element = Array.get(dstObject, i);
            Array.set(newDexElements, i + 1, element);
        }
        if (VERSION.SDK_INT >= 14) {
            fDexElements.set(pathListField.get(classLoader), newDexElements);
        } else {
            pathListField.set(classLoader, newDexElements);
        }
        return true;
    } catch (Exception e) {
        Log.e(TAG, "fail to override classloader " + classLoader + " with " + dex.getAbsolutePath(), e);
        return false;
    }
}
Also used : Field(java.lang.reflect.Field) DexClassLoader(dalvik.system.DexClassLoader) DexFile(dalvik.system.DexFile)

Aggregations

DexFile (dalvik.system.DexFile)59 File (java.io.File)32 IOException (java.io.IOException)28 ArrayList (java.util.ArrayList)17 Field (java.lang.reflect.Field)13 ZipFile (java.util.zip.ZipFile)13 Method (java.lang.reflect.Method)11 DexClassLoader (dalvik.system.DexClassLoader)9 Enumeration (java.util.Enumeration)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 ApplicationInfo (android.content.pm.ApplicationInfo)3 NClassLoader (android.taobao.atlas.startup.NClassLoader)2 URL (java.net.URL)2 HashSet (java.util.HashSet)2 Application (android.app.Application)1 ActivityNotFoundException (android.content.ActivityNotFoundException)1 Context (android.content.Context)1 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)1 Main (com.android.dx.command.dexer.Main)1 Action (com.jia.base.annotation.Action)1