Why Kotlin Symbol Processing (KSP) changes the game for developers

Why Kotlin Symbol Processing (KSP) changes the game for developers

ClearPoint’s Mobile Practice Lead David Rawson, gave stubs a worthy send-off at the Chicago Roboto conference in 2023, showcasing the benefits of Kotlin Symbol Processing (KSP), a new metaprogramming tool from Google that promises speed and ergonomics.

Kotlin has been knocking around since 2016 (version 1.0) and picked up speed when Google officially recommended it for Android development in 2019. It’s an easy sell – expressive and modern syntax with seamless Java interoperability. Seamless, that is, until you venture into meta-programming

 

What is Metaprogramming?

Metaprogramming allows one program to become an input for another program. For instance, instead of manually writing out code to parse some JSON response, you could annotate a class in your program with the names of the JSON fields and have an adapter generated automatically. 

Every piece of code written by hand requires maintenance and has the opportunity for error, and since metaprogramming eliminates that manual labour, it increases productivity. This kind of code generation has a strong history in Java with annotation processors written using the Javax APIs that run with Javac.

This is where Kotlin’s seamless interoperability with Java breaks down – javac only understands Java code. So to run annotation processors in Kotlin projects, a tool called “kapt” generates a no-op Java translation of your Kotlin code called a “stub.” Since stub generation requires the compiler to perform a large amount of symbol resolution, this is slow and wasteful. 

In some cases, a third of the time spent in the annotation processor is spent in this stub generation. This slowdown is experienced every time developers build the project locally and negatively impacts the developer experience.

 

What is Kotlin Symbol Processing (KSP)?

KSP is an API that allows you to build Kotlin annotation processors that do not require stub generation, leading to improvements in performance over legacy processors that run with kapt. Additionally, the processors we write with KSP are more maintainable. 

Instead of painstakingly translating Kotlin code into foreign javax.lang.model abstractions like Element, we model the Kotlin code idiomatically as class declarations, property declarations and so on. This makes it much easier to reason about how the annotation processor functions.

Furthermore, since KSP is not tied to the JVM and Kotlin supports compilation targets like JS, we end up with a metaprogramming solution that is multi-platform. Future applications of KSP will see the reuse of generated code across web, desktop, Android and iOS. This is an incredible opportunity for increases in productivity!

 

Why is KSP better?

  • Clean API that models Kotlin code intuitively, leading to annotation processors that are easier to maintain
  • Builds faster, allowing for a better developer experience
  • Decoupled from the JVM, promoting code reuse in multi-platform projects

 

ClearPoint are leaders in engineering, design and technology transformative solutions. Gain a competitive edge in a digital-first world with support from ClearPoint’s leading mobile engineering team. We pride ourselves on our product-ready knowledge of app architecture with MVVM and VIPER, server-driven UI, GraphQl with Apollo, reactive programming frameworks with RxJava and RxSwift, code generation using kapt and KSP, static analysis using linters, builds and deployment with CircleCI, Gradle, and Fastlane.  With a combination of technology and business knowledge expertise, we ensure your organisation can seamlessly transition into the digital age, empowering you to drive lasting business value. Talk to us today for your mobile app needs.

Empower your digital journey