Reputation: 4959
I realize that this is not possible since Android doesnt have a JVM but is there a work around to this problem? I need to perform a byte code injection operation for an Android application. Any suggestions?
Upvotes: 2
Views: 7587
Reputation: 79
Do you want to inject during runtime or compile time ?
For compile time - there are several very mature solutions for manipulating java source code / bytecode - ASM, java-assist, etc
Specifically for android, try ASMDEX
http://asm.ow2.org/doc/tutorial-asmdex.html
Upvotes: 0
Reputation: 9034
Code injection is possible in Android, please take a look on Disabler project hosted on Github.
Disabler allows to optimize, trace and modify Android project on the fly using code injection into existing project. Code is injected on the fly, no need to modify old functionality to add logging/profiling or disable portion of the flow.
Main functionality of the tool:
Under the hood, it uses AspectJ and Eclipse build mechanism (javac is replaced by ajc)
Upvotes: 3
Reputation: 20262
You can't directly inject bytecode into already loaded classes/methods. However, you can dynamically create new classes, write them to a dex file, and then dynamically load them
See this blog post for more information on dynamic loading of classes from a dex file on disk.
In order to dynamically create a new dex file, you might look at using the dexlib component that is part of the smali/baksmali codebase, which is a general purpose library for reading/writing dex files.
Or, alternatively, you could include smali in your application and generate your classes in the smali assembly format and use smali directly to assemble them into a new dex file.
Upvotes: 3