Moshi & Room KAPT -> KSP Migration.

Manish Singh
4 min readNov 26, 2022

--

KAPT vs KSP

Kotlin Symbol Processing (KSP) is an API that you can use to develop lightweight compiler plugins. KSP provides a simplified compiler plugin API that leverages the power of Kotlin while keeping the learning curve at a minimum. Compared to kapt, annotation processors that use KSP can run up to 2 times faster.

Why KSP?

Compiler plugins are powerful metaprogramming tools that can greatly enhance how you write code. Compiler plugins call compilers directly as libraries to analyze and edit input programs. These plugins can also generate output for various uses. For example, they can generate boilerplate code, and they can even generate full implementations for specially-marked program elements, such as Parcelable. Plugins have a variety of other uses and can even be used to implement and fine-tune features that are not provided directly in a language.

KSP is designed to hide compiler changes, minimizing maintenance efforts for processors that use it. KSP is designed not to be tied to the JVM so that it can be adapted to other platforms more easily in the future. KSP is also designed to minimize build times. For some processors, such as Glide, KSP reduces full compilation times by up to 25% when compared to kapt.

Comparison to reflection

KSP’s API looks similar to kotlin.reflect. The major difference between them is that type references in KSP need to be resolved explicitly. This is one of the reasons why the interfaces are not shared.

Comparison to kapt

kept is a remarkable solution which makes a large amount of Java annotation processors work for Kotlin programs out-of-box. The major advantages of KSP over kapt are improved build performance, not tied to JVM, a more idiomatic Kotlin API, and the ability to understand Kotlin-only symbols.

To run Java annotation processors unmodified, kapt compiles Kotlin code into Java stubs that retain information that Java annotation processors care about. To create these stubs, kapt needs to resolve all symbols in the Kotlin program. The stub generation costs roughly 1/3 of a full kotlinc analysis and the same order of kotlinc code-generation. For many annotation processors, this is much longer than the time spent in the processors themselves. For example, Glide looks at a very limited number of classes with a predefined annotation, and its code generation is fairly quick. Almost all of the build overhead resides in the stub generation phase. Switching to KSP would immediately reduce the time spent in the compiler by 25%.

Limitations

While KSP tries to be a simple solution for most common use cases, it has made several trade-offs compared to other plugin solutions. The following are not goals of KSP:

  • Examining expression-level information of source code.
  • Modifying source code.
  • 100% compatibility with the Java Annotation Processing API.

We are also exploring several additional features. These features are currently unavailable:

  • IDE integration: Currently IDEs know nothing about the generated code.

Finding in the complex project

KSP DATA

KSP Benchmark

* Running warm-up build #1
Execution time 246156 ms
* Running warm-up build #2
Execution time 75455 ms
* Running measured build #1
Execution time 71653 ms
* Running measured build #2
Execution time 72894 ms
* Running measured build #3
Execution time 72602 ms

KAPT DATA

KAPT Benchmark

* Running warm-up build #1
Execution time 294587 ms
* Running warm-up build #2
Execution time 79696 ms
* Running measured build #1
Execution time 70531 ms
* Running measured build #2
Execution time 80581 ms
* Running measured build #3
Execution time 75901 ms

How to migrate Moshi KAPT -> KSP

The first thing is to update your Moshi and Kotlin to the latest version.

Add this dependency to your root-level Gradle file.

Add this plugin to your app module or where do you want to use it.

plugins {
id("com.google.devtools.ksp")
}

Add change kapt to ksp

🥳 Congratulation you migrated your Moshi to KSP :)

How to migrate ROOM KAPT -> KSP

The first thing is to update your Room and Kotlin to the latest version.

Add this dependency to your root-level Gradle file.

Add this plugin to your app module or where do you want to use it.

plugins {
id("com.google.devtools.ksp")
}

In-app level Gradle file

🥳 Congratulation you migrated your Room to KSP :)

Thank you folks for reading till end :)

“KEEP LEARNING,
KEEP SHARING”

📱HAPPY CODING ….

--

--

Manish Singh
Manish Singh

Written by Manish Singh

I am passionated Android developer. Helping other with my skill give me fulfillment in myself.

No responses yet