Adapt Kotlin 2.0 in Android applications
Tags: kotlin, android, jetpackcompose, k2
Published: 2024-06-01 23:43:07

> [Read this post in Spanish here.](https://dev.to/marlonlom/adaptar-kotlin-20-en-aplicaciones-android-1kfa)

Recently, version 2.0 of Kotlin has been released, and with it, several adjustments to Android projects to adapt or implement code with this new version of the programming language officially supported by Google for Android mobile development (Multiplatform, according to the latest versions of the Google I/O event).

In this short article, I explain my experience migrating from version `1.9.23` to version `2.0.0`; This, due to the novelties that the use of this new version covers in projects that use Kotlin as a programming language, and also the possible difficulty for many of us developers to apply migrations from versions of Kotlin much lower than `1.9.0 `.


## About the K2 compiler

The K2 compiler is a complete reimplementation of the original Kotlin compiler, designed to offer significant improvements in Android application development. Introduced in Kotlin 2.0, K2 provides several advantages, among which are: faster compilation speed, performance improvements, and improved cross-platform support; These advantages applied to Android projects allow a reduction in the size of the applications by generating more compact code, as well as the generation of native code which implies greater performance in mobile applications.


### How to test K2 compiler on Android?

It is worth mentioning that this section covers the configuration aspect from the point of view of Android projects. I will not mention details of KMP, although several of the details indicated are easy to assimilate if one already has experience in cross-platform projects using Kotlin.


#### 1. A new build directory

Kotlin 2.0 introduces a new build output directory: `.kotlin`. you must add it to the `.gitignore` file so that its contents do not appear in commits:

```
#.gitignore
# Kotlin 2.0
.kotlin/
```


#### 2. Apply new version of kotlin

Now we go to the gradle/libs.versions.toml file, in this section, it is assumed that Version Catalogs are being used to control the dependencies of the mobile application (versions, plugins, etc.), The reference to the Kotlin version is located, and it is changed to version 2.0.0. So:

```
# gradle/libs.versions.toml
[versions]
agp="8.4.1"
kotlin="2.0.0"
ksp="2.0.0-1.0.21"
```

Regarding compatibility, the versions of the Kotlin symbol processor (KSP) libraries and the gradle plugin for Android (AGP, acronym used for reference) must also be updated. The previous snippet indicates the versions for review.

At the library level, the following Kotlin libraries are also updated:

```
# gradle/libs.versions.toml
[libraries]
kotlin-gradle-plugin = "org.jetbrains.kotlin:kotlin-gradle-plugin:2.0.0"
kotlin-serialization-plugin = "org.jetbrains.kotlin:kotlin-serialization:2.0.0"
```


> Note: there are libraries that, at the time of writing this article, did not present problems in terms of changing versions, such as `kotlin-coroutines`, `kotlin-serialization`, `kotlin-serialization-json`.

At the plugin level, the Kotlin libraries are also updated below, taking into account the version reference already indicated previously:

```
# gradle/libs.versions.toml
[plugins]
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
```

With this done, run the command `gradle clean build` or from Android Studio, build the project.


#### 3. Update the Compose compiler

Another important aspect regarding the configuration of Android projects, especially using Jetpack Compose, is related to the Compose compiler for Kotlin, which is a configuration that allows the transformation of functions annotated as `@Composable` in order to apply optimizations In the compilation of these functions, now, the way of defining the version of the compose compiler has presented a big change, which is described below.

Previously, the version of the compose compiler had to be placed inside the build.gradle file of the gradle project module that has the Android implementations and that also contains the Jetpack Compose implementations.

```
/* build.gradle.kts */
…
composeOptions {
 kotlinCompilerExtensionVersion="1.5.3"
}
…
```


And if you use Version Catalogs, the version would be indicated as follows:

```
# gradle/libs.versions.toml
[versions]
compose-compiler = “1.5.3”
…
```

```
/* build.gradle.kts */
…
composeOptions {
 kotlinCompilerExtensionVersion = libs.versions.composeCompiler.get()
}
…
```

This initiative was good at the time, but it has some maintenance problems, since it is required to be compatible with the current version of Kotlin. It is different from the Compose version and the IDE did not propose to improve it. Every time you update your Kotlin version, you have to Google the Jetpack Compose documentation for the compatible version of the Compose compiler. Kotlin 2.0 solves this problem.

Now, the inclusion of a new gradle plugin is applied to the `gradle/libs.versions.toml` file, which is responsible for managing the more organized version of the compose compiler and linked to the Kotlin version.

```
# gradle/libs.versions.toml
[plugins]
…
kotlin-compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
```

After this action, the `build.gradle.kts` file of the Android module that has the Jetpack Compose capabilities is modified, removing the `composeOptions configuration. kotlinCompilerExtensionVersion ` and adding the reference to the `kotlin-compose-compiler` plugin in the `plugins { .. }` section.

```
/* build.gradle.kts */
…
plugins {
 …
 alias(libs.plugins.compose.compiler)
}
…
```

With this done, run the `gradle clean build` command or from Android Studio, build the project or synchronize.

And that's it! This plugin will configure the Compose compiler version based on the current Kotlin version.

## Conclusions

Overall, the K2 compiler represents a significant step forward for Android app development with Kotlin. Improvements in speed, performance, and extensibility make it a valuable tool for Android developers.

Now that the use of Kotlin 2.0.0 is becoming standard, now is a good time to update our Android applications so that they support more of the technology that goes and evolves in terms of Kotlin, KMP, Jetpack Compose, and the other technologies that Google and app developers and mobile libraries are adapting for more modern mobile apps with better features.

I hope you find this informative and useful and that at some point you can follow these steps in your Android applications, in order to apply a good migration to Kotlin 2.0.0.

Thanks for reading, happy coding! 😊