Is it possible to do method swizzling in android using java? i would like to intercept a system method and log its parameters then process it normally
1 Answers
I think that technique could not be used using Java in any environment.
Maybe you could achieve a similar result using AOP.
But what you can do with that looks limited on Android. See Aspect-oriented programming in android. In fact, since you won't be compiling the target code (system method), compile-time weaving (which appears to be all you could use on Android) will be useless for this case. As is this answer I suppose.
One other thought... I guess you want to do this logging consistently. But if you needed this to debug a problem, you could do it using a conditional breakpoint in Eclipse.
A conditional expression can contain arbitrary Java code and may contain more than one statement, allowing breakpoint conditions to implement features like tracing. For example, a condition can execute a print statement and then return a hard coded value to never suspend ("System.out.println(...); return false;").
I don't know specifically whether this works with methods in the Android SDK. But it does work with methods in the Java SDK. For example, here is simple code:
System.err.println("foo");
I made a conditional breakpoint in PrintStream.print, like this:
System.err.println("hello: " + arg0);
return false;
And the console output when debugging in the program is this:
hello: foo
foo
Note that since the JDK is not compiled with debug symbols, I can't refer to method parameters by name, but using the arg0..argn
.

- 1
- 1

- 16,145
- 43
- 60
-
does this also apply of api methods from the android sdk ? – Mike G Dec 06 '11 at 16:39