Join us at TechEx 2024!

From June 5-6 we'll be at the event series for the tech ecosystem.

Learn more
Back to Insights

June 21, 2023

What’s new in Kotlin for Android?

Check out what is new in Kotlin, a widely used programming language for Android.

Kotlin for Android

Kotlin has become one of the most popular programming languages for Android development in recent years. It is now widely used by developers across the globe to create high-quality Android apps. Kotlin offers several advantages, such as better code quality, enhanced performance, and improved developer productivity. Today, we will explore the latest features and updates in Kotlin for Android development, which is utilized by our Mobile Studio.

The Kotlin 2.0 compiler

Also known as the codename “K2”, the Kotlin 2.0 compiler is a drop-in replacement for most code, so the developer using it doesn’t have to change their Kotlin source. The new architecture will be faster and have better build performance, and it is said to be twice as fast as the current Kotlin compiler.

The Kotlin 2.0 compiler is expected to have a stable release in 2024, but it’s currently able to be tried out as soon as possible to verify it works correctly.

You can test it out by the setting the language version to 2.0, as shown below:

// build.gradle.kts (module) 
kotlin { 
    sourceSets.all { 
        languageSettings { 
            languageVersion = "2.0" 
        } 
    } 
} 

Kotlin DSL for Gradle builds

Android has primarily used Kotlin for years, due to higher productivity and more stable apps for developers. Even with a Kotlin preference, the default language to define builds has been Groovy, even with a Kotlin Gradle option.

Android has recently announced that they will be switching to Kotlin for the default languages for build scripts. This is a beneficial change for Android developers, since there are a lot of benefits to having Kotlin as the default language. Kotlin builds scripts by using statically typed values, so code hinting is more precise and helpful. Syntax errors are also more accurate and displayed while editing Kotlin build scripts instead of when the developer is trying to sync the project. Kotlin DSL also includes type and method documentation. There is also the option to migrate your build to version catalogs, if you’re looking to add and maintain your dependencies and plugins in a scalable way. If you’re looking for resources on how to make the changes, here are some guides from Android Studio:

Kotlin symbol processing

Code generation initially began with the Kotlin Annotation Processing Tool (KAPT), which generates Java stubs for Kotlin code and allows annotation processors written for Java to work with Kotlin. This stub generation has been a time consuming process, and over has had a significant impact on the build speed of projects that use annotation processing.

However, KSP is a Kotlin-first alternative to KAPT, which analyzes Kotlin code and has shown to have several advantages such as:

  • Up to two times the speed. KSP requires no stub generation.
  • Better typing. KSP understands Kotlin specific languages such as nullability.
  • Multiplatform-ready. Doesn’t depend on Java source code.

Migration is recommended to be done as soon as possible. Here is how to set it up:

  • Add the plugin
// build.gradle.kts (root) 
plugins { 
    id("com.google.devtools.ksp") version "1.8.10-1.0.9" apply false 
}
  • Change KAPT for KSP whenever possible. (note: some libraries might not support KSP yet.)
// build.gradle.kts (module)
plugins {
  id("com.google.devtools.ksp")
}

dependencies {
  kapt("androidx.room:room-compiler:2.5.0")
  ksp("androidx.room:room-compiler:2.5.0")

  kapt("com.squareup.moshi:moshi-kotlin-codegen:1.14.0")
}

For more information, check out the official libraries for KSP support:

Kotlin Symbol Processing API 

Kotlin Multiplatform (Beta) 

Kotlin Multiplatform is made to simplify the development of cross-platform projects by sharing Kotlin code across different platforms. It reduces the overall time spent writing and maintaining the code for different platforms, while maintaining the benefits of native programming.

Kotlin Multiplatform is currently in Beta, and Google is currently experimenting with code sharing in Kotlin across Android, iOS and web while utilizing Google Suite applications.

With the benefits of native programming while working on different platforms, Kotlin provides the following code sharing mechanics:

  • Share common code among all the platforms that are being used in your project
  • Share code among only some of the platforms included in your project in order to reuse the code in similar platforms
  • If platform specific APIS need to be accessed from the shared code, you can use the Kotlin mechanism of expected and actual declarations.

Some Jetpack libraries are bringing support for  Kotlin Multiplatform, to learn more about the Jetpack libraries, click here.

Conclusion

Kotlin has introduced several new features that will continue to make it a great language for Android development. Kotlin is continuing to make it easier for developers to write clean and efficient code, and making it more efficient to migrate their existing code to Kotlin.

Overall, Kotlin is a powerful language, and as Google continues to support Kotlin and add new features to the language, we can expect even more exciting features in the future. To learn more about what our Mobile Studio is doing in Android development, check out our Mobile Studio site at App Solutions Studio.

andres de la grana

By Andres De La Grana

Mobile Developer

Andres de la Grana is a Mobile Developer at Qubika. With 5+ years of experience, Andres is passionate about Android development. He has worked on various scalable applications with lots of users, and believes in creating clean code and following best practices.

News and things that inspire us

Receive regular updates about our latest work

Let’s work together

Get in touch with our experts to review your idea or product, and discuss options for the best approach

Get in touch