KSP 快速入门

For a quick start, you can create your own processor or get a sample one.

Create a processor of your own

  1. Create an empty gradle project.
  2. Specify version 1.9.10 of the Kotlin plugin in the root project for use in other project modules:

【Kotlin】

  1. plugins {
  2. kotlin("jvm") version "1.9.10" apply false
  3. }
  4. buildscript {
  5. dependencies {
  6. classpath(kotlin("gradle-plugin", version = "1.9.10"))
  7. }
  8. }

【Groovy】

  1. plugins {
  2. id 'org.jetbrains.kotlin.jvm' version '1.9.10' apply false
  3. }
  4. buildscript {
  5. dependencies {
  6. classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.10'
  7. }
  8. }
  1. Add a module for hosting the processor.

  2. In the module’s build script, apply Kotlin plugin and add the KSP API to the dependencies block.

【Kotlin】

  1. plugins {
  2. kotlin("jvm")
  3. }
  4. repositories {
  5. mavenCentral()
  6. }
  7. dependencies {
  8. implementation("com.google.devtools.ksp:symbol-processing-api:1.9.10-1.0.13")
  9. }

【Groovy】

  1. plugins {
  2. id 'org.jetbrains.kotlin.jvm' version '1.9.10'
  3. }
  4. repositories {
  5. mavenCentral()
  6. }
  7. dependencies {
  8. implementation 'com.google.devtools.ksp:symbol-processing-api:1.9.10-1.0.13'
  9. }
  1. You’ll need to implement com.google.devtools.ksp.processing.SymbolProcessor and com.google.devtools.ksp.processing.SymbolProcessorProvider. Your implementation of SymbolProcessorProvider will be loaded as a service to instantiate the SymbolProcessor you implement. Note the following:
    • Implement SymbolProcessorProvider.create() to create a SymbolProcessor. Pass dependencies that your processor needs (such as CodeGenerator, processor options) through the parameters of SymbolProcessorProvider.create().
    • Your main logic should be in the SymbolProcessor.process() method.
    • Use 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 com.google.devtools.ksp.symbol.KSDefaultVisitor.
    • For sample implementations of the SymbolProcessorProvider and SymbolProcessor interfaces, see the following files in the sample project.
      • src/main/kotlin/BuilderProcessor.kt
      • src/main/kotlin/TestProcessor.kt
    • After writing your own processor, register your processor provider to the package by including its fully-qualified name in resources/META-INF/services/com.google.devtools.ksp.processing.SymbolProcessorProvider.

Use your own processor in a project

  1. Create another module that contains a workload where you want to try out your processor.

【Kotlin】

  1. pluginManagement {
  2. repositories {
  3. gradlePluginPortal()
  4. }
  5. }

【Groovy】

  1. pluginManagement {
  2. repositories {
  3. gradlePluginPortal()
  4. }
  5. }
  1. In the module’s build script, apply the com.google.devtools.ksp plugin with the specified version and add your processor to the list of dependencies.

【Kotlin】

  1. plugins {
  2. id("com.google.devtools.ksp") version "1.9.10-1.0.13"
  3. }
  4. dependencies {
  5. implementation(kotlin("stdlib-jdk8"))
  6. implementation(project(":test-processor"))
  7. ksp(project(":test-processor"))
  8. }

【Groovy】

  1. plugins {
  2. id 'com.google.devtools.ksp' version '1.9.10-1.0.13'
  3. }
  4. dependencies {
  5. implementation 'org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version'
  6. implementation project(':test-processor')
  7. ksp project(':test-processor')
  8. }
  1. Run ./gradlew build. You can find the generated code under build/generated/source/ksp.

Here’s a sample build script to apply the KSP plugin to a workload:

【Kotlin】

  1. plugins {
  2. id("com.google.devtools.ksp") version "1.9.10-1.0.13"
  3. kotlin("jvm")
  4. }
  5. repositories {
  6. mavenCentral()
  7. }
  8. dependencies {
  9. implementation(kotlin("stdlib-jdk8"))
  10. implementation(project(":test-processor"))
  11. ksp(project(":test-processor"))
  12. }

【Groovy】

  1. plugins {
  2. id 'com.google.devtools.ksp' version '1.9.10-1.0.13'
  3. id 'org.jetbrains.kotlin.jvm' version '1.9.10'
  4. }
  5. repositories {
  6. mavenCentral()
  7. }
  8. dependencies {
  9. implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.9.10'
  10. implementation project(':test-processor')
  11. ksp project(':test-processor')
  12. }

Pass options to processors

Processor options in SymbolProcessorEnvironment.options are specified in gradle build scripts:

  1. ksp {
  2. arg("option1", "value1")
  3. arg("option2", "value2")
  4. ...
  5. }

Make IDE aware of generated code

Generated source files are registered automatically since KSP 1.8.0-1.0.9. If you’re using KSP 1.0.9 or newer and don’t need to make the IDE aware of generated resources, feel free to skip this section.

KSP 快速入门 - 图1

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:

  1. build/generated/ksp/main/kotlin/
  2. build/generated/ksp/main/java/

If your IDE supports resource directories, also mark the following one:

  1. build/generated/ksp/main/resources/

It may also be necessary to configure these directories in your KSP consumer module’s build script:

【Kotlin】

  1. kotlin {
  2. sourceSets.main {
  3. kotlin.srcDir("build/generated/ksp/main/kotlin")
  4. }
  5. sourceSets.test {
  6. kotlin.srcDir("build/generated/ksp/test/kotlin")
  7. }
  8. }

【Groovy】

  1. kotlin {
  2. sourceSets {
  3. main.kotlin.srcDirs += 'build/generated/ksp/main/kotlin'
  4. test.kotlin.srcDirs += 'build/generated/ksp/test/kotlin'
  5. }
  6. }

If you are using IntelliJ IDEA and KSP in a Gradle plugin then the above snippet will give the following warning:

  1. Execution optimizations have been disabled for task ':publishPluginJar' to ensure correctness due to the following reasons:
  2. Gradle detected a problem with the following location: '../build/generated/ksp/main/kotlin'.
  3. Reason: Task ':publishPluginJar' uses this output of task ':kspKotlin' without declaring an explicit or implicit dependency.

In this case, use the following script instead:

【Kotlin】

  1. plugins {
  2. // ...
  3. idea
  4. }
  5. idea {
  6. module {
  7. // Not using += due to https://github.com/gradle/gradle/issues/8749
  8. sourceDirs = sourceDirs + file("build/generated/ksp/main/kotlin") // or tasks["kspKotlin"].destination
  9. testSourceDirs = testSourceDirs + file("build/generated/ksp/test/kotlin")
  10. generatedSourceDirs = generatedSourceDirs + file("build/generated/ksp/main/kotlin") + file("build/generated/ksp/test/kotlin")
  11. }
  12. }

【Groovy】

  1. plugins {
  2. // ...
  3. id 'idea'
  4. }
  5. idea {
  6. module {
  7. // Not using += due to https://github.com/gradle/gradle/issues/8749
  8. sourceDirs = sourceDirs + file('build/generated/ksp/main/kotlin') // or tasks["kspKotlin"].destination
  9. testSourceDirs = testSourceDirs + file('build/generated/ksp/test/kotlin')
  10. generatedSourceDirs = generatedSourceDirs + file('build/generated/ksp/main/kotlin') + file('build/generated/ksp/test/kotlin')
  11. }
  12. }