Xposed Method Hooking
Xposed method hooking to intercept and modify app behavior in your plugins.
Introduction
Xposed method hooking allows your plugin to intercept calls to methods (or constructors) within the application, modify their parameters, change their behavior, or replace their implementation entirely. This is a powerful technique for altering app functionality at a low level.
Hooking Concepts
To hook a method, you need to provide a "hook handler" — a Python class that defines what code to run when the target method is called. The system supports three main ways to interact with a method call.
The Hook Handler Base Classes
For clarity and correctness, you should create your handler by inheriting from one of the abstract base classes provided in base_plugin.py
:
MethodHook
: Use this when you want to run code before and/or after the original method executes, but still allow the original method to run.MethodReplacement
: Use this when you want to completely replace the original method's logic with your own.
The param
Object
All hook callback methods receive a param
object (de.robv.android.xposed.XC_MethodHook.MethodHookParam
) which is your key to interacting with the method call:
param.thisObject
: The instance on which the method was called (None
for static methods).param.args
: A list-like object of the arguments passed to the method. You can read and modify these. Changes made inbefore_hooked_method
will affect the original call.param.result
: The value returned by the original method. Available inafter_hooked_method
. You can read and modify this.param.method
: Ajava.lang.reflect.Member
object representing the hooked method or constructor.
A special and very useful feature is param.returnEarly = True
. If you set this in before_hooked_method
, the original method and any after_hooked_method
logic will be skipped entirely. You must also set param.result
to provide an immediate return value.
Reference: LSPosed XC_MethodHook.java
The Hooking Process (Step-by-Step)
1. Find the Target Method or Constructor
First, you need a reference to the java.lang.reflect.Method
or java.lang.reflect.Constructor
you want to hook. This is done using Java reflection.
2. Implement the Hook Handler
Create a Python class that inherits from MethodHook
or MethodReplacement
and implements the required callback(s).
3. Apply the Hook
From your BasePlugin
class, instantiate your handler and call self.hook_method()
.
Practical Examples
Example 1: Modifying Arguments (Before Hook)
Let's modify every "Toast" message to add a prefix.
Example 2: Changing the Return Value (After Hook)
This example hooks BuildVars.isMainApp()
and makes it always return False
.
Example 3: Replacing a Method (MethodReplacement)
This example completely disables a specific internal logging method to reduce logcat spam.
Return Values in MethodReplacement
When using MethodReplacement
, your Python replace_hooked_method
is the new implementation. You are responsible for returning a value of the correct type.
- For
void
Java methods,return
orreturn None
. - For methods returning primitives (e.g.,
int
,boolean
), return a standard Pythonint
orbool
. - For methods returning objects (e.g.,
String
), return a compatible Python object orNone
(which becomesnull
in Java).