Package 

Class XposedHelpers


  • 
    public final class XposedHelpers
    
                        

    Helpers that simplify hooking and calling methods/constructors, getting and settings fields, ...

    • Method Detail

      • findClass

         static Class<out Object> findClass(String className, ClassLoader classLoader)

        Look up a class with the specified class loader.

        There are various allowed syntaxes for the class name, but it's recommended to use one ofthese:

        • {@code java.lang.String}
        • {@code java.lang.String[]} (array)
        • {@code android.app.ActivityThread.ResourcesKey}
        • {@code android.app.ActivityThread$ResourcesKey}
        Parameters:
        className - The class name in one of the formats mentioned above.
        classLoader - The class loader, or {@code null} for the boot class loader.
      • findClassIfExists

         static Class<out Object> findClassIfExists(String className, ClassLoader classLoader)

        Look up and return a class if it exists.Like findClass, but doesn't throw an exception if the class doesn't exist.

        Parameters:
        className - The class name.
        classLoader - The class loader, or {@code null} for the boot class loader.
      • findField

         static Field findField(Class<out Object> clazz, String fieldName)

        Look up a field in a class and set it to accessible.

        Parameters:
        clazz - The class which either declares or inherits the field.
        fieldName - The field name.
      • findFieldIfExists

         static Field findFieldIfExists(Class<out Object> clazz, String fieldName)

        Look up and return a field if it exists.Like findField, but doesn't throw an exception if the field doesn't exist.

        Parameters:
        clazz - The class which either declares or inherits the field.
        fieldName - The field name.
      • findFirstFieldByExactType

         static Field findFirstFieldByExactType(Class<out Object> clazz, Class<out Object> type)

        Returns the first field of the given type in a class.Might be useful for Proguard'ed classes to identify fields with unique types.

        Parameters:
        clazz - The class which either declares or inherits the field.
        type - The type of the field.
      • findAndHookMethod

         static XC_MethodHook.Unhook findAndHookMethod(String className, ClassLoader classLoader, String methodName, Array<Object> parameterTypesAndCallback)

        Look up a method and hook it. The last argument must be the callback for the hook.

        This combines calls to findMethodExact and hookMethod.

        The method must be declared or overridden in the given class, inheritedmethods are not considered! That's because each method implementation exists only once inthe memory, and when classes inherit it, they just get another reference to the implementation.Hooking a method therefore applies to all classes inheriting the same implementation. Youhave to expect that the hook applies to subclasses (unless they override the method), but youshouldn't have to worry about hooks applying to superclasses, hence this "limitation".There could be undesired or even dangerous hooks otherwise, e.g. if you hook {@code SomeClass.equals()} and that class doesn't override the {@code equals()} on some ROMs,making you hook {@code Object.equals()} instead.

        There are two ways to specify the parameter types. If you already have a reference to the Class, use that. For Android framework classes, you can often use something like {@code String.class}. If you don't have the class reference, you can simply use thefull class name as a string, e.g. {@code java.lang.String} or {@code com.example.MyClass}.It will be passed to findClass with the same class loader that is used for the targetmethod, see its documentation for the allowed notations.

        Primitive types, such as {@code int}, can be specified using {@code int.class} (recommended)or {@code Integer.TYPE}. Note that {@code Integer.class} doesn't refer to {@code int} but to {@code Integer}, which is a normal class (boxed primitive). Therefore it must not be used whenthe method expects an {@code int} parameter - it has to be used for {@code Integer} parametersthough, so check the method signature in detail.

        As last argument to this method (after the list of target method parameters), you needto specify the callback that should be executed when the method is invoked. It's usuallyan anonymous subclass of XC_MethodHook or XC_MethodReplacement.

        Example

        	// In order to hook this method ...
        	package com.example;
        	public class SomeClass {
        	  public int doSomething(String s, int i, MyClass m) {
        	    ...
        	  }
        	}
        	
        	// ... you can use this call:
        	findAndHookMethod("com.example.SomeClass", lpparam.classLoader, String.class, int.class, "com.example.MyClass", new XC_MethodHook() {
        	  @Override
        	  protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
        	    String oldText = (String) param.args[0];
        	    Log.d("MyModule", oldText);
        	
        	    param.args[0] = "test";
        	    param.args[1] = 42; // auto-boxing is working here
        	    setBooleanField(param.args[2], "great", true);
        	
        	    // This would not work (as MyClass can't be resolved at compile time):
        	    //   MyClass myClass = (MyClass) param.args[2];
        	    //   myClass.great = true;
        	  }
        	});
        	
        Parameters:
        className - The name of the class which implements the method.
        classLoader - The class loader for resolving the target and parameter classes.
        methodName - The target method name.
        parameterTypesAndCallback - The parameter types of the target method, plus the callback.
      • findMethodExact

         static Method findMethodExact(String className, ClassLoader classLoader, String methodName, Array<Object> parameterTypes)

        Look up a method in a class and set it to accessible.The method must be declared or overridden in the given class.

        See findAndHookMethod for details aboutthe method and parameter type resolution.

        Parameters:
        className - The name of the class which implements the method.
        classLoader - The class loader for resolving the target and parameter classes.
        methodName - The target method name.
        parameterTypes - The parameter types of the target method.
      • findMethodExactIfExists

         static Method findMethodExactIfExists(String className, ClassLoader classLoader, String methodName, Array<Object> parameterTypes)

        Look up and return a method if it exists.Like findMethodExact, but doesn't throw anexception if the method doesn't exist.

        Parameters:
        className - The name of the class which implements the method.
        classLoader - The class loader for resolving the target and parameter classes.
        methodName - The target method name.
        parameterTypes - The parameter types of the target method.
      • findMethodsByExactParameters

         static Array<Method> findMethodsByExactParameters(Class<out Object> clazz, Class<out Object> returnType, Array<Class<out Object>> parameterTypes)

        Returns an array of all methods declared/overridden in a class with the specified parameter types.

        The return type is optional, it will not be compared if it is {@code null}.Use {@code void.class} if you want to search for methods returning nothing.

        Parameters:
        clazz - The class to look in.
        returnType - The return type, or {@code null} (see above).
        parameterTypes - The parameter types.
      • findMethodBestMatch

         static Method findMethodBestMatch(Class<out Object> clazz, String methodName, Array<Class<out Object>> parameterTypes)

        Look up a method in a class and set it to accessible.

        This does'nt only look for exact matches, but for the best match. All considered candidatesmust be compatible with the given parameter types, i.e. the parameters must be assignableto the method's formal parameters. Inherited methods are considered here.

        Parameters:
        clazz - The class which declares, inherits or overrides the method.
        methodName - The method name.
        parameterTypes - The types of the method's parameters.
      • getParameterIndexByType

         static int getParameterIndexByType(Member method, Class<out Object> type)

        Returns the index of the parameter declared with the given type, ensuring that there is exactly one such parameter.

      • setObjectField

         static void setObjectField(Object obj, String fieldName, Object value)

        Sets the value of an object field in the given object instance. A class reference is not sufficient! See also findField.

      • setBooleanField

         static void setBooleanField(Object obj, String fieldName, boolean value)

        Sets the value of a {@code boolean} field in the given object instance. A class reference is not sufficient! See also findField.

      • setByteField

         static void setByteField(Object obj, String fieldName, byte value)

        Sets the value of a {@code byte} field in the given object instance. A class reference is not sufficient! See also findField.

      • setCharField

         static void setCharField(Object obj, String fieldName, char value)

        Sets the value of a {@code char} field in the given object instance. A class reference is not sufficient! See also findField.

      • setDoubleField

         static void setDoubleField(Object obj, String fieldName, double value)

        Sets the value of a {@code double} field in the given object instance. A class reference is not sufficient! See also findField.

      • setFloatField

         static void setFloatField(Object obj, String fieldName, float value)

        Sets the value of a {@code float} field in the given object instance. A class reference is not sufficient! See also findField.

      • setIntField

         static void setIntField(Object obj, String fieldName, int value)

        Sets the value of an {@code int} field in the given object instance. A class reference is not sufficient! See also findField.

      • setLongField

         static void setLongField(Object obj, String fieldName, long value)

        Sets the value of a {@code long} field in the given object instance. A class reference is not sufficient! See also findField.

      • setShortField

         static void setShortField(Object obj, String fieldName, short value)

        Sets the value of a {@code short} field in the given object instance. A class reference is not sufficient! See also findField.

      • getSurroundingThis

         static Object getSurroundingThis(Object obj)

        For inner classes, returns the surrounding instance, i.e. the {@code this} reference of the surrounding class.

      • getBooleanField

         static boolean getBooleanField(Object obj, String fieldName)

        Returns the value of a {@code boolean} field in the given object instance. A class reference is not sufficient! See also findField.

      • getByteField

         static byte getByteField(Object obj, String fieldName)

        Returns the value of a {@code byte} field in the given object instance. A class reference is not sufficient! See also findField.

      • getCharField

         static char getCharField(Object obj, String fieldName)

        Returns the value of a {@code char} field in the given object instance. A class reference is not sufficient! See also findField.

      • getDoubleField

         static double getDoubleField(Object obj, String fieldName)

        Returns the value of a {@code double} field in the given object instance. A class reference is not sufficient! See also findField.

      • getFloatField

         static float getFloatField(Object obj, String fieldName)

        Returns the value of a {@code float} field in the given object instance. A class reference is not sufficient! See also findField.

      • getIntField

         static int getIntField(Object obj, String fieldName)

        Returns the value of an {@code int} field in the given object instance. A class reference is not sufficient! See also findField.

      • getLongField

         static long getLongField(Object obj, String fieldName)

        Returns the value of a {@code long} field in the given object instance. A class reference is not sufficient! See also findField.

      • getShortField

         static short getShortField(Object obj, String fieldName)

        Returns the value of a {@code short} field in the given object instance. A class reference is not sufficient! See also findField.

      • callMethod

         static Object callMethod(Object obj, String methodName, Array<Object> args)

        Calls an instance or static method of the given object.The method is resolved using findMethodBestMatch.

        Parameters:
        obj - The object instance.
        methodName - The method name.
        args - The arguments for the method call.
      • callMethod

         static Object callMethod(Object obj, String methodName, Array<Class<out Object>> parameterTypes, Array<Object> args)

        Calls an instance or static method of the given object.See callMethod.

        This variant allows you to specify parameter types, which can help in case there are multiplemethods with the same name, especially if you call it with {@code null} parameters.

      • newInstance

         static Object newInstance(Class<out Object> clazz, Array<Class<out Object>> parameterTypes, Array<Object> args)

        Creates a new instance of the given class.See newInstance.

        This variant allows you to specify parameter types, which can help in case there are multipleconstructors with the same name, especially if you call it with {@code null} parameters.

      • setAdditionalInstanceField

         static Object setAdditionalInstanceField(Object obj, String key, Object value)

        Attaches any value to an object instance. This simulates adding an instance field.The value can be retrieved again with getAdditionalInstanceField.

        Parameters:
        obj - The object instance for which the value should be stored.
        key - The key in the value map for this object instance.
        value - The value to store.
      • assetAsByteArray

         static Array<byte> assetAsByteArray(Resources res, String path)

        Loads an asset from a resource object and returns the content as {@code byte} array.

        Parameters:
        res - The resources from which the asset should be loaded.
        path - The path to the asset, as in open.
      • getMD5Sum

         static String getMD5Sum(String file)

        Returns the lowercase hex string representation of a file's MD5 hash sum.

      • incrementMethodDepth

         static int incrementMethodDepth(String method)

        Increments the depth counter for the given method.

        The intention of the method depth counter is to keep track of the call depth for recursivemethods, e.g. to override parameters only for the outer call. The Xposed framework uses thisto load drawable replacements only once per call, even when multiple getDrawable variants call each other.

        Parameters:
        method - The method name.