How to get Context through hooking in android

后端 未结 2 1995
刺人心
刺人心 2020-12-19 21:45

The background is :

  1. I\'m using xposed framework to hook a third party app.
  2. When I hook method XXX, xposed gave me \"ClassNotFound\" error. I checked a
2条回答
  •  太阳男子
    2020-12-19 22:22

    The answer from the poster below is more succinct:

    Context context = (Context) AndroidAppHelper.currentApplication();
    

    An alternative hack is to retrieve the current activity (which can be cast to Context) like this:

    Class instrumentation = XposedHelpers.findClass(
                    "android.app.Instrumentation", lpparam.classLoader);
    
    XposedBridge.hookAllMethods(instrumentation, "newActivity", new XC_MethodHook() {
    
                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
    
                        mCurrentActivity = (Activity) param.getResult();
    
                        Log.v(TAG, "Current Activity : " + mCurrentActivity.getClass().getName());
                    }
    });
    

    Regarding the class loader, if it consists of the main app classloader then you can retrieve it from LoadPackageParam passed to the handleLoadPackage method.

    If the app itself creates a new DexClassLoader then you can hook the DexClassLoader constructor to keep a reference to it. That way you have the actual ClassLoader that contains your class and method. No need to get any context.

提交回复
热议问题