Kotlin 2.0 — Android project migration guide

Kacper Wojciechowski
5 min readMay 12, 2024

--

Photo by Mimi Thian on Unsplash

Kotlin has gone through quite a journey since it’s first release. Now it is reaching a new milestone—a 2.0 release. As of the writing of this article, version 2.0.0-RC3 has been released, which is almost a final version of 2.0. I was a part of the Kotlin EAP, and I want to give you a quick rundown of what I’ve had to change in the setup of my private Android project to adapt Kotlin 2.0.

K2 compiler

This is probably the biggest feature of the new Kotlin version. The JetBrains team promises 2x faster compilation time. The reality is that it is a handy buzzword, as this “2x” probably just applies to the sum of compilation tasks. In real world, Gradle executes some tasks in parallel, which nullifies some of those gains. In my experience, it comes down to about 15–30% improvement.

The JetBrains team states that it will greatly improve the experience of IntelliJ IDEA-based IDEs with the K2 mode. IDE is compiling the project as-you-write for spellchecking, code autofill, etc. They promise the writing experience will get even smoother with the new K2 mode, but as of now, it is in Alpha, and it is a buggy mess.

How to migrate

Fun fact: the K2 compiler was available in an experimental version in previous Kotlin versions with the gradle.properties flag! As of Kotlin 2.0, the K2 compiler is the default compiler, so you don’t need to do anything. Just be aware that it gets enabled when you bump the Kotlin version to 2.0.

.kotlin — a new build directory

Kotlin 2.0 introduces a new build output directory — .kotlin. Since Kotlin 1.8.20, the Kotlin compiler has started to store some compilation data in the Gradle directories, but those directories are reserved for Gradle data. In Kotlin 2.0, the team decided to migrate to a new directory reserved for Kotlin compilation data — .kotlin.

How to migrate

Since this is a new build directory, you need to add it to the .gitignore file for its content to not appear in the commits:

# Kotlin 2.0
.kotlin/

If, for some reason, this directory does not suit your project, you can change it with kotlin.project.persistent.dir gradle property.

kotlinOptions call deprecated

Previously, to setup the JDK version or to add some compiler arguments, we needed to setup all of the KotlinCompile tasks to access the kotlinOptions object that has such functionality. Since Kotlin 2.0, this approach has been deprecated, and there is a new API to perform such setups.

How to migrate

Let me introduce to you compilerOptions:

And here is a version for Gradle convention plugins:

Note: JvmTarget is a new class, different and incompatible with the JavaVersion class. I’ve filed a YouTrack issue to improve its compatibility, as we still need the JavaVersion class for Android target setup.

Compose compiler version

Before 2.0, we needed to supply the Kotlin Compose compiler version manually:

It was quite annoying to maintain, as it is required to be compatible with the current Kotlin version. It is different from the Compose version and was not proposed by the IDE to bump. Every time you’ve bumped the Kotlin version, you’ve had to Google the compatible Compose compiler version. Kotlin 2.0 fixes this struggle.

How to migrate

Since Compose compiler version is strongly coupled with the Kotlin version, why not get the Compose compiler version alongside the Kotlin version and not think about it anymore. It seems like Google and JetBrains came to this conclusion when working on Kotlin 2.0 and moved its repository to the JetBrains repository. This way, JetBrains can easily deliver the Compose compiler alongside the Kotlin version! Let me introduce you to the new Gradle plugin that will set everything up for you:

[plugins]
kotlin-compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }

Apply the plugin in Compose Modules:

And thats all! This plugin will setup the Compose compiler version for you based on your current Kotlin version. It also introduces a new setup block for Compose stuff:

And here’s the version for Gradle convention plugins:

[libraries]
# Implement it in the gradle convention module to access the ComposeCompilerGradlePluginExtension class
plgn-kotlin-compose-compiler = { module = "org.jetbrains.kotlin:compose-compiler-gradle-plugin", version.ref = "kotlin" }

Conclusion

That’s all, as of now, that I’ve found has changed in the project setup. It feels like not much has changed, but most changes happened under the hood. Let’s see what kind of features the Kotlin language developers will provide us with in future versions (the explicit backing fields are already experimental🤞).

If you also like to check out cutting-edge new stuff and you’ve noticed more changes in your Android project setup, feel free to comment. I will try to update the article if new stuff appears to be worth mentioning here.

--

--