Thursday, 15 January 2015

java - android replace a method call at runtime -



java - android replace a method call at runtime -

i developing android app 3rd-party library. want replace method phone call in library. please note cannot obtain library's source code, have alter @ runtime.

for example, let's assume there doa() method in class foo in library

class foo { doa() { //method body } ... }

i want replace method body of doa() own code. have done exploration , found next stackoverflow thread: replacing method phone call in class @ runtime. thread tells me may seek bytecode manipulation library called javassist. found there android-version of library @ here: https://github.com/crimsonwoods/javassist-android. imported library , wrote next code:

try { final classpool cp = classpool.getdefault(getapplicationcontext()); ctclass cc = cp.get("foo"); ctmethod method = cc.getmethod("doa","()v"); method.setbody("{ java.lang.system#out.println(\"doa() called.\");}"); cc.writefile(); //where exception raised } grab (exception e) { e.printstacktrace(); }

but encountered exception when executing cc.writefile(). "filenotfoundexception: ./foo.class: open failed: enoent (no such file or directory)". not know how address problem.

your code trying save new class file not possible. there huge differences between java , android android not using normal .class files .dex file instead.

you should follow sample: https://github.com/crimsonwoods/javassist-android/blob/master/javassistsample/src/org/jamruby/javassistsample/mainactivity.java

java android methods bytecode javassist

No comments:

Post a Comment