Author/Software Engineer Ting-Yuan Huang and Jiaxiang Chen
Kotlin Symbol Processing (KSP) -a new tool for building lightweight compiler plugins in Kotlin is now available in stable version! It has similar functions to the Kotlin annotation processing tool ( KAPT ), but the speed is increased by 2 times, and it can also directly access the Kotlin language structure and support multiple platform targets.
In the past few months, KSP has released a total of 32 versions, and more than 162 bugs and issues from the community feedback have been fixed. If you are looking forward to applying this tool, now is the time to try it.
Why build KSP
In the Android team, we often ask developers such a question: At this stage, what is the biggest pain point encountered when developing applications? One of the most frequent issues is build speed. Over the years, we have been steadily optimizing the Android build tool chain, and now we are very happy to be able to achieve these optimizations through KSP. KSP is a new generation tool that uses Kotlin for annotation processing: it will greatly increase the speed of Kotlin developers' construction, and unlike KAPT, this tool provides support for Kotlin/Native and Kotlin/JS.
Adding KSP support to Room not only improves the compilation speed, but also allows Room to better understand Kotlin code, such as the nullability of generics that cannot be achieved using KAPT. KSP also unlocks new possibilities such as generating Kotlin code, which will allow Room to have a better Kotlin user experience in the future.
- Android Software Engineer Yigit Boyar
Why is KSP faster?
Kotlin annotation processing tools ( KAPT ) combine with the Java annotation processing infrastructure to enable most Java language annotation processors to be used out of the box in Kotlin. To this end, KAPT compiles Kotlin code into Java stubs, which retain the information that the Java annotation processor cares about. But the cost of creating these stubs is high, which means that the compiler must parse all the symbols in the program multiple times (one time to generate the stub, another time to complete the actual compilation).
KSP does not use stubs to generate models, but runs as a Kotlin compiler plugin. It allows annotation processors to use Kotlin to read and analyze source programs and resources, so you don't need to rely on Java annotation processing infrastructure. This has greatly increased the build speed (for Room's Kotlin test application , the speed is increased by 2 times), and it also means that KSP can be used in non-Android and non-JVM environments (such as Kotlin/Native and Kotlin/JS) .
started using KSP
To start using KSP, you can download KSP Playground project from GitHub, from which you will learn how to use KSP as an annotation processor and use applications/libraries:
test-processorlibrary that implements the builder pattern as a KSP processor
- Use library: Shows how to use the
workloaddirectory of the builder processor in an actual Kotlin project
Use Moshi or Room
If you use Moshi or Room in your project, you only need to quickly repair the build file of the module to experience KSP. For example, to use the KSP version of Room in the Gradle module, you only need to replace the KAPT plugin with KSP and replace the KSP dependencies:
You can check the Room release notes for more information.
By using KSP version 1.0, you can find that the build speed of the Kotlin project has been improved, because you will no longer use KAPT-based libraries. At the same time, we have also updated some specific Android libraries, you can try it now and experience the powerful performance after improvement. You are also welcome to continue to follow the official WeChat public account to get the latest information at any time.
Welcome to click here to submit feedback to us, or share your favorite content, found problems. Your feedback is very important to us, thank you for your support!