Author/Software Engineer Ting-Yuan Huang and Product Manager David Winer
We are pleased to introduce the Alpha version of Kotlin Symbol Processing (KSP), a new tool to help you build lightweight compiler plug-ins in Kotlin. KSP offers similar functionality to KAPT, but twice as fast, direct access to the Kotlin compiler functionality, and was developed with multiplatform compatibility in mind.
KSP is compatible with Kotlin 1.4.30 and later. You can view open source and documentation in the KSP GitHub repo.
Why KSP?
One of the things the Kotlin developers reported to us the most was the need to speed up builds. Many developers iterate and deploy applications dozens of times a day, so slow builds can lead to a lot of time spent waiting. One of the biggest challenges in compiling Kotlin code is that Kotlin has no native annotation handling system. Annotation processors such as Room are ubiquitous on the Android platform and rely on Java annotation processing compatibility through the Kotlin Annotation Processing tool (KAPT). However, KAPT can be slow to run because it needs to generate an intermediate Java stub before the Java annotation processing system can extract it.
When designing KSP, we thought about what Kotlin’s annotation handling would look like if we were building from scratch. KSP provides a powerful and simple API that can parse Kotlin code directly, thus greatly reducing the build speed burden of KAPT generating stubs. In fact, initial benchmarks performed with the Room library showed that KSP was about twice as fast as KAPT.
Begin to use
To see KSP in action, download the KSP Playground project from GitHub. You can get:
- Library: a small test-Processor library that implements the builder pattern as a KSP processor
- Using projects: Shows how to use the Builder processor’s workload directory in an actual Kotlin project
All logic for implementing the builder resides in the test-processor – the only difference for the workload user between using KAPT and using KSP is that two lines of the build file are changed:
That’s the goal of KSP: most Android app developers don’t have to worry about internal architecture; Aside from this one-line change, the kSP-enabled library is similar to a normal annotation processor, but twice as fast. That said, using KAPT and KSP in the same module may initially slow down your build, so it’s best to use KSP and KAPT in separate modules during Alpha.
As more and more annotation processors adopt KSP, we expect that most of your modules will be able to use KSP as a direct replacement for KAPT. You can now see in the table which annotation processors provide KSP support. If there are no libraries in the table that have support or are implementing KSP support, please send us your valuable suggestions!
If you are a current author of a library that uses annotations, you can find more information on how to make your library kSP-compatible in the Quick Start and README guides.
The current Alpha phase of KSP is a good opportunity for library authors to delve deeper into it, and we welcome your feedback on the API in the KSP issues tracker. In addition, we’ll be Posting regular updates on our Google Developer account and Kotlin Slack’s # KSP channel. Since the release of the Developer preview last June, we have resolved over 100 bugs and issues, dozens of which have come from feedback from the excellent Kotlin library developer community. We hope you can continue to send us your questions via the KSP question tracker or leave a comment below.
Java is a registered trademark of Oracle and/or its affiliates.