For a quick start, you can create your own processor or get a sample one.
Create a processor of your own
Create an empty gradle project.
1.6.10of the Kotlin plugin in the root project for use in other project modules:
Add a module for hosting the processor.
In the module's build script, apply Kotlin plugin and add the KSP API to the
You'll need to implement
com.google.devtools.ksp.processing.SymbolProcessorProvider. Your implementation of
SymbolProcessorProviderwill be loaded as a service to instantiate the
SymbolProcessoryou implement. Note the following:
SymbolProcessorProvider.create()to create a
SymbolProcessor. Pass dependencies that your processor needs (such as
CodeGenerator, processor options) through the parameters of
Your main logic should be in the
resolver.getSymbolsWithAnnotation()to get the symbols you want to process, given the fully-qualified name of an annotation.
A common use case for KSP is to implement a customized visitor (interface
com.google.devtools.ksp.symbol.KSVisitor) for operating on symbols. A simple template visitor is
For sample implementations of the
SymbolProcessorinterfaces, see the following files in the sample project.
After writing your own processor, register your processor provider to the package by including its fully-qualified name in
Use your own processor in a project
Create another module that contains a workload where you want to try out your processor.
In the module's build script, apply the
com.google.devtools.kspplugin with the specified version and add your processor to the list of dependencies.
./gradlew build. You can find the generated code under
Here's a sample build script to apply the KSP plugin to a workload:
Pass options to processors
Processor options in
SymbolProcessorEnvironment.options are specified in gradle build scripts:
Make IDE aware of generated code
By default, IntelliJ IDEA or other IDEs don't know about the generated code. So it will mark references to generated symbols unresolvable. To make an IDE be able to reason about the generated symbols, mark the following paths as generated source roots:
If your IDE supports resource directories, also mark the following one:
It may also be necessary to configure these directories in your KSP consumer module's build script:
If you are using IntelliJ IDEA and KSP in a Gradle plugin then the above snippet will give the following warning:
In this case, use the following script instead: