Search in sources :

Example 51 with DexClassLoader

use of dalvik.system.DexClassLoader in project xposed_module_loader by WindySha.

the class XposedModuleLoader method loadModule.

public static boolean loadModule(final String moduleApkPath, String moduleOdexDir, String moduleLibPath, final ApplicationInfo currentApplicationInfo, ClassLoader appClassLoader) {
    XLog.i(TAG, "Loading modules from " + moduleApkPath);
    if (!new File(moduleApkPath).exists()) {
        Log.e(TAG, moduleApkPath + " does not exist");
        return false;
    }
    // use system classloader to load asset to avoid the app has file assets/xposed_init
    ClassLoader assetLoader = new DexClassLoader(moduleApkPath, moduleOdexDir, moduleLibPath, ClassLoader.getSystemClassLoader());
    InputStream is = assetLoader.getResourceAsStream("assets/xposed_init");
    if (is == null) {
        Log.i(TAG, "assets/xposed_init not found in the APK");
        return false;
    }
    ClassLoader mcl = new DexClassLoader(moduleApkPath, moduleOdexDir, moduleLibPath, appClassLoader);
    BufferedReader moduleClassesReader = new BufferedReader(new InputStreamReader(is));
    try {
        String moduleClassName;
        while ((moduleClassName = moduleClassesReader.readLine()) != null) {
            moduleClassName = moduleClassName.trim();
            if (moduleClassName.isEmpty() || moduleClassName.startsWith("#"))
                continue;
            try {
                XLog.i(TAG, "  Loading class " + moduleClassName);
                Class<?> moduleClass = mcl.loadClass(moduleClassName);
                if (!XposedHelper.isIXposedMod(moduleClass)) {
                    Log.i(TAG, "    This class doesn't implement any sub-interface of IXposedMod, skipping it");
                    continue;
                } else if (IXposedHookInitPackageResources.class.isAssignableFrom(moduleClass)) {
                    Log.i(TAG, "    This class requires resource-related hooks (which are disabled), skipping it.");
                    continue;
                }
                final Object moduleInstance = moduleClass.newInstance();
                if (moduleInstance instanceof IXposedHookZygoteInit) {
                    XposedHelper.callInitZygote(moduleApkPath, moduleInstance);
                }
                if (moduleInstance instanceof IXposedHookLoadPackage) {
                    // hookLoadPackage(new IXposedHookLoadPackage.Wrapper((IXposedHookLoadPackage) moduleInstance));
                    IXposedHookLoadPackage.Wrapper wrapper = new IXposedHookLoadPackage.Wrapper((IXposedHookLoadPackage) moduleInstance);
                    XposedBridge.CopyOnWriteSortedSet<XC_LoadPackage> xc_loadPackageCopyOnWriteSortedSet = new XposedBridge.CopyOnWriteSortedSet<>();
                    xc_loadPackageCopyOnWriteSortedSet.add(wrapper);
                    XC_LoadPackage.LoadPackageParam lpparam = new XC_LoadPackage.LoadPackageParam(xc_loadPackageCopyOnWriteSortedSet);
                    lpparam.packageName = currentApplicationInfo.packageName;
                    lpparam.processName = getCurrentProcessName(currentApplicationInfo);
                    ;
                    lpparam.classLoader = appClassLoader;
                    lpparam.appInfo = currentApplicationInfo;
                    lpparam.isFirstApplication = true;
                    XC_LoadPackage.callAll(lpparam);
                }
                if (moduleInstance instanceof IXposedHookInitPackageResources) {
                // hookInitPackageResources(new IXposedHookInitPackageResources.Wrapper((IXposedHookInitPackageResources) moduleInstance));
                // TODO: Support Resource hook
                }
            } catch (Throwable t) {
                Log.e(TAG, " error ", t);
            }
        }
    } catch (IOException e) {
        Log.e(TAG, " error ", e);
    } finally {
        try {
            is.close();
        } catch (IOException ignored) {
        }
    }
    return true;
}
Also used : IXposedHookLoadPackage(de.robv.android.xposed.IXposedHookLoadPackage) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) DexClassLoader(dalvik.system.DexClassLoader) IXposedHookZygoteInit(de.robv.android.xposed.IXposedHookZygoteInit) IOException(java.io.IOException) XposedBridge(de.robv.android.xposed.XposedBridge) BufferedReader(java.io.BufferedReader) DexClassLoader(dalvik.system.DexClassLoader) IXposedHookInitPackageResources(de.robv.android.xposed.IXposedHookInitPackageResources) File(java.io.File) XC_LoadPackage(de.robv.android.xposed.callbacks.XC_LoadPackage)

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