Illustration by Kiran Puri

How to write Java-friendly Kotlin code

Nicole Borrelli
Android Developers
Published in
2 min readOct 10, 2019

--

TL:DR: Learn how to write Kotlin code that works idiomatically from both Kotlin and the Java Programming Language by following our new Java-Friendly Kotlin codelab.

Since we announced adding Kotlin as an officially-supported language on Android, we’ve recommended a staged approach when adopting Kotlin. At the beginning, this means writing code such as tests and new features in Kotlin, which typically means calling from Java code into Kotlin.

As new libraries are added, or as existing libraries are converted to Kotlin, it becomes more common for Java code to call into Kotlin.

The Java-Friendly Kotlin codelab presents a sample app where we’ve refactored a library from Java to Kotlin. The library includes a singleton Repository, a data class, and Kotlin extension methods. We then explain how we can update our newly-created Kotlin code so we don’t have to perform any refactoring of the existing Java code. We looked at how and when to use Kotlin features such as the const keyword, and annotations such as @JvmName, @JvmStatic, and @JvmOverrides.

Get started with the Java-Friendly Kotlin codelab, and gain confidence writing Kotlin code that can be used idiomatically from both Kotlin and the Java Programming Language.

Did you start converting your code to Kotlin? What problems did you have when calling Kotlin code from Java code? Let us know in the comments below!

--

--