Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Helpers

rovo89 edited this page Jan 28, 2014 · 4 revisions

There are many helper methods in Xposed that can make developing a module much easier.

Class XposedBridge

log

The log method is an easy way of logging debug output to the standard logcat and a file called /data/xposed/debug.log. It can take the log message or a Throwable. In the latter case, it will print the stack trace.

hookAllMethods / hookAllConstructors

You can use these methods if you want to hook all methods with a specific name or all constructors in a class. This is useful if there are different variants, but you want to execute some code before/after any of them has been called. Keep in mind that other ROMs might have additional variants that will also be hooked by this. Especially, be careful about the args you get in the callback.

Class XposedHelpers

I recommend adding this class to the Eclipse favorites for static imports: Window => Preferences => Java => Editor => Content Assist => Favorites => New Type, enter de.robv.android.xposed.XposedHelpers. By doing this, Eclipse will automatically suggest the methods from this class if you start typing "get.." for example and will create a static import of that method (that means you don't have the classname visible in your code).

findMethod / findConstructor / findField

There are a couple of methods for retrieving methods, constructors and fields without using reflection yourself. Also, you can find methods and constructors using a "best match" for certain parameter types. So for example, you can call findMethodBestMatch(Class<?> clazz, String methodName, Object... args) with a TextView argument and it will also find a method with the given name that takes a View parameter if no more specific variant exists.

callMethod / callStaticMethod / newInstance

Making use of the findXXX methods mentioned above, these methods make it easy to call a method or create a new instance of a class. The caller doesn't have to use reflection for this. There is no need to retrieve the method before, just use these methods to call it on-the-fly. The types of the parameters are automatically copied from the actual parameter values and the best-matching method is called. In case you want to explicitly specify the type for a parameter, create a Class<?> array and pass it to callXXX/newInstance. You can leave some of the items in the array empty (null) to use the class of the actual parameter, but the array length has to match the number of parameters.

getXXXField / setXXXField / getStaticXXXField /setStaticXXXField

These are wrappers to easily get and set the content of instance and class variables. You just need the reference to the object, the field name and type (and the new value for setters of course). If you want to get/set a static field and don't have an object reference, you can use the getStaticXXX and setStaticXXX methods. There is however no need to differentiate between static and instance fields when you have an object reference, the getXXX and setXXX can set both.

getAdditionalXXXField / setAdditionalXXXField

These methods let you associate any values with either an instance of an object or a whole class (like a static field). The values are stored in a key-value-map, so you can save multiple values per object. The key can be any string, including names of fields that the object actually has. Please note that it you cannot retrieve a value you stored with setAdditionalStaticField by calling getAdditionalInstanceField. Use getAdditionalStaticField instead, which has a variant which takes an object and looks up its class automatically.

assetAsByteArray

This method returns an asset as a byte array. If you want to load your module's resources, you can use something like this:

public class XposedTweakbox implements IXposedHookZygoteInit {
	@Override
	public void initZygote(StartupParam startupParam) throws Throwable {
		Resources tweakboxRes = XModuleResources.createInstance(startupParam.modulePath, null);
		byte[] crtPatch = assetAsByteArray(tweakboxRes, "crtfix_samsung_d506192d5049a4042fb84c0265edfe42.bsdiff");
...

getMD5Sum

Returns the MD5 sum of a file on the file system. The current app needs read access to the file (in the init method you have root permissions, so that should not be a problem).

getProcessPid

Finds a process by the first part of its /proc/[pid]/cmdline and returns its PID as a String.