Migrate To Kotlin Symbol Processing(KSP)
Hello everyone, today we are going to talk about a very popular topic for Android Development recently: Kotlin Symbol Processing(KSP).
Before we started, we need to first look at what is the KAPT and why do we need a new tool instead of KAPT(the Kotlin Annotation Processing Tool)
KAPT
Kotlin Annotation Processing Tool is a tool that combines JVM (Java Virtual Machine) and the Kotlin programming language. It is an Annotation Processor based on Java. It is designed to process Annotations used in Kotlin projects and perform operations related to these annotations. It is a tool that runs on JVM.
What is the JVM?
It is a virtual machine used to run programs written in Java language. Kotlin can run on JVM because it is a JVM supported language.
KAPT allows us to use Kotlin code with Java Annotation Processors ‘even if those processors don’t have specific support for Kotlin’. This is accomplished by automatically Generating Java Stubs from Kotlin code. These Stubs are generated so that Java Annotation Processors can read Kotlin code.
How does KAPT work?
First of all, it converts Kotlin Code into Java Stubs and Java Annotation Proccers works on these converted stubs. This process of generating Java code from Kotlin is an expensive process and has a significant impact on build time, especially in large projects.
What are Java Stubs?
When using some libraries such as Dagger-Hilt, we mark the code with annotations to facilitate certain operations, and the library we use automatically generates code by following these annotations. While Kotlin code interacts with Java classes, Java Stubs are generated so that Kotlin code can run smoothly with Java. These Stubs are the automatic generation of classes that can be used by Kotlin code, thanks to the Annotations in the code.
As I mentioned above, this process is expensive and directly affects the build time, especially on large-scale projects.
This is where Kotlin Symbol Processing (KSP) comes into play.
KSP
KSP; It is a new tool that provides direct processing of Kotlin code without converting Kotlin code into Java Stubs, thus accelerating the Annotation Processing process and increasing its efficiency. KSP is designed not to be tied to the JVM so that it can be adapted to other platforms more easily in the future.
It provides a Kotlin-specific API to handle Kotlin code or symbols. KSP; Since Java Stubs does not generate, automatically generated classes are processed faster and thus are much more performant.
In this way, KSP can run up to 2 times faster.
KSP is a very new tool therefore, it is not supported in all libraries that we use for Android Development. But now, two core libraries of Android Development “Dagger-Hilt” and “Room” are supported by KSP. However;
To use KSP, you need to use at the least Kotlin version 1.9.0 or above, for Dagger-Hilt; version 2.48 or above, for Room; version 2.5.0 or above.
On the other hand, unfortunately, Data Binding will be deprived of this performance improvement as it is not supported by KSP in its current state. If you use DataBinding extensively in your project, you should think twice before doing this update.
In the future whenever you want to add a new library in your Project, check if there is KSP support to be able to get faster build.
If you have any feedback about this, please feel free to message me on LinkedIn
Resources: