2

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?

Mike G
  • 4,829
  • 11
  • 47
  • 76
  • Have you checked this [thread](http://stackoverflow.com/questions/230193/what-can-you-not-do-on-the-dalvik-vm-androids-vm-that-you-can-in-sun-vm)? – CRM Nov 22 '11 at 02:30
  • And [this one](http://groups.google.com/group/android-platform/browse_thread/thread/21549d6f3228eb50). – Dave Newton Nov 22 '11 at 02:33

3 Answers3

3

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:

  1. trace: entering/exiting to/from method, collecting parameters and exiting value)
  2. profile: measuring the frequency and duration of method calls
  3. disable: disabling/skipping part of the program flow by overriding returning value from methods defined by the user
  4. delay: introduce delays in certain sections of the code (i.e. for certain packages)

Under the hood, it uses AspectJ and Eclipse build mechanism (javac is replaced by ajc)

Andrzej Karpuszonak
  • 8,896
  • 2
  • 38
  • 50
  • @Hi, how can I use the project? Do you have any examples hosted? – P basak Jan 15 '14 at 02:32
  • yes, take a look here: https://github.com/miktam/Disabler/blob/master/src/com/devoxx/disabler/DisablerActivity.java Also, take a look at the talk - there are plenty of examples there: http://www.devoxx.com/display/DV12/Code+injection+in++Android – Andrzej Karpuszonak Jan 15 '14 at 08:53
  • Hi, but do you support on the fly code injection for android apps in the dex format? I mean apps without source code. For example I am running an app and trying to inject my own classes for testing purposes? – P basak Jan 25 '14 at 01:29
3

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.

JesusFreke
  • 19,784
  • 5
  • 65
  • 68
0

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

YAZR
  • 79
  • 1
  • 8