Hello World of Annotation Processing in Kotlin

- 5 mins

This is the result of some trial and error until I got a simple annotation processing example working, with the challenge of not using Java, but Kotlin instead. Hopefully will help you get started!

Sample code here: https://github.com/miquelbeltran/kotlin-code-gen-sample

The goal is to generate a new class with the method getName() which will return the String “World”.

So if we annotate the class Hello, it will generate the class Generated_Hello, and it will contain the method getName() that returns “World”.

PS: You can jump directly into Building the processor for the good stuff.


Setup

First step is to setup your project. I went with a pure Kotlin project directly with IntelliJ IDEA.

In the root folder we won’t have any code, instead, create two modules inside the root folder, one that will contain our processor and another one that will contain our main code.

It’s important to do so, because you need to compile & process the processor before our main module!

On the root we will have two files: a build.gradle and a settings.gradle:

We will have two modules: generator and sample-main


Let’s create the sample-main first:

Expect a structure similar to this one:

So far not much for a setup, we will go back to the Main.kt and the build.gradle later, let’s create the generator now.


Similarly, create a new module named generator with a package structure and add two Kotlin files:

GenName is going to be our custom annotation. It will be a simple annotation without extra parameters. The way to create an annotation in Kotlin is by adding annotation in front of class.

We will look at the Generator later, now back to the main module.


Main Module Code

On the Main.kt, create a class named Hello and annotate it with your new annotation.

Secondly, generate a main method (you can just type main and hit Enter) and add a call to the Generated_Hello (which does not exist yet).

As you expect, the project will fail to resolve both GenName and Generated_Hello. So far it’s OK.

Now we will add the module dependency for GenName.

https://gist.github.com/miquelbeltran/c56905a7b142f2666412d7d3481c27e5#file-sample-main-build-gradle

On the main module build.gradle, you will have to add the dependency to the generator module, both as kapt and compileOnly.

There are two more tweaks you will need:


Building the processor

Everything except our annotation processor is ready, let’s build it.

First, create a class that will extend AbstractProcessor:

class Generator: AbstractProcessor()

Our processor needs to implement the following methods:

Going step by step:

Two important notes:

One more thing: AutoService

In order to kapt to run our processor, we need to add a META-INF into our generator module indicating our processors. However AutoService from Google will generate this for us. Go for it and use it!

Also don’t forget, we need to add it to the generator build.gradle

At the time of this article, 1.0-rc3 did not work, so I had to step back to 1.0-rc2.

Let’s build and check our generated file:

If everything is alright, now you can run the main method, and you will see Hello World printed on the terminal.

Hello World
Process finished with exit code 0

Summary

Sample code here: https://github.com/miquelbeltran/kotlin-code-gen-sample

##References

Miquel Beltran

Miquel Beltran

Software engineer and Android developer

rss facebook twitter github youtube mail spotify instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora