Meta-programming with KSP and Kotlin compiler plugins
summary
KSP replaces KAPT and runs about 2× faster, provides a stable processor API, supports Java sources, and only generates new files — it has no expression-level introspection and cannot modify existing declarations. Kotlin compiler plugins can change IR and synthesize private members via synthetic resolve extension + IR generation extension, enabling Compose, kotlinx.serialization, Zipline, and Touchlab’s Swift‑friendly API, but require complex Gradle/IDE wiring, unstable Kotlin APIs, painful maintenance, and slower compile/test iterations. Use KSP unless you truly need expression‑level or in‑place code modification.
AI-written summary · about
chapters (16)
- 0:00Introduction and overview
- 2:39What is metaprogramming
- 5:32History of JVM metaprogramming
- 6:59KSP overview and benefits
- 8:47KSP limitations
- 10:16Compiler plugins introduction
- 12:23Compiler plugin structure and complexity
- 14:52Synthetic resolve extension
- 16:39IR generation extension
- 18:06Setting up plugin development
- 21:14Hard learned lessons
- 25:13Learning from existing plugins
- 26:16Choosing between KSP and plugins
- 28:03Testing compiler plugins
- 29:08KMP and compiler plugins need
- 31:13Recap and recommendations
related talks
44mMeta-programming with KSP and Kotlin compiler plugins
38mKSP and Plugins. Optimizing Kotlin Multiplatform common code
26mBuild your own compiler with KSP
37mWhy you should positively consider introducing and migrating to KSP
15mOn the Applicability of Annotation-Based Source Code Modification in Kotlin (Work...
36mMeta-programming with Kotlin compiler plugins
47mDealing With KSP in Common Code for Android and iOS
19mDemystifying the Kotlin Compiler
