Android Development Basics: How to add Kotlin to an existing Java Android project

Android Development Basics: How to add Kotlin to an existing Java Android project

Kotlin is pretty great. It’s got a whole bunch of useful language features, and the syntax is generally clean and simple. It’s also cross-platform, and the base language can compile to a whole bunch of different languages and platforms.

Kotlin for Java (called KotlinJVM) compiles to the same thing Java compiles to — JVM bytecode for pure Java, and whatever Android is using these days for its version of Java. This means code written in KotlinJVM has access to all existing Java and Android APIs, along with any Java classes, methods, and fields that are already in your app.

That compatibility works the other way around, too. Meaning, you can access Kotlin APIs from Java. The code might end up looking a little messy if you try to use more advanced features, but it’s possible.

This tutorial is going to go over how you can add Kotlin to an existing Android project that’s made in Java. It also assumes you’re using Android Studio. Before we get started though, you may want to familiarize yourself a bit on how Kotlin works, if you haven’t done so already. JetBrains, the company behind the language, has a handy FAQ for this.

Dependencies

Kotlin is technically a library. It’s a fancy library, with a lot of features and an accompanying IntelliJ/Android Studio plugin, but it’s a library. So to add it, you’ll need to add some dependencies.

In your project level build.gradle, add the Kotlin dependency.

buildscript {
    ...
    dependencies {
        ...
        //1.5.10 is currently the latest stable version of Kotlin.
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.10"
    }
    ...
}

In your module level build.gradle, apply the Kotlin Android plugin and add the dependencies.

...
//This should be near the top of the file, underneath the "android" plugin.
apply plugin: 'kotlin-android'

...

dependencies {
    ...
    //1.5.0 is currently the latest stable version of AndroidX Core for Kotlin.
    //If you already have "androidx.core:core" implemented, remove it.
    implementation 'androidx.core:core-ktx:1.5.0'
    implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.5.10'
}

And that’s it for implementing Kotlin. The latest version of Android Studio already comes bundled with the IDE plugin.

Basic Usage

Now the integration is done, you can start actually using Kotlin. To make a new class, just right-click on the package where you want the file to be created, click New, and click Kotlin Class/File.

Image showing how to add a new Kotlin file/class in Android Studio

Once you click that, you’ll be shown a dialog asking for the name of the file, along with what type of object it should be (Class, Interface, Object, plain file, etc). This is pretty similar to creating a new Java Class.

An image showing the New Kotlin Class/File dialog

Choose what you want, and the file will be created. Now you can get started programming in Kotlin.

Conclusion

Adding Kotlin to an existing Java Android project is easy. Just include a few Gradle dependencies, apply a plugin, and you can start programming in the language.

For more advanced usage, including how to automatically convert Java code to Kotlin, check out Google’s official documentation.

About author

Zachary Wander
Zachary Wander

Started out rooting and installing custom ROMs before moving onto modifying Android apps in Smali and subsequently developing various customization and utility apps for Android, such as SystemUI Tuner. Check me out on Twitter: https://twitter.com/wander1236.