A look into Kotlin KAPT for Android

Reading Time: 3 minutes

Kotlin Annotation Processing Tool. What is it? When to use it? Let’s learn about this Android tool.

It has been three years since Kotlin was introduced to the Android world. Like many other programming languages, annotations are a needed and powerful tool, but surprisingly Kotlin was not compatible with Java’s Annotation Processing Tools. So let’s dive into what is KAPT for Android.

When to use KAPT?

The first question that usually comes to our mind when talking about annotation processing in Android, especially coming from a Java background, is whether you should be using KAPT. The answer is, KAPT is the official tool for doing annotation processing in Kotlin, thus the answer is always. This is as long as you need annotation processing, otherwise, why add it?

Including KAPT in an Android project

To start using KAPT in an Android project, you should add it in the app gradle dependencies as you do with any other external tool. Use the following line to include it:

apply plugin: 'kotlin-kapt'

It is that simple, but there is a common error when adding this dependency to the project. The error says “Kotlin plugin should be enabled before ‘kotlin-kapt'”, the solution to this is simple, but when you have tons of lines of code it can be confusing. What this error means is that when adding the above line to your Gradle file, you need to add the Kotlin plugin first like this:

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

If you switch lines 1 and 2 you will be getting the error above.

What is KAPT used for?

The answer to this is straight, it is used for annotation processing in Kotlin. But if you are wondering this it probably means you are not familiar with annotation processing.

Let’s first learn what are annotations. Annotations are the words you see at the top of files and methods preceded with an ‘@’ symbol. The most common example that you may have already seen is the @Override annotation. But there are many more, some of which can be directly interpreted by Kotlin, those are called built-in annotations. Others need an ‘Annotation Processor’, also known as KAPT, to be used in your code.

The definition for annotations in the Kotlin documentation states the following:

Annotations are means of attaching metadata to code.

https://kotlinlang.org/docs/reference/annotations.html

If you want to know more about annotations the documentation above should be helpful.

Ok, but… What does an Annotation Processor do?

Annotation Processors date back to when Android was based on Java. There are many tutorials on annotations in Java, because at some time in the past they became a very powerful (which they are) and popular tool.

Wether it is in Java or Kotlin, annotation processors are tools that run through your code during compile time and generate code themselves for your app. This explains why they are so powerful, if you know how to use annotations, they can save time for you by generating code themselves.

A great thing of KAPT is that if you have a project with both Java and Kotlin files, it can take care of both. The opposite is not true as APT (Java annotation processing tool) can’t interpret Kotlin annotations.

Wrapping up

Now you should have a clearer view of what KAPT for Android is. As you dive deeper in the world of annotations you will learn that you can create your own annotations and make your coding more practical and safe.

If you liked the content on this post, please subscribe at the bottom of the page. If you have any doubts don’t hesitate to leave a comment and I will reply as soon as possible. You can also donate a coffee to support me so I can keep on creating things like this!

See you next time!

Evana Margain Puig

Leave a Reply