Mobile App Development

Try the K2 compiler in your Android projects

Posted by Márton Braun, Developer Relations Engineer

The Kotlin compiler is being rewritten for Kotlin 2.0. The new compiler implementation, named K2, brings significant build speed improvements, compiling Kotlin code up to twice as fast as the original compiler. It also has a more flexible architecture that will allow the introduction of new language features after version 2.0.

Try the new compiler

With Kotlin 1.9, K2 is now available in beta for JVM targets including Android projects. To help stabilize the new compiler and ensure that you are ready for Kotlin 2.0, we encourage you to try to compile your projects with the new compiler. If you encounter any problems, you can report them on The Kotlin issue tracker.

To try out the new compiler, update to Kotlin 1.9 and add the following to your project’s file:


Note that the new compiler should not yet be used for production builds. A good approach to try it out from the start is to create a separate branch in your project for compiling with K2. You can find an example of this in the Now in Android repository.

Tooling Support

Plugins and tools that depend on the Kotlin compiler interface will also need to be updated to add K2 support. Some tools already have experimental support for building with K2: Jetpack Compose compiler plugin supports K2 from version 1.5.0Which one is compatible with Kotlin 1.9.

Android Lint also supports K2 from version 8.2.0-alpha12. To run Lint on K2, upgrade to that version and add android.lint.useK2Uast=true to your deposit. Note that any custom lint rules that rely on the old interface APIs will need to be updated to use the Analytics API instead.

Adding K2 support in other tools is still in progress: KSP and KAPT tasks are currently reverting to using the old compiler when building your project with K2. However, compilation tasks can still run using K2 when these tools are used.

Android Studio also relies on the Kotlin compiler for code analysis. Until Android Studio supports K2, building with K2 may cause discrepancies between IDE code parsing and command line builds in some extreme cases.

If you use additional compiler plugins, check their documentation to see if they are already compatible with K2.

Get started today with the K2 Compiler

The Kotlin 2.0 compiler offers significant enhancements to help you push updates faster, be more productive, and spend more time focusing on what makes your app unique.

It already works with Jetpack Compose and we have a roadmap to improve support for other tools including Android Studio, KSP and compiler plugins. This is the perfect time to try it out in your application’s codebase and provide feedback on Kotlin, ComposeOr Plush.

Source link

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button