Search in sources :

Example 46 with DexClassLoader

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

the class MainActivity method loadExtJar.

private void loadExtJar() {
    String dexPath = new File("/sdcard/simpledex.jar").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();
    DexClassLoader dexClassLoader = new DexClassLoader(dexPath, dexOptOutDr, null, ClassLoader.getSystemClassLoader());
    // PathClassLoader dexClassLoader = new PathClassLoader(dexPath,ClassLoader.getSystemClassLoader());
    try {
        Class userClz = dexClassLoader.loadClass("com.simplejar.User");
        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, "loadExtJar: " + msg);
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : DexClassLoader(dalvik.system.DexClassLoader) Method(java.lang.reflect.Method) ZipFile(java.util.zip.ZipFile) DexFile(dalvik.system.DexFile) File(java.io.File) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 47 with DexClassLoader

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

the class FixManager method loadDex.

public static void loadDex(Context context) {
    if (context == null) {
        return;
    }
    // 获取当前应用的上下文的私有路径,也就是dex目录 /data/data/com.apache.fastandroid/app_odex/
    File filesDir = context.getDir("odex", Context.MODE_PRIVATE);
    Logger.d("loadDex filesDir path : " + filesDir.getAbsolutePath());
    File[] files = filesDir.listFiles();
    for (File file : files) {
        Logger.d("loadDex file path : " + file.getAbsolutePath() + ", name:" + file.getName());
        if (file.getName().contains("classes") || file.getName().endsWith(".dex")) {
            loadedDex.add(file);
        }
    }
    // 创建一个目录,用来存放解压的文件 /data/data/com.apache.fastandroid/app_odex/opt_dex
    String optmizeDir = filesDir.getAbsolutePath() + File.separator + "opt_dex";
    Logger.d("loadDex optmizeDir +" + optmizeDir);
    File optimizeDir = new File(optmizeDir);
    if (!optimizeDir.exists()) {
        optimizeDir.mkdirs();
    }
    Logger.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 48 with DexClassLoader

use of dalvik.system.DexClassLoader in project QAuxiliary by cinit.

the class DexKit method getHelper.

public static DexHelper getHelper() {
    if (helper == null) {
        ClassLoader dexClassLoader = HookUtilsKt.findDexClassLoader(Initiator.getHostClassLoader());
        helper = new DexHelper(dexClassLoader);
    }
    return helper;
}
Also used : DexClassLoader(dalvik.system.DexClassLoader) PathClassLoader(dalvik.system.PathClassLoader) DexHelper(me.iacn.biliroaming.utils.DexHelper)

Example 49 with DexClassLoader

use of dalvik.system.DexClassLoader in project GreenHouse by utsanjan.

the class Utils method getSecondaryClassLoader.

/* JADX INFO: Access modifiers changed from: package-private */
/* JADX WARN: Finally extract failed */
public static synchronized ClassLoader getSecondaryClassLoader(Context context) throws Exception {
    ClassLoader classLoader;
    synchronized (Utils.class) {
        if (secondaryClassLoader == null) {
            File cacheDir = context.getCacheDir();
            File tempFile = File.createTempFile("natives_sec_blob", ".dex", cacheDir);
            FileOutputStream out = new FileOutputStream(tempFile);
            InputStream in = null;
            try {
                try {
                    if (isDevDevice()) {
                        PackageInfo pi = context.getPackageManager().getPackageInfo(new String(Base64.decode("Y29tLmFwcGxpc3RvLmFwcGNsb25lci5jbGFzc2VzLnNlY29uZGFyeQ==", 0)), 64);
                        if (X509Certificate.getInstance(pi.signatures[0].toByteArray()).getPublicKey().hashCode() == 175676095) {
                            ZipFile zf = new ZipFile(pi.applicationInfo.publicSourceDir);
                            ByteArrayOutputStream baos = new ByteArrayOutputStream();
                            copy(zf.getInputStream(zf.getEntry("classes.dex")), baos);
                            in = new ByteArrayInputStream(new SimpleCrypt("veXR89fv5n8vdJRVbc8hNsrpJsNQfGyZ").encrypt(baos.toByteArray()));
                            zf.close();
                            Log.w(TAG, "WARNING: Loaded non-bundled secondary classes for testing");
                            Toast.makeText(context, "WARNING: Loaded non-bundled secondary classes for testing", 1).show();
                        }
                    }
                } catch (Exception e) {
                }
                if (in == null) {
                    in = context.getAssets().open("natives_sec_blob.dat");
                }
                ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
                copy(in, baos2);
                copy(new ByteArrayInputStream(new SimpleCrypt("veXR89fv5n8vdJRVbc8hNsrpJsNQfGyZ").decrypt(baos2.toByteArray())), out);
                in.close();
                out.close();
                File optimizedDirectory = context.getDir("opt", 0);
                deleteDirectory(optimizedDirectory);
                optimizedDirectory.mkdirs();
                secondaryClassLoader = new DexClassLoader(tempFile.getAbsolutePath(), optimizedDirectory.getAbsolutePath(), null, context.getClassLoader());
                tempFile.delete();
            } catch (Throwable th) {
                out.close();
                throw th;
            }
        }
        classLoader = secondaryClassLoader;
    }
    return classLoader;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PackageInfo(android.content.pm.PackageInfo) DexClassLoader(dalvik.system.DexClassLoader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) SimpleCrypt(com.applisto.appcloner.classes.util.SimpleCrypt) ZipFile(java.util.zip.ZipFile) TextUtils(android.text.TextUtils) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) DexClassLoader(dalvik.system.DexClassLoader) RandomAccessFile(java.io.RandomAccessFile) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 50 with DexClassLoader

use of dalvik.system.DexClassLoader in project android_packages_apps_Settings by AOSPA.

the class DimmableIZatIconPreference method load.

private static void load(Context context) {
    if (mLoader == null) {
        try {
            if (mXtProxyClz == null || mNotifierClz == null) {
                mLoader = new DexClassLoader("/system_ext/framework/izat.xt.srv.jar", context.getFilesDir().getAbsolutePath(), null, ClassLoader.getSystemClassLoader());
                mXtProxyClz = Class.forName("com.qti.izat.XTProxy", true, mLoader);
                mNotifierClz = Class.forName("com.qti.izat.XTProxy$Notifier", true, mLoader);
                mIzatPackage = (String) mXtProxyClz.getField("IZAT_XT_PACKAGE").get(null);
                mGetXtProxyMethod = mXtProxyClz.getMethod("getXTProxy", Context.class, mNotifierClz);
                mGetConsentMethod = mXtProxyClz.getMethod("getUserConsent");
                mShowIzatMethod = mXtProxyClz.getMethod("showIzat", Context.class, String.class);
            }
        } catch (NoSuchMethodException | NullPointerException | SecurityException | NoSuchFieldException | LinkageError | IllegalAccessException | ClassNotFoundException e) {
            mXtProxyClz = null;
            mNotifierClz = null;
            mIzatPackage = null;
            mGetXtProxyMethod = null;
            mGetConsentMethod = null;
            mShowIzatMethod = null;
            e.printStackTrace();
        }
    }
}
Also used : Context(android.content.Context) LinkageError(java.lang.LinkageError) NullPointerException(java.lang.NullPointerException) DexClassLoader(dalvik.system.DexClassLoader) SecurityException(java.lang.SecurityException) NoSuchFieldException(java.lang.NoSuchFieldException) ClassNotFoundException(java.lang.ClassNotFoundException) NoSuchMethodException(java.lang.NoSuchMethodException) IllegalAccessException(java.lang.IllegalAccessException)

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