Search in sources :

Example 21 with DexClassLoader

use of dalvik.system.DexClassLoader in project Auto.js by hyb1996.

the class AndroidClassLoader method loadDex.

public DexClassLoader loadDex(File file) throws FileNotFoundException {
    Log.d(LOG_TAG, "loadDex: file = " + file);
    if (!file.exists()) {
        throw new FileNotFoundException(file.getPath());
    }
    DexClassLoader loader = new DexClassLoader(file.getPath(), mCacheDir.getPath(), null, parent);
    mDexClassLoaders.add(loader);
    return loader;
}
Also used : DexClassLoader(dalvik.system.DexClassLoader) FileNotFoundException(java.io.FileNotFoundException)

Example 22 with DexClassLoader

use of dalvik.system.DexClassLoader in project blog_resource by Guolei1130.

the class MainActivity method loadExtApk.

private void loadExtApk() {
    String apkPath = new File("/sdcard/plugin_1.apk").getPath();
    File dexOptOutDir = new File(getFilesDir(), "dexopt");
    if (!dexOptOutDir.exists()) {
        boolean result = dexOptOutDir.mkdir();
        if (!result) {
            Log.e(TAG, "loadExtJar: create out dir error");
        }
    }
    String dexOptOutDr = dexOptOutDir.getPath();
    ClassLoader classLoader = null;
    if (Constants.isDalvik()) {
        classLoader = new DexClassLoader(apkPath, dexOptOutDr, null, ClassLoader.getSystemClassLoader());
    } else {
        classLoader = new PathClassLoader(apkPath, ClassLoader.getSystemClassLoader());
    }
    try {
        Class userClz = classLoader.loadClass("com.guolei.plugin_1.People");
        Object user = userClz.getConstructor(String.class, int.class).newInstance("guolei", 24);
        Method method = userClz.getDeclaredMethod("toString");
        method.setAccessible(true);
        String msg = (String) method.invoke(user);
        Log.e(TAG, "loadExtApk: " + msg);
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : PathClassLoader(dalvik.system.PathClassLoader) DexClassLoader(dalvik.system.DexClassLoader) DexClassLoader(dalvik.system.DexClassLoader) PathClassLoader(dalvik.system.PathClassLoader) Method(java.lang.reflect.Method) ZipFile(java.util.zip.ZipFile) DexFile(dalvik.system.DexFile) File(java.io.File) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 23 with DexClassLoader

use of dalvik.system.DexClassLoader in project FastAndroid by jerryliuinfo.

the class HotFixManager method loadDex.

public static void loadDex(Context context) {
    if (context == null) {
        return;
    }
    // 获取当前应用的上下文的私有路径,也就是dex目录
    File filesDir = context.getDir("odex", Context.MODE_PRIVATE);
    FrameworkLogUtil.d("loadDex filesDir path : " + filesDir.getAbsolutePath());
    File[] files = filesDir.listFiles();
    for (File file : files) {
        FrameworkLogUtil.d("loadDex file path : " + file.getAbsolutePath() + ", name:" + file.getName());
        if (file.getName().contains("classes") || file.getName().endsWith(".dex")) {
            loadedDex.add(file);
        }
    }
    // 创建一个目录,用来存放解压的文件
    String optmizeDir = filesDir.getAbsolutePath() + File.separator + "opt_dex";
    FrameworkLogUtil.d("loadDex optmizeDir +" + optmizeDir);
    File optimizeDir = new File(optmizeDir);
    if (!optimizeDir.exists()) {
        optimizeDir.mkdirs();
    }
    FrameworkLogUtil.d("loadDex loadedDex : " + loadedDex);
    for (File dex : loadedDex) {
        DexClassLoader myDexClassLoader = new DexClassLoader(dex.getAbsolutePath(), optimizeDir.getAbsolutePath(), null, context.getClassLoader());
        // 实现一个类加载器对象
        PathClassLoader pathClassLoader = (PathClassLoader) context.getClassLoader();
        try {
            /**
             ************获取系统的未修复的dexElements开始***************
             */
            // 获取到DexClassLoader类加载器的父类
            Class<?> baseDexCkazzLoader = Class.forName("dalvik.system.BaseDexClassLoader");
            Field pathListField = baseDexCkazzLoader.getDeclaredField("pathList");
            // 设置成员变量允许被访问,用于反射
            pathListField.setAccessible(true);
            // 执行pathList的get方法
            Object pathListObject = pathListField.get(pathClassLoader);
            // 获取到PathList类对象
            Class<?> systemPathListClass = pathListObject.getClass();
            // 通过这类对象获取到它里面的一个叫做dexElement的成员表变量
            Field dexElements = systemPathListClass.getDeclaredField("dexElements");
            dexElements.setAccessible(true);
            // 获取到dexElements成员变量的值
            Object systemElements = dexElements.get(pathListObject);
            /**
             ************获取系统的未修复的dexElements结束***************
             */
            /**
             ************获取已修复的dexElements开始***************
             */
            // 通过反射获取类加载器的父类
            Class<?> myDexClazzLoader = Class.forName("dalvik.system.BaseDexClassLoader");
            // 从父类的对象中去获取pathList的成员变量
            Field myPathListField = baseDexCkazzLoader.getDeclaredField("pathList");
            myPathListField.setAccessible(true);
            Object myPathListObject = myPathListField.get(myDexClassLoader);
            Field myDexElements1Field = myPathListObject.getClass().getDeclaredField("dexElements");
            myDexElements1Field.setAccessible(true);
            Object myElements = myDexElements1Field.get(myPathListObject);
            /**
             ************获取已修复的dexElements结束***************
             */
            /**
             ************dex文件的融合***************
             */
            // 获取到系统的(未修复)systemElements的类对象
            Class<?> componentType = systemElements.getClass().getComponentType();
            // 得到系统的的dexElements的长度
            int systemLength = Array.getLength(systemElements);
            // 获取到修复的dexElements长度
            int myLength = Array.getLength(myElements);
            // 创建一个新的长度
            int newLength = systemLength + myLength;
            Object newElementArray = Array.newInstance(componentType, newLength);
            for (int i = 0; i < newLength; i++) {
                if (i < myLength) {
                    // 将补丁的myElements的第i位放到newElementArray 的第i位置
                    Array.set(newElementArray, i, Array.get(myElements, i));
                } else {
                    Array.set(newElementArray, i, Array.get(systemElements, i - myLength));
                }
            }
            // 将系统的数组和我们定义的数组融合之后,再放入到系统的数组中
            Field dexElementsField = pathListObject.getClass().getDeclaredField("dexElements");
            dexElementsField.setAccessible(true);
            dexElementsField.set(pathListObject, newElementArray);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Also used : PathClassLoader(dalvik.system.PathClassLoader) Field(java.lang.reflect.Field) DexClassLoader(dalvik.system.DexClassLoader) File(java.io.File)

Example 24 with DexClassLoader

use of dalvik.system.DexClassLoader in project XCFramework by wydzxc21.

the class XCHotfixUtil method doDexInject.

/**
 * @author ZhangXuanChen
 * @date 2021/6/29
 * @description doDexInject
 */
private static boolean doDexInject(Context context, File dexDir) {
    if (context == null || dexDir == null) {
        return false;
    }
    try {
        HashSet<File> dexList = getDexList(dexDir);
        if (dexList != null && !dexList.isEmpty()) {
            File optimizedFile = new File(getOptimizedPath(context));
            if (!optimizedFile.exists()) {
                optimizedFile.mkdirs();
            }
            // 
            PathClassLoader pathLoader = (PathClassLoader) context.getClassLoader();
            for (File dexFile : dexList) {
                DexClassLoader dexLoader = new DexClassLoader(dexFile.getAbsolutePath(), optimizedFile.getAbsolutePath(), null, pathLoader);
                // 
                Object dexPathList = getPathList(dexLoader);
                Object pathPathList = getPathList(pathLoader);
                // 
                Object leftDexElements = getDexElements(dexPathList);
                Object rightDexElements = getDexElements(pathPathList);
                // 
                Object dexElements = combineArray(leftDexElements, rightDexElements);
                // 
                Object pathList = getPathList(pathLoader);
                setField(pathList, pathList.getClass(), "dexElements", dexElements);
            }
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}
Also used : PathClassLoader(dalvik.system.PathClassLoader) DexClassLoader(dalvik.system.DexClassLoader) File(java.io.File)

Example 25 with DexClassLoader

use of dalvik.system.DexClassLoader in project Shadow by Tencent.

the class ImplLoader method loadWhiteList.

public String[] loadWhiteList(InstalledApk installedApk, String whiteListClassName, String whiteListFieldName) {
    DexClassLoader dexClassLoader = new DexClassLoader(installedApk.apkFilePath, installedApk.oDexPath, installedApk.libraryPath, getClass().getClassLoader());
    String[] whiteList = null;
    try {
        Class<?> whiteListClass = dexClassLoader.loadClass(whiteListClassName);
        Field whiteListField = whiteListClass.getDeclaredField(whiteListFieldName);
        Object o = whiteListField.get(null);
        whiteList = (String[]) o;
    } catch (ClassNotFoundException ignored) {
    } catch (NoSuchFieldException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
    String[] interfaces;
    if (whiteList != null) {
        interfaces = concatenate(getCustomWhiteList(), whiteList);
    } else {
        interfaces = getCustomWhiteList();
    }
    return interfaces;
}
Also used : Field(java.lang.reflect.Field) DexClassLoader(dalvik.system.DexClassLoader)

Aggregations

DexClassLoader (dalvik.system.DexClassLoader)55 File (java.io.File)30 PathClassLoader (dalvik.system.PathClassLoader)13 IOException (java.io.IOException)8 DexFile (dalvik.system.DexFile)7 Field (java.lang.reflect.Field)6 FileOutputStream (java.io.FileOutputStream)5 InputStream (java.io.InputStream)5 ZipFile (java.util.zip.ZipFile)5 TargetApi (android.annotation.TargetApi)3 Intent (android.content.Intent)3 Resources (android.content.res.Resources)3 RemoteException (android.os.RemoteException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ActivityNotFoundException (android.content.ActivityNotFoundException)2 Context (android.content.Context)2 ContextWrapper (android.content.ContextWrapper)2 AssetManager (android.content.res.AssetManager)2 Handler (android.os.Handler)2 TextView (android.widget.TextView)2