Search in sources :

Example 36 with DexClassLoader

use of dalvik.system.DexClassLoader in project BestPracticeApp by pop1234o.

the class DexInsertHotFix method loadFix.

public static void loadFix(Context context) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
    File directory = Environment.getExternalStorageDirectory();
    File dexFile = new File(directory, "classes.dex");
    if (!dexFile.exists()) {
        Log.i("test", "补丁不存在");
        return;
    }
    File optimizedDirectory = new File(context.getFilesDir(), "optimizedDirectory");
    if (!optimizedDirectory.exists()) {
        optimizedDirectory.mkdirs();
    }
    // 系统的类加载器
    PathClassLoader pathClassLoader = (PathClassLoader) context.getClassLoader();
    // 创建我们补丁的类加载器
    DexClassLoader dexClassLoader = new DexClassLoader(dexFile.getAbsolutePath(), optimizedDirectory.getAbsolutePath(), null, pathClassLoader);
    Field pathList = Class.forName("dalvik.system.BaseDexClassLoader").getDeclaredField("pathList");
    pathList.setAccessible(true);
    // 获取两个classLoader的pathList的成员变量(DexPathList.java)
    Object pathPathList = pathList.get(pathClassLoader);
    Object dexPathList = pathList.get(dexClassLoader);
    Field dexElements = Class.forName("dalvik.system.DexPathList").getDeclaredField("dexElements");
    dexElements.setAccessible(true);
    // (DexPathList.java中的Element[]数组,这个是个内部类)
    Object leftDexElements = dexElements.get(dexPathList);
    Object rightDexElements = dexElements.get(pathPathList);
    Class<?> componentType = leftDexElements.getClass().getComponentType();
    // 得到左数组长度(补丁数组)
    int i = Array.getLength(leftDexElements);
    // 得到原dex数组长度
    int j = Array.getLength(rightDexElements);
    // 得到总数组长度(补丁数组+原dex数组)
    int k = i + j;
    // 创建一个类型为componentType,长度为k的新数组
    Object result = Array.newInstance(componentType, k);
    System.arraycopy(leftDexElements, 0, result, 0, i);
    System.arraycopy(rightDexElements, 0, result, i, j);
    // 将新的 Element[] 赋值到 PathDexClassLoader 对象中的pathList,中的 Element[] dexElements;
    // 获取 (DexPathList)对象
    Object pathPathList_1 = pathList.get(pathClassLoader);
    // 将新的Element[] 赋值到 PathDexClassLoader->DexPathList->Element[]中
    dexElements.set(pathPathList_1, result);
    Log.i("test", "合并完成");
}
Also used : PathClassLoader(dalvik.system.PathClassLoader) Field(java.lang.reflect.Field) DexClassLoader(dalvik.system.DexClassLoader) File(java.io.File)

Example 37 with DexClassLoader

use of dalvik.system.DexClassLoader in project MagiskManager by topjohnwu.

the class CheckSafetyNet method dyload.

private void dyload() throws Exception {
    DexClassLoader loader = new DexClassLoader(dexPath.getPath(), dexPath.getParent(), null, ISafetyNetHelper.class.getClassLoader());
    Class<?> clazz = loader.loadClass("com.topjohnwu.snet.SafetyNetHelper");
    helper = (ISafetyNetHelper) clazz.getConstructors()[0].newInstance(getActivity(), (ISafetyNetHelper.Callback) code -> MagiskManager.get().safetyNetDone.publish(false, code));
    if (helper.getVersion() != Const.SNET_VER) {
        throw new Exception();
    }
}
Also used : ISafetyNetHelper(com.topjohnwu.magisk.utils.ISafetyNetHelper) OutputStream(java.io.OutputStream) HttpURLConnection(java.net.HttpURLConnection) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) Const(com.topjohnwu.magisk.utils.Const) Shell(com.topjohnwu.superuser.Shell) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) ShellUtils(com.topjohnwu.superuser.ShellUtils) WebService(com.topjohnwu.magisk.utils.WebService) MagiskManager(com.topjohnwu.magisk.MagiskManager) Activity(android.app.Activity) DexClassLoader(dalvik.system.DexClassLoader) InputStream(java.io.InputStream) DexClassLoader(dalvik.system.DexClassLoader) ISafetyNetHelper(com.topjohnwu.magisk.utils.ISafetyNetHelper)

Example 38 with DexClassLoader

use of dalvik.system.DexClassLoader in project AndroidLife by CaMnter.

the class SmartApplication method loadDex.

