Image for post
Image for post
Illustration by Kiran Puri

How to write Java-friendly Kotlin code

Nicole Borrelli
Oct 10, 2019 · 2 min read

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!

Android Developers

The official Android Developers publication on Medium

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store