Kotlin Symbol Processing API
Kotlin Symbol Processing (KSP) is a framework for Kotlin source code generation. 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 processors include:
To learn how to create your first KSP-based processor, see Getting started with KSP.
Requirements
The latest KSP version, 2.3.12, supports the following dependency versions:
Dependency | Supported versions |
|---|---|
Kotlin Gradle plugin (KGP) | 2.3.20–2.4.x |
Android Gradle plugin (AGP) | 8.13.0 or later |
Gradle | 8.13 or later. For Android projects, also check the AGP and Gradle compatibility table. |
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 then use these symbols to inspect declarations such as classes, functions, properties, and types.
KSP fits into the compilation process as follows:
KSP loads the processors and runs them.
Processors use KSP to analyze the source code and its dependencies. They inspect symbols and types and generate new files, such as source files and resources. Importantly, they cannot modify existing files.
The Kotlin compiler compiles the original source code together with the generated code.
To learn more about KSP, watch this video:
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
The following table lists popular libraries and their various stages of support for KSP:
Library | Status |
|---|---|
Room 2.x | |
Room 3.x | |
Moshi | |
RxHttp | |
Kotshi | |
Lyricist | |
Lich SavedState | |
gRPC Dekorator | |
EasyAdapter | |
Glide | |
Micronaut | |
Epoxy | |
Paris | |
Auto Dagger | |
SealedX | |
Ktorfit | |
Mockative | |
Kotest | |
Dagger | |
Hilt | |
DeepLinkDispatch | |
Motif | |
Auto Factory | |
Koin Annotations | KSP processor is deprecated in favor of the Koin Compiler Plugin |
See also
Learn how to add a processor to your project or create your own.
Learn how KSP represents source code as symbols and when to resolve types.
Learn how to use KSP in a Kotlin Multiplatform project.
Find the KSP equivalents of Java annotation processing APIs.
Learn how to migrate your annotation processors from kapt to KSP.
Explore the example projects in the KSP repository.