de.robv.android.xposed.IXposedHookLoadPackage Java Examples

The following examples show how to use de.robv.android.xposed.IXposedHookLoadPackage. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RetalDriverApplication.java    From ratel with Apache License 2.0 4 votes vote down vote up
public static boolean loadModule(final String moduleApkPath,
                                 final ApplicationInfo currentApplicationInfo, ClassLoader appClassLoader) {


    //dexposed这里,会读取xposedinstaller,但是我们并没有xposed installer,所以忽略掉xposed installers的过滤
    log("Loading modules from " + moduleApkPath);

    if (!new File(moduleApkPath).exists()) {
        log(moduleApkPath + " does not exist");
        return false;
    }

    ClassLoader hostClassLoader = ExposedBridge.class.getClassLoader();
    ClassLoader appClassLoaderWithXposed = ExposedBridge.getAppClassLoaderWithXposed(appClassLoader);

    //ClassLoader mcl = new DexClassLoader(moduleApkPath, moduleOdexDir, moduleLibPath, hostClassLoader);
    ClassLoader mcl = new PathClassLoader(moduleApkPath, hostClassLoader);
    InputStream is = mcl.getResourceAsStream("assets/xposed_init");
    if (is == null) {
        log("assets/xposed_init not found in the APK");
        return false;
    }

    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 {
                log("  Loading class " + moduleClassName);
                Class<?> moduleClass = mcl.loadClass(moduleClassName);

                if (!ExposedHelper.isIXposedMod(moduleClass)) {
                    log("    This class doesn't implement any sub-interface of IXposedMod, skipping it");
                    continue;
                } else if (IXposedHookInitPackageResources.class.isAssignableFrom(moduleClass)) {
                    log("    This class requires resource-related hooks (which are disabled), skipping it.");
                    continue;
                }

                final Object moduleInstance = moduleClass.newInstance();
                if (moduleInstance instanceof IXposedHookZygoteInit) {
                    ExposedHelper.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 = currentApplicationInfo.processName;
                    lpparam.classLoader = appClassLoaderWithXposed;
                    lpparam.appInfo = currentApplicationInfo;
                    lpparam.isFirstApplication = true;
                    XC_LoadPackage.callAll(lpparam);
                }

                if (moduleInstance instanceof IXposedHookInitPackageResources) {
                    // hookInitPackageResources(new IXposedHookInitPackageResources.Wrapper((IXposedHookInitPackageResources) moduleInstance));
                    // TODO: 17/12/1 Support Resource hook
                    log("not support hook resource,the hook" + moduleInstance.getClass() + " will be ignore");
                }

                return true;
            } catch (Throwable t) {
                log(t);
            }
        }
    } catch (IOException e) {
        log(e);
    } finally {
        try {
            is.close();
        } catch (IOException ignored) {
        }
    }
    return false;
}