How is it done? There are 2 ways:

Annotation processing Bytecode weaving

Both methods generate code during build time (as shown in the build process below), but each has it’s own advantages and disadvantages.

Build Process

Annotation Processing

This is a tool built in javac for scanning and processing annotations. You can register your own annotation processor for certain annotations. (In case you don’t know what an annotation is - check out official doc) Annotation processing has been available since Java 5 but a usable API has been available since Java 6. It allows you to generate new .java files, but not to modify existing ones. Because it generates .java files, it can also be debugged and is easy to test.

Bytecode Weaving

This method allows you to inspect, create new or modify existing .class files before they are compiled to dex file. Because it manipulates bytecode, it cannot be debugged and is hard to test. Since Gradle plugin 1.5.0-beta1 we have a more simple API for achieving this in the Android world, called Transform API.

With the library Blade I wanted to destroy as much boilerplate/repetitive code as possible, so that it is easy to use while allowing the user to debug the code as if it was his own (not some black box). How did I do that? I simply combined them — an annotation processor generates new code that is then called by weaved bytecode.