Kotlin Symbol Processing API
Kotlin Symbol Processing (KSP) is a source code generation framework for Kotlin. With the KSP API, you can create processors that inspect static information about your source code and generate new code from it. The most common use case is generating code based on annotations.
KSP aims to simplify the creation of lightweight compiler plugins. A compiler plugin built with KSP is called a symbol processor, or processor for short. KSP's well-defined API hides compiler changes, so you don't need to spend much effort maintaining your processors. However, this approach comes with trade-offs. For example, KSP-based processors can't examine expressions or statements, and they can't modify the source code.
Typical use cases for KSP-based plugins include:
To learn how to create your first KSP-based processor, see Getting started with KSP.
Requirements
The latest KSP version, 2.3.10, supports the following dependency versions:
Dependency | Min and max versions |
|---|---|
Kotlin Gradle plugin (KGP) | 2.2.10–2.3.x |
Android Gradle Plugin (AGP) | 8.12.0 or later |
Gradle | 8.13 or later. For AGP 9.0 or later, use Gradle 9.x. |
JDK | 17 or later |
How KSP works during compilation
KSP represents Kotlin source code as a hierarchy of symbols based on the Kotlin grammar. Processors use these symbols to inspect declarations such as classes, functions, properties, and types.
KSP fits into the compilation process as follows:
KSP processors analyze source code and resources.
The processors generate source files or other outputs.
The Kotlin compiler compiles the original source code together with the generated code.
To learn more about KSP, watch this video:
How KSP looks at source files
Most processors navigate through the various program structures of the input source code. Before diving into usage of the API, let's see at how a file might look from KSP's point of view:
This view lists common things that are declared in the file: classes, functions, properties, and so on.
How KSP runs a processor
KSP uses an implementation of SymbolProcessorProvider as the entry point for creating a SymbolProcessor instance:
The SymbolProcessor interface contains the processing logic. KSP calls the process() function and provides a Resolver, which the processor uses to access symbols in the source code:
For a step-by-step guide on how to implement and register a processor, see Getting started with KSP.
Supported libraries
This table lists popular libraries on Android and their various stages of support for KSP:
Library | Status |
|---|---|
Room | |
Moshi | |
RxHttp | |
Kotshi | |
Lyricist | |
Lich SavedState | |
gRPC Dekorator | |
EasyAdapter | |
Koin Annotations | |
Glide | |
Micronaut | |
Epoxy | |
Paris | |
Auto Dagger | |
SealedX | |
Ktorfit | |
Mockative | |
Kotest | |
DeeplinkDispatch | |
Dagger | |
Motif | |
Hilt | |
Auto Factory |