androidengineers.Book a session

Advanced Language Features

Annotations & Annotation Processing (kapt/ksp overview)

article15 minHard

An annotation needs a consumer

Annotations attach metadata to declarations. They do not perform work by themselves. A compiler, processor, framework, or reflection-based consumer must interpret that metadata.

@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
annotation class Feature(val name: String)

@Feature("learning")
class StudyScreen

Target constrains where the annotation can be applied; Retention controls how long metadata survives. Choose runtime retention only when runtime inspection is needed. Kotlin properties can map to JVM fields, getters, and constructor parameters, so use-site targets matter for libraries expecting a particular location.

Kapt supports Java annotation processors through generated stubs. KSP exposes Kotlin symbols to processors. Processor compatibility, incremental behavior, and plugin versions must be checked together; do not simply replace one plugin name and assume the same processor works.

Exercise

Create an annotation with a required name and inspect where your intended consumer expects it. Add a deliberately unsupported target and observe the compiler error.

Check: distinguish metadata declaration, build-time code generation, and runtime reflection as separate steps.

Reference: Annotations

YOUR LEARNING JOURNEY

0 of 110 available lessons completed

Progress saved in this browser. No account needed.
Annotations & Annotation Processing (kapt/ksp overview) | Kotlin Core Programming | Android Engineers