Author / Software Engineer Ting yuan Huang and product manager David winer
We are pleased to introduce youKotlin Symbol Processing(KSP), a new tool to help you build lightweight compiler plug-ins in kotlin. KSP provides functions similar toKAPTHowever, the speed is increased by 2 times, the kotlin compiler function can be accessed directly, and the multi platform compatibility is fully considered during development.
KSP is compatible with kotlin version 1.4.30 and later. You canKSP GitHub repoView open source and documentation in.
Kotlin developers give us the most feedback on the need to improve the construction speed. Many developers have to iterate and deploy applications dozens of times a day, so the slow construction speed will make developers have to spend a lot of time waiting. One of the biggest challenges in compiling kotlin code is that kotlin does not have a native annotation processing system. such asRoomAnnotation processors such as are ubiquitous on the Android platform, and they rely on Java annotation processing compatibility through the kotlin annotation processing tool (kapt). However, kapt may run slowly because it needs to generate intermediate Java stubs before the Java annotation processing system can extract them.
When designing KSP, we considered what form kotlin’s annotation processing should be if we build it from scratch. KSP provides a powerful and simple API, which can directly parse kotlin code, so it greatly reduces the construction speed burden caused by kapt generating stubs. In fact, the initial benchmark performed with the room library showed that KSP was about twice as fast as kapt.
To view the actual operation of KSP, please download it from GitHubKSP playground project。 You can get:
- library: a small test processor library that implements the builder pattern as a KSP processor
- Use item: shows how to use the workload directory of the builder processor in an actual kotlin project
All the logic to implement the builder is in the test processor – for the workload, the only difference between using kapt and using KSP is that two lines of build file have been changed:
This is the goal of KSP: most Android application developers don’t have to worry about their internal structure; In addition to this line change, KSP enabled libraries are similar to ordinary annotation processors, but the speed is twice as fast. That is to say the first mock exam of KAPT and KSP in the same module will reduce the speed of your building, so it is better to use KSP and KAPT in the separate modules during the Alpha version.
As more and more annotation processors adopt KSP, we hope that most of your modules can use KSP to directly replace kapt. Now you canformSee which annotation processors provide KSP support. If there is no library that has been supported or is implementing KSP support in the table, please feed back your valuable suggestions to us!
For library authors, KSP’s current alpha version is a good opportunity for in-depth research. Welcome toKSP problem trackerProvides us with feedback on the API. In addition, we will be in the official account of Google developers.Kotlin SlackRelease version dynamics regularly on the #ksp channel of. Since the release of the developer preview in June last year, we have solved more than 100 errors and problems, dozens of which have been fed back by the excellent kotlin library developer community. I hope you can continue to pass the questionKSP problem trackerGive us feedback or leave a message at the bottom of the article.
Java is a registered trademark of Oracle and / or its affiliates.