Skip to main content

Command Palette

Search for a command to run...

K2 Compiler Chronicles: Jetpack Compose

A quick start guide to Jetpack Compose with the K2 compiler.

Updated
6 min read
K2 Compiler Chronicles: Jetpack Compose
I

Passionate software engineer skilled in Kotlin, Android, and Ruby, with a focus on creating robust and scalable applications. Proficient in full-stack development, I excel in architecting, developing, and deploying complex software systems that deliver seamless user experiences. With expertise in Kotlin, I have successfully developed and maintained Android applications, leveraging modern frameworks and libraries to ensure high-quality software solutions. Additionally, my proficiency in Ruby enables me to build efficient web applications and contribute to the development of dynamic and interactive websites.

Introduction

The K2 Compiler is a new compiler for the Kotlin programming language. It is currently in alpha (beta on the JVM) but is expected to be released in the stable version of Kotlin 2.0. The K2 Compiler brings significant performance improvements, a more flexible architecture, and support for new language features.

Key Features of the K2 Compiler

Here are some of the key features of the K2 Compiler:

  • Performance improvements: The K2 Compiler is up to two times faster than the prior compiler. This is due to a combination of causes, including a new frontend that employs a more efficient parsing method, a new backend that employs an intermediate representation(IR), and several improvements implemented throughout the compiler.

  • Flexible architecture: The architecture of the K2 Compiler is more versatile than that of the preceding compiler. This simplifies the addition of new language features and the extension of the compiler with custom plugins.

  • Support for new language features: The K2 Compiler supports all of the current Kotlin features, including coroutines, inline functions, and sealed classes. It also supports several upcoming new features, such as type inference for generic functions.

Remember ...

The K2 Compiler is still in development, but it is already a major advance over the prior compiler. If you want to give it a try, you can find more information on the Kotlin website.

Release the Kraken ...

The K2 Compiler has been developed with making build times faster, especially for frontend applications; these include Android, iOS, Desktop, and Web/Wasm clients. The compiler is claimed to be almost 2 times faster than the original compiler; this article will attempt to discover how true these claims are.

We will measure the compilation time by looking at the amount of time it will take to build a Compose application that runs on Android and Desktop. The choice of these two targets is very intentional, bearing that beta support for K2 Compiler is only available for JVM targets. (Android and Desktop clients in Kotlin Multiplatform are built on top of the JVM).

Getting Started With the K2 Compiler

In order to try the K2 Compiler, you will need to perform some refactoring to your current app. To try the K2 Compiler you will need the following:

  1. Migrate to Kotlin 1.9.0 - Ensure your IDE has Kotlin 1.9 downloaded and installed. Set your Kotlin version in the gradle.properties file to 1.9.0. Make sure you change in all places where you are referencing the Kotlin version.

  2. (Optional) Update relevant dependencies - If you have certain dependencies that are tightly coupled to specific versions of Kotlin, you may need to update their versions as well. A good example is Compose Multiplatform v1.4.0 not being compatible with Kotlin 1.9.0. (Heads up! This may require a few minutes or hours of playing around with Gradle)

  3. Update Gradle Properties - Lastly, you need to add the K2 compiler experimental property in your gradle.properties file: kotlin.experimental.tryK2=true

Hurray! You are now ready to try the K2 Compiler.

Test Bench

Our experiment was run on a machine with the following specs:

  • CPU - Intel® Core™ i5-1240P (12c/16t) @ 3.30GHz

  • RAM - 16GB LPDDR5 @ 5200MT/s (Dual Channel)

  • OS - Ubuntu 22.04.3 Jammy

  • IDE - IntelliJ IDEA 2023.2

Remember, the absolute values of the benchmark results may value depending on your computer specifications.

Test App

We will run our tests on a calculator application that has been built using Jetpack Compose through the Compose Multiplatform library. This allowed us to have fully shared base logic and UI data for both platforms. The sample project can be found in our Kotlin multiplatform samples repository: https://github.com/rutubishi/kotlin-multiplatform-samples/tree/main/CalculatorKMP.

We are currently accepting contributions to the repository. Feel free and fix our calculator logic bug.

Test Params

This benchmark test will be run with the following in consideration:

  1. Cold IDE boot - This is the time taken to run the initial build after the IDE is first launched for the first time on the machine.

  2. Build Artifact - This is the time taken to run a build for the JVM artifacts (our Desktop App). For some reason, I could not get the regular IDE build to work on button click; currently looking for a fix for this.

  3. Build and Run Android App - Time taken to build and run the Android target.

  4. Build and Run Desktop App - Time taken to build and run the Desktop target.

Feel free to try different other types of builds that you can try and benchmark.

Test Results

The tests were run three times and the average time was recorded in the table below:

Old Compiler (seconds)K2 Compiler (seconds)% change
IDE Boot38.9437.43.95480226
Artifact Build11.267.1836.23445826
Android Build & Run10.354.3757.77777778
Desktop Build & Run3.251.7147.38461538
Average36.33791342

On the surface, the new compiler has some significant build times improvement, however, it is not yet at the doubled time improvements; at best it is 1.5 times faster.

It should be noted that the initial IDE build has an almost insignificant change in build time, which could be due to other constraints involving the IDE itself. With that in mind, we can exclude this build and focus only on build times that are not constrained by the editor itself. This now results in a significant shift in compilation time, making it almost 1.95 times faster, which aligns more closely with the initial claims.

Caution!

The K2 Compiler is still under development, but it is already showing promise as a significant improvement over the previous compiler. If you are interested in trying it out, you can find more information on the Kotlin website. The new compiler should not be used for production builds yet.

Conclusion

If you are a Kotlin developer who is looking for a faster, more flexible, and feature-rich compiler, then the K2 compiler is a great option. You can learn more about the K2 compiler and its benefits on the Kotlin website.

Here are some additional thoughts on the benefits of the K2 compiler:

  • The K2 compiler's performance improvements can make a big difference in the speed of Kotlin applications. This is especially important for applications that are performance-critical, such as games and real-time systems.

  • The K2 compiler's flexible architecture makes it easier for Kotlin developers to use the latest language features. This can help developers to write more expressive and efficient code.

  • The K2 compiler's support for new language features makes it a valuable tool for Kotlin developers who want to stay up-to-date with the latest developments in the language.

Overall, the K2 compiler is a significant improvement over the previous Kotlin compiler. It offers a number of benefits that can make it a valuable tool for Kotlin developers.

Here are some additional resources that you may find helpful:

TRY K2 TODAY !!!