Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do I need to use Kapt in Gradle dependencies?

I have been installing dependencies in Gradle and I don't understand why sometimes I need to use kapt for libraries like lifecycle and room database to use @Something annotations. But in some libraries like Retrofit2 and Gson, I don't need use kapt and I can use annotations such as @SerializedName?

like image 814
gd08xxx Avatar asked Jun 20 '19 18:06

gd08xxx


People also ask

Do I need kapt?

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.

What is kapt in gradle?

1. Kapt is the Kotlin Annotation Processing Tool, and it's in pretty good shape these days. If you want to be able to reference generated code from Kotlin, you need to use kapt. To do that, simply include the plugin in your build.gradle file with the line : apply plugin: 'kotlin-kapt' 2.

Is kapt deprecated?

app: Original kapt is deprecated.


1 Answers

Annotations (e.g. @Something) are basically labels for code. You mark one part of the code so that some other code can find those markings.

This "other code" is usually an Annotation Processor. It finds annotations and does something with code marked with those annotations. E.g. it can generate new code (like Dagger, Butterknife, etc.).


Depending on the way you introduce dependencies in your project, (depending on the keyword you use - implementation, api, compileOnly, runtimeOnly, annotationProcessor, kapt, etc.), the dependency will be used by your project differently.

If you use annotationProcessor, your dependency will not be packed within your app, but will be used during the compilation of your app.

You don't want to pack the compiler (the code that handles @AnAnnotation) within your app, since it's just used to properly prepare the code of your app (and is never used within your application in Runtime).

Think of it this way:

If you're going on a train and you need to print a train ticket, you don't want to carry a printer with you on the train. After the printer is done printing the ticket, you take the ticket and go on the train. Printer has done its job already. You can leave it.

If you mark some code with @AnAnnotation you just want the library that handles that annotation to do its job and disappear. Hence the special type of a dependency - annotationProcessor.

Now about kapt. This is simple. If you want to use Annotation Processors in projects with Kotlin code, just use kapt instead of annotationProcessor. Think of it as annotationProcessor with Kotlin support.


Some libraries use @Annotations differently. They do not cause any code to be generated in compile-time, but they use annotations in runtime.

Those are usually reflection-based libraries that "look through" the code in Runtime. Just like Retrofit is looking through your interface when your app is executed.

That's why you include a library with @Annotations normally within your application, and those annotations are packed within your apk for Runtime operation.


Summerizing:

annotationProcessor and kapt keywords, are to help you specify how dependencies will be used in your project.

If you want to introduce a library that uses annotations and generates some code, use kapt not to "bloat" your apk with code that already has done its job, and will never be used again.

like image 63
Bartek Lipinski Avatar answered Oct 04 '22 00:10

Bartek Lipinski