private void loadDex() {
    try {
        String dir = null;
        final File cacheDir = this.getExternalCacheDir();
        final File filesDir = this.getExternalFilesDir("");
        if (cacheDir != null) {
            dir = cacheDir.getAbsolutePath();
        } else {
            if (filesDir != null) {
                dir = filesDir.getAbsolutePath();
            }
        }
        if (TextUtils.isEmpty(dir))
            return;
        // assets 的 load-plugin-resource-plugin.apk 拷贝到 /storage/sdcard0/Android/data/[package name]/cache
        // 或者  /storage/sdcard0/Android/data/[package name]/files
        final File dexPath = new File(dir + File.separator + "load-plugin-resource-plugin.apk");
        final String dexAbsolutePath = dexPath.getAbsolutePath();
        AssetsUtils.copyAssets(this, "load-plugin-resource-plugin.apk", dexAbsolutePath);
        // /data/data/[package name]/app_load-plugin-resource-plugin
        final File optimizedDirectory = this.getDir("load-plugin-resource-plugin", MODE_PRIVATE);
        this.dexClassLoader = new DexClassLoader(dexPath.getAbsolutePath(), optimizedDirectory.getAbsolutePath(), null, this.getClassLoader());
        this.dexAbsolutePath = dexAbsolutePath;
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : DexClassLoader(dalvik.system.DexClassLoader) File(java.io.File)

Example 39 with DexClassLoader

use of dalvik.system.DexClassLoader in project AndroidLife by CaMnter.

the class SmartApplication method loadDex.

private void loadDex() {
    try {
        String dir = null;
        final File cacheDir = this.getExternalCacheDir();
        final File filesDir = this.getExternalFilesDir("");
        if (cacheDir != null) {
            dir = cacheDir.getAbsolutePath();
        } else {
            if (filesDir != null) {
                dir = filesDir.getAbsolutePath();
            }
        }
        if (TextUtils.isEmpty(dir))
            return;
        // assets 的 load-service-plugin.apk 拷贝到 /storage/sdcard0/Android/data/[package name]/cache
        // 或者  /storage/sdcard0/Android/data/[package name]/files
        final File dexPath = new File(dir + File.separator + "load-service-plugin.apk");
        final String dexAbsolutePath = dexPath.getAbsolutePath();
        AssetsUtils.copyAssets(this, "load-service-plugin.apk", dexAbsolutePath);
        // /data/data/[package name]/app_single-resources-plugin
        final File optimizedDirectory = this.getDir("load-service-plugin", MODE_PRIVATE);
        this.dexClassLoader = new DexClassLoader(dexPath.getAbsolutePath(), optimizedDirectory.getAbsolutePath(), null, this.getClassLoader());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : DexClassLoader(dalvik.system.DexClassLoader) File(java.io.File)

Example 40 with DexClassLoader

use of dalvik.system.DexClassLoader in project AndroidLife by CaMnter.

the class LoadSimplePluginActivity method initViews.

/**
 * Initialize the view in the layout
 *
 * @param savedInstanceState savedInstanceState
 */
@Override
protected void initViews(Bundle savedInstanceState) {
    try {
        String dir = null;
        final File cacheDir = this.getExternalCacheDir();
        final File filesDir = this.getExternalFilesDir("");
        if (cacheDir != null) {
            dir = cacheDir.getAbsolutePath();
        } else {
            if (filesDir != null) {
                dir = filesDir.getAbsolutePath();
            }
        }
        if (TextUtils.isEmpty(dir))
            return;
        // assets 的 simple-plugin.dex 拷贝到 /storage/sdcard0/Android/data/[package name]/cache
        // 或者  /storage/sdcard0/Android/data/[package name]/files
        final File dexPath = new File(dir + File.separator + "simple-plugin.dex");
        AssetsUtils.copyAssets(this, "simple-plugin.dex", dexPath.getAbsolutePath());
        // /data/data/[package name]/app_simple-plugin
        final File optimizedDirectory = this.getDir("simple-plugin", MODE_PRIVATE);
        final DexClassLoader dexClassLoader = new DexClassLoader(dexPath.getAbsolutePath(), optimizedDirectory.getAbsolutePath(), null, this.getClassLoader());
        final Class clazz = dexClassLoader.loadClass("com.camnter.load.simple.plugin.LoadSimplePlugin");
        final LoadSimplePluginInterface pluginInterface = (LoadSimplePluginInterface) clazz.newInstance();
        final TextView textView = this.findView(R.id.text);
        textView.setText(pluginInterface.getInfo());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : DexClassLoader(dalvik.system.DexClassLoader) TextView(android.widget.TextView) File(java.io.File)

Aggregations

DexClassLoader (dalvik.system.DexClassLoader)51 File (java.io.File)27 PathClassLoader (dalvik.system.PathClassLoader)13 DexFile (dalvik.system.DexFile)7 IOException (java.io.IOException)7 FileOutputStream (java.io.FileOutputStream)5 InputStream (java.io.InputStream)5 Field (java.lang.reflect.Field)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