07 九月 2021
Posted by Ting-Yuan Huang, Software Engineer and Jiaxiang Chen, Software Engineer
Kotlin Symbol Processing (KSP), our new tool for building lightweight compiler plugins in Kotlin, is now stable! KSP offers similar functionality to the Kotlin Annotation Processing Tool (KAPT), however it’s up to 2x faster, offers direct access to Kotlin language constructs, and offers support for multiplatform targets.
Over the past few months, KSP has gone through 32 releases with over 162 bugs reported from the community and fixed by our team. If you were waiting to adopt it, now is the time to check it out.
On the Android team, we regularly ask developers: what are your biggest frustrations with writing apps today? One of the top issues that comes up repeatedly is build speed. Over the years we’ve been making steady improvements to the Android build toolchain, and today we’re excited to add to those improvements with KSP. KSP is the next generation of annotation processing in Kotlin: it will dramatically improve build speed for Kotlin developers, and unlike KAPT, it offers support for Kotlin/Native and Kotlin/JS.
"Adding KSP support to Room improved the compilation speed and also enabled Room to better understand Kotlin code, such as the nullability of generics that was not possible with KAPT. It also unlocks new possibilities like generating Kotlin code, which will allow Room to have a better Kotlin user experience in the future." - Yigit Boyar, Software Engineer, Android
The Kotlin Annotation Processing Tool (KAPT) works with Java’s annotation processing infrastructure to make most Java language annotation processors work in Kotlin out of the box. To do this, KAPT compiles Kotlin code into Java stubs that retain information that Java annotation processors care about. Creating these stubs is costly though, and means the compiler must resolve all the symbols in your program multiple times (once to generate stubs, and then again to do the actual compilation).
KSP moves away from the stub generation model by working as a Kotlin compiler plugin — it allows annotation processors to read and analyze source programs and resources directly in Kotlin instead of requiring you to depend on the Java annotation processing infrastructure. This both dramatically improves build speed (up to 2x faster for Room's Kotlin test app) and means that KSP can be used for non-Android and non-JVM environments like Kotlin/Native and Kotlin/JS.
To start using KSP, download the KSP playground project from GitHub, which shows how to use KSP both as an annotation processor and as a consuming app/library:
test-processor
library that implements the builder pattern as a
KSP processor
workload
directory that
shows how to use the builder processor in a real-world Kotlin project
If you’re an app developer, check out the list of supported libraries and the quickstart guide for moving a module over from KAPT to KSP.
If you’re using Moshi or Room in your project, you can already try out KSP by making a quick fix to your module’s build file. For example, to use the KSP version of Room in a Gradle module you can simply replace the KAPT plugin with KSP and swap out the KSP dependency:
apply plugin: 'com.google.devtools.ksp' dependencies { ... implementation "androidx.room:room-runtime:$room_version"kapt "androidx.room:room-compiler:$room_version"ksp "androidx.room:room-compiler:$room_version" }
Check out the Room release notes for more info.
With the 1.0 release of KSP you will start to see improved build times for your Kotlin projects as you migrate away from libraries based on KAPT. We have also updated a number of Android specific libraries which are ready for you to try today and offer significant performance improvements.