Question

I want to automatically transform the source code of an Android application so that all the methods in all classes include two more instructions.

Basically, what I want is to instrument the source code of the app, but with some restraints (for instance, an class that extends an Activity needs a different approach in the onCreate method).

I saw the ASM 2.0 Framework, but apparently this framework is designed to manipulate JVM bytecode, and Android uses Dalvik Virtual Machine, so it wouldn't work...

Does anyone knows a framework or a tool that I can use for this purpose? Thanks! :)

Was it helpful?

Solution

The authors of ASM offer a byte code library similar to ASM that deals with Android's DEX format. The 2.0 version of ASM is old by the way. The current version number is 4 with 5 being in the beta state.

It does however sound to me like you should not apply this instrumentation at run time. You should usually not use run time byte code generation when you can also generate this code at compile time. Instrumentation is a quite heavy tool.

Instead, maybe source code generation is an option for you? Or maybe your problem is solved as simple as by applying the template method pattern? You might also look into the Java compiler API or into tools like Spoon which you could apply in your build process.

OTHER TIPS

You might take a look at the dexlib2 library. It allows reading/modification/writing of dex files. For example, https://gist.github.com/JesusFreke/6945806

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top