Skip to content
✔️ [ARTICLE] Writing your own Annotation Processors in Android
Java Kotlin
Branch: master
Clone or download
Latest commit 53878f8 Jan 13, 2020

Full article here:
Android Weekly

What are Annotations ?

An annotation is a form of syntactic metadata that can be added to Java source code.
We can annotate classes, interfaces, methods, variables, parameters etc.
Java annotations can be read from source files. Java annotations can also be embedded in and read from class files generated by the compiler.
Annotations can be retained by Java VM at run-time and read via reflection.

public @interface BindView {
    int value();

Creating an annotation requires two pieces of information: Retention and Target

A RetentionPolicy specifies how long, in terms of the program lifecycle, the annotation should be retained for. For example, annotations may be retained during compile-time or runtime, depending on the retention policy associated with the annotation.

The Target of an annotation specifies which Java ElementTypes an annotation can be applied to.

Why Annotation Processors ?

1. Runtime Compile time

2. No Reflection

3. Generate boilerplate code*

*Annotation processing can only be used to generate new files and not to modify existing ones

How does Annotation Processing work ?

The annotation processing takes place in many rounds. The compiler reads a java source file with the registered annotations and calls their corresponding annotation processors which will generate more java source files with more annotations. These new annotations will again call their corresponding annotation processors which will again generate more java source files. This cycle continues until no new java source file is generated in the cycle.

How to register a Processor ?

A processor must be registered to the compiler so that it can run while the application is being compiled.

Annotation Processors can be registered in two ways.


Create a directory structure like this
Now, in the services directory, we will create a file named javax.annotation.processing.Processor.
This file will list the classes (fully qualified name) that the compiler will call when it compiles the application's source code while annotation processing.


Use Google's AutoService library.
Just annotate your Processor with @AutoService(Processor.class)



import javax.annotation.processing.Processor;

final class MyProcessor implements Processor {

How to create a Processor ?

To create our custom Annotation Processor we need to make a class that extends AbstractProcessor which defines the base methods for the processing.
We have to override four methods to provide our implementations for the processing.

public class Processor extends AbstractProcessor {

    public synchronized void init(ProcessingEnvironment processingEnvironment) {
        // initialize helper/utility classes...

    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnvironment) {
        // do processing...
        return true;

    public Set<String> getSupportedAnnotationTypes() {
        return new HashSet<String>() {{

    public SourceVersion getSupportedSourceVersion() {
        return SourceVersion.latestSupported();

init() gives you Helper/Utility classes like Filer (to generate files), Messager (for logging errors, warnings etc.), Elements (utility methods for operating on program elements), Types (utility methods for operating on types) etc.
You can get these classes with processingEnvironment.

process() is the method where all the processing happens.
It gives you annotations that need to be processed and roundEnvironment provides the information about the round and has some utility methods for querying elements.
e.g. processingOver(),getRootElements(),getElementsAnnotatedWith() etc.

getSupportedAnnotationTypes() returns the names of the annotation types supported by this processor.

getSupportedSourceVersion() returns the latest source version supported by this processor.

Note : You can also use @SupportedAnnotationTypes and @SupportedSourceVersion instead of getSupportedAnnotationTypes() and getSupportedSourceVersion() respectively.

Demo #1 : Singleton


Demo #2 : KSingleton

  1. KSingleton.kt
  2. Processor.kt

Important Classes/Objects

ProcessingEnvironment : An annotation processing tool framework will provide an annotation processor with an object implementing this interface so the processor can use facilities provided by the framework to write new files, report error messages, and find other utilities.

Elements : Utility methods for operating on program elements. Can be accessed by ProcessingEnvironment.getElementUtils().

Types : Utility methods for operating on types. Can be accessed by ProcessingEnvironment.getTypeUtils().

Messager : A Messager provides the way for an annotation processor to report error messages, warnings, and other notices. Can be accessed by ProcessingEnvironment.getMessager().

Filer : This interface supports the creation of new files by an annotation processor. Can be accessed by ProcessingEnvironment.getFiler().

RoundEnvironment : An annotation processing tool framework will provide an annotation processor with an object implementing this interface so that the processor can query for information about a round of annotation processing. We can get our desired elements with RoundEnvironment.getRootElements() and RoundEnvironment.getElementsAnnotatedWith() methods.

ElementFilter : Filters for selecting just the elements of interest from a collection of elements. Contains methods like ElementFilter.constructorsIn(), ElementFilter.methodsIn(), ElementFilter.fieldsIn() etc.

How to generate .java files ?

We can use Square's JavaPoet library for generating .java files.
JavaPoet makes it really simple to define a class structure and write it while processing. It creates classes that are very close to a handwritten code.


This HelloWorld class

package com.example.helloworld;

public final class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello, JavaPoet!");

can be generated by this piece of code

MethodSpec main = MethodSpec.methodBuilder("main")
    .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
    .addParameter(String[].class, "args")
    .addStatement("$T.out.println($S)", System.class, "Hello, JavaPoet!")

TypeSpec helloWorld = TypeSpec.classBuilder("HelloWorld")
    .addModifiers(Modifier.PUBLIC, Modifier.FINAL)

JavaFile javaFile = JavaFile.builder("com.example.helloworld", helloWorld)


Note : There's also another Square library KotlinPoet for generating .kt files.

Demo #3 : ButterKnife








This ButterKnife library will generate separate classes (with suffix Binder) for each of the Activity where we used the annotations @BindView or @OnClick.

public class MainActivity extends AppCompatActivity {

    private int numberOfTimesTextViewClicked = 0;

    TextView textView;

    protected void onCreate(Bundle savedInstanceState) {


    void onTextViewClicked(View view) {

public class MainActivityBinder {
    public MainActivityBinder(MainActivity activity) {

    private void bindViews(MainActivity activity) {
        activity.textView = (TextView) activity.findViewById(2131165314);

    private void bindOnClicks(final MainActivity activity) {
        activity.findViewById(2131165314).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {


You can’t perform that action at this time.