Here's a sample processor that you can check out.
Create an empty gradle project.
Specify version 1.4.32
of the Kotlin plugin in the root project for use in other project modules.
plugins { kotlin("jvm") version "1.4.32" apply false } buildscript { dependencies { classpath(kotlin("gradle-plugin", version = "1.4.32")) } }
Add a module for hosting the processor.
In the module's build.gradle.kts
file, do the following:
google()
to repositories so that Gradle can find our plugins.dependencies
block.plugins { kotlin("jvm") } repositories { mavenCentral() google() } dependencies { implementation("com.google.devtools.ksp:symbol-processing-api:1.4.32-1.0.0-alpha08") }
The processor you're writing needs to implement com.google.devtools.ksp.processing.SymbolProcessorProvider
. Note the following:
SymbolProcessorProvider.create()
to create a SymbolProcessor
. Dependencies your processor needs (e.g. CodeGenerator
, processor options) are passed through the parameters of SymbolProcessorProvider.create()
.SymbolProcessor.process()
method.resolver.getSymbolsWithAnnotation()
to get the symbols you want to process, given the fully-qualified name of an annotation.com.google.devtools.ksp.symbol.KSVisitor
) for operating on symbols. A simple template visitor is com.google.devtools.ksp.symbol.KSDefaultVisitor
.SymbolProcessorProvider
and SymbolProcessor
interfaces, see the following files in the sample project.src/main/kotlin/BuilderProcessor.kt
src/main/kotlin/TestProcessor.kt
resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessor
.Create another module that contains a workload where you want to try out your processor.
In the project's settings.gradle.kts
, add google()
to repositories
for the KSP plugin.
pluginManagement { repositories { gradlePluginPortal() google() } }
In the new module's build.gradle.kts
, do the following:
com.google.devtools.ksp
plugin with the specified version.ksp(<your processor>)
to the list of dependencies.Run ./gradlew build
. You can find the generated code under build/generated/source/ksp
.
Here's a sample build.gradle.kts
to apply the KSP plugin to a workload.
plugins { id("com.google.devtools.ksp") version "1.4.32-1.0.0-alpha08" kotlin("jvm") } version = "1.0-SNAPSHOT" repositories { mavenCentral() google() } dependencies { implementation(kotlin("stdlib-jdk8")) implementation(project(":test-processor")) ksp(project(":test-processor")) }
In the projects settings.gradle
, add google()
to repositories
for the KSP plugin:
pluginManagement { repositories { gradlePluginPortal() google() } }
In your projects build.gradle
file add a plugins block containing the ksp plugin:
plugins { id "com.google.devtools.ksp" version "1.4.32-1.0.0-alpha08" }
In the modules build.gradle
, add the following:
com.google.devtools.ksp
plugin:apply plugin: 'com.google.devtools.ksp'
ksp <your processor>
to the list of dependencies.dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation project(":test-processor") ksp project(":test-processor") }
Processor options in SymbolProcessorProvider.create(options: Map<String, String>, ...)
are specified in gradle build scripts:
ksp { arg("option1", "value1") arg("option2", "value2") ... }
By default, IntelliJ or other IDEs don't know about the generated code and therefore references to those generated symbols will be marked unresolvable. To make, for example, IntelliJ be able to reason about the generated symbols, the following paths need to be marked as generated source root:
build/generated/ksp/main/kotlin/ build/generated/ksp/main/java/
and perhaps also resource directory if your IDE supports them:
build/generated/ksp/main/resources