CodeBlanks Logo Icon CodeBlanks

Plugin vs KSP

Learn the difference between compiler plugins and KSP.

Plugin vs KSP

A compiler plugin and KSP both run at compile time, but their power and complexity differ. KSP only reads symbols; a plugin can transform the whole AST.

KSP is simpler and stable; it is ideal for code generation (Room, Moshi). A plugin is more powerful but more complex; it is necessary to modify existing code (all-open, no-arg, compose). A plugin offers class open marking, synthetic constructors and IR transforms that KSP cannot do. When building a tool, ask first whether KSP is enough; if not, consider a plugin.

When to use which?

If you only generate new code, KSP is enough. If you need to modify existing classes (make open, add a constructor, transform IR) you need a plugin.

Example: generating a DAO from an @Entity annotation needs only KSP. But to make Spring's @Configuration open for a CGLIB proxy, the all-open plugin is used. UI frameworks like Compose use a plugin to transform @Composable functions at compile time. This distinction affects architecture decisions.

Practice Write the mode comparison

Define data class Mode(val name: String, val level: String). In main, build listOf(Mode("Plugin", "AST"), Mode("KSP", "Symbol")). For each, write println("${'$'}{it.name}: ${'$'}{it.level}").

Starter code

Main.kt
// add the Mode data class

fun main() {
    // create two modes and print
}

Sample solution

Main.kt
data class Mode(val name: String, val level: String)
fun main() {
    val modes = listOf(Mode("Plugin", "AST"), Mode("KSP", "Symbol"))
    modes.forEach { println("${it.name}: ${it.level}") }
}

Expected output

Plugin: AST
KSP: Symbol

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Match plugin and KSP

Term Meaning
KSP Symbol reading
Plugin AST transform
all-open Makes class open
Room KSP-using library

2 Which is needed to make a class open?

  • Only KSP
  • Compiler plugin (all-open) Correct answer
  • Runtime reflection
  • Only an IDE setting

Explanation: Making a class open modifies existing code; only a plugin can do that.

3 Complete the mode list

Question
val modes = listOf(Mode("Plugin", "AST"), Mode("____", "Symbol"))
Answer
val modes = listOf(Mode("Plugin", "AST"), Mode("KSP", "Symbol"))

Explanation: KSP works at symbol level.

All lessons in Expert