Edxposed: [BUG] Unable to hook methods in a specific class

Created on 5 Mar 2019  ·  6Comments  ·  Source: ElderDrivers/EdXposed

什么情况/What happened?

Cannot hook methods in a particular class, unusual error.

Xposed模块列表/Xposed Module List

Only one module that hooks this class.

Magisk模块列表/Magisk Module List

Busybox for Android NDK 1.29.3
Magisk Manager for Recovery Mode 2018.8.1
Riru - Core v16
Riru - Ed Xposed v0.3.0.0_beta1
Systemless Hosts 1.0
xmlpak v3.1
YouTube Vanced v14.06.54

EdXposed及Riru版本/Versions of EdXposed and Riru

EdXposed: v0.3.0.0_beta1
Riru: v16

相关Logcat/Logcat

2019-03-05 19:52:06.011 7217-7217/? E/EdXposed-dexmaker: error when getClassForSure
    java.lang.ExceptionInInitializerError
        at java.lang.Class.classForName(Native Method)
        at java.lang.Class.forName(Class.java:453)
        at com.elderdrivers.riru.xposed.c.e.a(Unknown Source:5)
        at de.robv.android.xposed.XposedBridge.hookMethod(Unknown Source:150)
        at de.robv.android.xposed.XposedHelpers.findAndHookMethod(Unknown Source:31)
        at de.robv.android.xposed.XposedHelpers.findAndHookMethod(Unknown Source:4)
        at name.mikanoshi.customiuizer.mods.Launcher.SwipeActionsHook(Launcher.java:38)
        at name.mikanoshi.customiuizer.MainModule.handleLoadPackage(MainModule.java:208)
        at de.robv.android.xposed.IXposedHookLoadPackage$Wrapper.handleLoadPackage(Unknown Source:2)
        at de.robv.android.xposed.callbacks.XC_LoadPackage.call(Unknown Source:6)
        at de.robv.android.xposed.callbacks.XCallback.callAll(Unknown Source:30)
        at com.elderdrivers.riru.xposed.d.b.a.hook(Unknown Source:191)
        at android.app.ActivityThread.access$1200(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(Unknown Source:703)
        at android.os.Handler.dispatchMessage(Unknown Source:21)
        at android.os.Looper.loop(Unknown Source:208)
        at android.app.ActivityThread.main(Unknown Source:107)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(Unknown Source:11)
        at com.android.internal.os.ZygoteInit.main(Unknown Source:274)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources com.miui.home.launcher.Application.getResources()' on a null object reference
        at com.miui.home.launcher.Workspace.<clinit>(Unknown Source:5)
        at java.lang.Class.classForName(Native Method) 
        at java.lang.Class.forName(Class.java:453) 
        at com.elderdrivers.riru.xposed.c.e.a(Unknown Source:5) 
        at de.robv.android.xposed.XposedBridge.hookMethod(Unknown Source:150) 
        at de.robv.android.xposed.XposedHelpers.findAndHookMethod(Unknown Source:31) 
        at de.robv.android.xposed.XposedHelpers.findAndHookMethod(Unknown Source:4) 
        at name.mikanoshi.customiuizer.mods.Launcher.SwipeActionsHook(Launcher.java:38) 
        at name.mikanoshi.customiuizer.MainModule.handleLoadPackage(MainModule.java:208) 
        at de.robv.android.xposed.IXposedHookLoadPackage$Wrapper.handleLoadPackage(Unknown Source:2) 
        at de.robv.android.xposed.callbacks.XC_LoadPackage.call(Unknown Source:6) 
        at de.robv.android.xposed.callbacks.XCallback.callAll(Unknown Source:30) 
        at com.elderdrivers.riru.xposed.d.b.a.hook(Unknown Source:191) 
        at android.app.ActivityThread.access$1200(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(Unknown Source:703) 
        at android.os.Handler.dispatchMessage(Unknown Source:21) 
        at android.os.Looper.loop(Unknown Source:208) 
        at android.app.ActivityThread.main(Unknown Source:107) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(Unknown Source:11) 
        at com.android.internal.os.ZygoteInit.main(Unknown Source:274) 
2019-03-05 19:52:06.011 7217-7217/? E/EdXposed-Bridge: java.lang.IllegalArgumentException: null class
        at com.elderdrivers.riru.xposed.b.a.a(Unknown Source:31)
        at de.robv.android.xposed.XposedBridge.hookMethod(Unknown Source:158)
        at de.robv.android.xposed.XposedHelpers.findAndHookMethod(Unknown Source:31)
        at de.robv.android.xposed.XposedHelpers.findAndHookMethod(Unknown Source:4)
        at name.mikanoshi.customiuizer.mods.Launcher.SwipeActionsHook(Launcher.java:38)
        at name.mikanoshi.customiuizer.MainModule.handleLoadPackage(MainModule.java:208)
        at de.robv.android.xposed.IXposedHookLoadPackage$Wrapper.handleLoadPackage(Unknown Source:2)
        at de.robv.android.xposed.callbacks.XC_LoadPackage.call(Unknown Source:6)
        at de.robv.android.xposed.callbacks.XCallback.callAll(Unknown Source:30)
        at com.elderdrivers.riru.xposed.d.b.a.hook(Unknown Source:191)
        at android.app.ActivityThread.access$1200(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(Unknown Source:703)
        at android.os.Handler.dispatchMessage(Unknown Source:21)
        at android.os.Looper.loop(Unknown Source:208)
        at android.app.ActivityThread.main(Unknown Source:107)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(Unknown Source:11)
        at com.android.internal.os.ZygoteInit.main(Unknown Source:274)

2019-03-05 19:52:08.099 7217-7217/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.miui.home, PID: 7217
    java.lang.NoClassDefFoundError: com.miui.home.launcher.Workspace
        at com.miui.home.launcher.Workspace.getScreenScaleRatio(Unknown Source:0)
        at com.miui.home.launcher.FolderCling.initFolderEnterEditModeAnimator(Unknown Source:6)
        at com.miui.home.launcher.FolderCling.onFinishInflate(Unknown Source:88)
        at android.view.LayoutInflater.rInflate(Unknown Source:125)
        at android.view.LayoutInflater.rInflateChildren(Unknown Source:9)
        at android.view.LayoutInflater.parseInclude(Unknown Source:273)
        at android.view.LayoutInflater.rInflate(Unknown Source:71)
        at android.view.LayoutInflater.rInflateChildren(Unknown Source:9)
        at android.view.LayoutInflater.rInflate(Unknown Source:103)
        at android.view.LayoutInflater.rInflateChildren(Unknown Source:9)
        at android.view.LayoutInflater.parseInclude(Unknown Source:273)
        at android.view.LayoutInflater.rInflate(Unknown Source:71)
        at android.view.LayoutInflater.rInflateChildren(Unknown Source:9)
        at android.view.LayoutInflater.rInflate(Unknown Source:103)
        at android.view.LayoutInflater.rInflateChildren(Unknown Source:9)
        at android.view.LayoutInflater.inflate(Unknown Source:124)
        at android.view.LayoutInflater.inflate(Unknown Source:20)
        at android.view.LayoutInflater.inflate(Unknown Source:5)
        at com.android.internal.policy.PhoneWindow.setContentView(Unknown Source:45)
        at android.app.Activity.setContentView(Unknown Source:4)
        at com.miui.home.launcher.Launcher.onCreate(Unknown Source:407)
        at android.app.Activity.performCreate(Unknown Source:12)
        at android.app.Activity.performCreate(Unknown Source:1)
        at android.app.Instrumentation.callActivityOnCreate(Unknown Source:3)
        at android.app.ActivityThread.performLaunchActivity(Unknown Source:368)
        at android.app.ActivityThread.handleLaunchActivity(Unknown Source:36)
        at android.app.servertransaction.LaunchActivityItem.execute(Unknown Source:57)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(Unknown Source:99)
        at android.app.servertransaction.TransactionExecutor.execute(Unknown Source:34)
        at android.app.ActivityThread$H.handleMessage(Unknown Source:36)
        at android.os.Handler.dispatchMessage(Unknown Source:21)
        at android.os.Looper.loop(Unknown Source:208)
        at android.app.ActivityThread.main(Unknown Source:107)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(Unknown Source:11)
        at com.android.internal.os.ZygoteInit.main(Unknown Source:274)
     Caused by: java.lang.ExceptionInInitializerError
        at java.lang.Class.classForName(Native Method)
        at java.lang.Class.forName(Class.java:453)
        at com.elderdrivers.riru.xposed.c.e.a(Unknown Source:5)
        at de.robv.android.xposed.XposedBridge.hookMethod(Unknown Source:150)
        at de.robv.android.xposed.XposedHelpers.findAndHookMethod(Unknown Source:31)
        at de.robv.android.xposed.XposedHelpers.findAndHookMethod(Unknown Source:4)
        at name.mikanoshi.customiuizer.mods.Launcher.SwipeActionsHook(Launcher.java:38)
        at name.mikanoshi.customiuizer.MainModule.handleLoadPackage(MainModule.java:208)
        at de.robv.android.xposed.IXposedHookLoadPackage$Wrapper.handleLoadPackage(Unknown Source:2)
        at de.robv.android.xposed.callbacks.XC_LoadPackage.call(Unknown Source:6)
        at de.robv.android.xposed.callbacks.XCallback.callAll(Unknown Source:30)
        at com.elderdrivers.riru.xposed.d.b.a.hook(Unknown Source:191)
        at android.app.ActivityThread.access$1200(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(Unknown Source:703)
        at android.os.Handler.dispatchMessage(Unknown Source:21) 
        at android.os.Looper.loop(Unknown Source:208) 
        at android.app.ActivityThread.main(Unknown Source:107) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(Unknown Source:11) 
        at com.android.internal.os.ZygoteInit.main(Unknown Source:274) 
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources com.miui.home.launcher.Application.getResources()' on a null object reference
        at com.miui.home.launcher.Workspace.<clinit>(Unknown Source:5)
        at java.lang.Class.classForName(Native Method) 
        at java.lang.Class.forName(Class.java:453) 
        at com.elderdrivers.riru.xposed.c.e.a(Unknown Source:5) 
        at de.robv.android.xposed.XposedBridge.hookMethod(Unknown Source:150) 
        at de.robv.android.xposed.XposedHelpers.findAndHookMethod(Unknown Source:31) 
        at de.robv.android.xposed.XposedHelpers.findAndHookMethod(Unknown Source:4) 
        at name.mikanoshi.customiuizer.mods.Launcher.SwipeActionsHook(Launcher.java:38) 
        at name.mikanoshi.customiuizer.MainModule.handleLoadPackage(MainModule.java:208) 
        at de.robv.android.xposed.IXposedHookLoadPackage$Wrapper.handleLoadPackage(Unknown Source:2) 
        at de.robv.android.xposed.callbacks.XC_LoadPackage.call(Unknown Source:6) 
        at de.robv.android.xposed.callbacks.XCallback.callAll(Unknown Source:30) 
        at com.elderdrivers.riru.xposed.d.b.a.hook(Unknown Source:191) 
        at android.app.ActivityThread.access$1200(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(Unknown Source:703) 
        at android.os.Handler.dispatchMessage(Unknown Source:21) 
        at android.os.Looper.loop(Unknown Source:208) 
        at android.app.ActivityThread.main(Unknown Source:107) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(Unknown Source:11) 
        at com.android.internal.os.ZygoteInit.main(Unknown Source:274) 

App is MIUI Launcher this version:
https://www.apkmirror.com/apk/xiaomi-inc/miui-launcher/miui-launcher-4-7-4-2-release/

Hook:

public void handleLoadPackage(final LoadPackageParam lpparam) throws Throwable {
    String pkg = lpparam.packageName;
    if (pkg.equals("com.miui.home")) {
        XposedHelpers.findAndHookMethod("com.miui.home.launcher.Workspace", lpparam.classLoader, "onVerticalGesture", int.class, MotionEvent.class, new XC_MethodHook() {
            @Override
            protected void beforeHookedMethod(final MethodHookParam param) throws Throwable {
                Log.e("onVerticalGesture", String.valueOf(param.args[0]));
            }
        });
    }
}
bug

Most helpful comment

You don't need XModuleResources to access your module resources. Use standard API like Context.createPackageContext. Example:

public static synchronized Context getModuleContext(Context context, Configuration config) throws Throwable {
        Context moduleContext = context.createPackageContext(MODULE_PACKAGE_NAME,
                    Context.CONTEXT_IGNORE_SECURITY);
        return (config == null ? moduleContext : moduleContext.createConfigurationContext(config));
    }

To access resources from any foreing process use context of this process and call:

Resources moduleResources = getModuleContext(context, 
               context.getResources().getConfiguration()).getResources();

All 6 comments

https://github.com/ElderDrivers/EdXposed/issues/77#issuecomment-470576541
貌似跟这个问题有点像。

Yes, I wrapped my hook in this hook and it works now:

XposedHelpers.findAndHookMethod(Application.class, "attach", Context.class, new XC_MethodHook() {
    @Override
    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
        ...original hook...
    }
});

I also get a crash every reboot which is caused by EdXposed itself:

E/EdXposed-Fwk: error when hooking LoadedApk.<init>: pkg=com.miui.securitycenter, prc=com.miui.securitycenter
    java.lang.NullPointerException
        at java.io.File.<init>(File.java:283)
        at dalvik.system.DexPathList.addNativePath(DexPathList.java:617)
        at dalvik.system.BaseDexClassLoader.addNativePath(BaseDexClassLoader.java:201)
        at android.app.ApplicationLoaders.addNative(Unknown Source:7)
        at android.app.LoadedApk.createOrUpdateClassLoaderLocked(Unknown Source:396)
        at android.app.LoadedApk.getClassLoader(Unknown Source:6)
        at com.elderdrivers.riru.xposed.d.b.b.hook(Unknown Source:170)
        at android.app.ContextCompat.createLoadedApk(Unknown Source:101)
        at android.app.ContextCompat.createApplicationContext(Unknown Source:0)
        at com.miui.securitycenter.dynamic.ApkLoader.getDynamicContext(Unknown Source:36)
        at com.miui.securitycenter.dynamic.DynamicServiceManager.createOrUpdateManagerWT(Unknown Source:2)
        at com.miui.securitycenter.dynamic.DynamicServiceManager.connectWT(Unknown Source:19)
        at com.miui.securitycenter.dynamic.DynamicServiceManager.access$400(Unknown Source:0)
        at com.miui.securitycenter.dynamic.DynamicServiceManager$DynamicWorkHandler.handleMessage(Unknown Source:35)
        at android.os.Handler.dispatchMessage(Unknown Source:21)
        at android.os.Looper.loop(Unknown Source:208)

And the last question :) What's the best way to use module's SharedPreferences and resources inside hooks?
I initialize XSharedPreferences and XModuleResources inside initZygote(), then I can use XSharedPreferences from hooks, but reload() is not working to fetch updated values, so dynamic settings are not possible without fast reboot. XModuleResources is not working from hooks at all, so I have to actually get resources (strings for example) and assign them to a static var inside my module's class. Is this the only way for now until resource hooks and file services are implemented?

You don't need XModuleResources to access your module resources. Use standard API like Context.createPackageContext. Example:

public static synchronized Context getModuleContext(Context context, Configuration config) throws Throwable {
        Context moduleContext = context.createPackageContext(MODULE_PACKAGE_NAME,
                    Context.CONTEXT_IGNORE_SECURITY);
        return (config == null ? moduleContext : moduleContext.createConfigurationContext(config));
    }

To access resources from any foreing process use context of this process and call:

Resources moduleResources = getModuleContext(context, 
               context.getResources().getConfiguration()).getResources();

If you get some problems with XSharedPreferences, please raise another issue to describe them in detail. ;)

Should I? Are they supposed to work? :)

@Mikanoshi Yes. In most cases, it's supposed to work.
Check #98 and #106 for more info.

Was this page helpful?
0 / 5 - 0 ratings