Mike G
Mike G

Reputation: 4959

Java byte-code injection on android

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

Answers (3)

YAZR
YAZR

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

Andrei Karpuszonak
Andrei Karpuszonak

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:

  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)

Upvotes: 3

JesusFreke
JesusFreke

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

Related Questions