简体   繁体   中英

Automatically change/instrument Android app's source code

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! :)

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM