Is it possible to make the usage of KSP easier?

Now, I’m adding kotlin-api to my framework.

The existing java-api uses annotation processor, the user gradle code looks like this

annotationProcessor(project("org.babyfish.jimmer:jimmer-apt:0.0.35"))

it’s very simple, isn’t it?

However, the kotlin-api should use KSP, the user gradle code looks like this

plugins {
    id("com.google.devtools.ksp") version "1.6.21-1.0.5" // 1
}
dependencies {
    ksp(project("org.babyfish.jimmer:jimmer-ksp:0.0.35")) //2
}
kotlin {
    sourceSets.main {
        kotlin.srcDir("build/generated/ksp/main/kotlin") //3
    }
}
  1. Add the ksp plugin
  2. Use ksp plugin to generate source files
  3. Add generated source files into compile path.

From the user’s point of view, using KSP is more troublesome than using annotation processor. Is there a way to make using KSP as easy as using annotation processor?

2 Likes

You cannot really avoid plugins part, because KSP is not a part of Kotlin Gradle Plugin, so it always at least 2 parts: ksp plugin and ksp dependency.

About sourceSets declaration it probably better to report a feature request to KSP issue tracker: GitHub - google/ksp: Kotlin Symbol Processing API

One more possible solution, is to replace your dependency with own Gradle Plugin, which will apply KSP plugin, dependency and add source sets, so it probably the easiest way for user to use it (though it may be less flexible in some cases)