I'm writing an instrumentation library that I'd like to work on both desktop and mobile (Android).
It functions by:
- Exposing a main which takes a single parameter, the main of the target class
- Installing a class loader which intercepts all classes as they are loaded and instruments them
Like so:
// Expects args[0] to contain the name of the INNER main
public static void main(String[] args) throws Throwable {
String className = args[0];
String [] newArgs = new String[0];
if(args.length > 1) {
newArgs = Arrays.copyOfRange(args, 1, args.length-1);
}
System.out.println("Bootstrapping " + className);
Loader s = new Loader(ClassLoader.getSystemClassLoader().getParent());
Class<?> c = s.loadClass(className);
c.getDeclaredMethod("main", new Class[] { String[].class }).invoke(
null, new Object[] { newArgs });
}
The question is this:
How can I do roughly the same thing for an android app?
One idea is to modify the android manifest to replace the existing activities with "wrapper" activities, that then install class loaders and call into the original underlying activity. Is there a better way?