How to Setup Android Studio for Kotlin Development

If you’ve been following the latest trends in the AndroidDev world, you’ve probably heard of Kotlin.

Kotlin is a (not so) new programming language developed by JetBrains, the creators of IntelliJ IDE on which Android Studio is based. Kotlin is statically typed, runs on the JVM, and offers many advantages. There are tons of items who talk about the advantages of Kotlin over Java, so I won’t cover that in this post.

In this article, however, we’ll learn how to set up Android Studio for Kotlin development and write our “Hello World” app in Kotlin.

Let’s start!

Install the Kotlin Plugin for Android Studio

The good folks at JetBrains have created an IntelliJ/Android Studio plugin for Kotlin. First, we’ll go ahead and install the plugin.

To do this, go to Preferences > plugins > Browse repositoriessearch for Kotlin, then click To installas shown in the figure below.

Once the installation is complete, you will need to restart Android Studio to apply the new plugin.

Just in case you’re using IntelliJ IDEA 15 or higher, you’re in luck. The Koltin plugin comes with the IDE.

Create a new Android project

Now that the plugin is installed, let’s go ahead and create a new Android project as usual. Move towards To file > New > New project and follow the project creation wizard. Select the Create an empty activity choice at the end.

For more information on creating an Android project, see this guide.

Apply the Kotlin plugin to the project

The next step is to apply the Kotlin plugin to the project. There is an automated tool to do this, but sometimes the tool messes things up, so let’s walk through the manual process of applying the plugin in our build.gradle files (both at the project level and at the application module level).

Add the Kotlin Gradle Plugin

To configure the plugin, we first need to add the plugin to the root project build.gradle, in the same way as for the Gradle plugin (automatically). Add the plug-in to dependencies closing in the project build.gradle to file. The project build.gradle the file looks like this:

buildscript {
    ext.kotlin_version = "1.0.6" // replace with the latest (stable) version: https://github.com/JetBrains/kotlin/releases/latest

    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

A few notes on the code block above:

  • We have created a variable ext.kotlin_version. The reason we have this is that we may have other places in the project where we need to refer to the Kotlin version, so it makes sense to “externalize” this value. You can view this post at externalize your dependency versions using additional gradle properties for more information.
  • We placed this variable in the buildscript closing. It is because the buildscript is the entry point into this file when building the project. If we place the variable outside of this closure, this variable will not be available until the project is built and the build will fail.

Apply Android Kotlin Plugin

After adding the Kotlin Gradle plugin, the next step is to apply the plugin. To do this we need to add apply plugin: kotlin-android to the application module build.gradle to file:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android' // apply kotlin android plugin

Convert activity code from Java to Kotlin

We’ve applied the plugin and configured everything we need, but our code generated by the “empty activity” is still in Java.

Luckily for us, the Kotlin plugin can help us convert our code from Java to Kotlin.

Currently, our Java activity looks like this:

To do this, select the file and navigate to Coded > Convert Java file to Kotlin fileor use the shortcut Order + Other + Change + K (I believe you can replace Order with Control if you are on PC).

After converting our code to Kotlin, our “empty activity” looks like this:

Bonus: Stay up to date with Kotlin releases

You’ve come this far – let’s add a bonus tip!

Kotlin is still a work in progress, and there are (frequent) patches and updates. It’s generally a good idea to stay up to date with new features and language changes. The Kotlin Android Studio plugin helps us manage Kotlin versions. If you navigate to Tools > Kotlin > Configure Kotlin Plugin Updates as shown in the screenshots below, you will be able to select the update channel — Stable, Early Access Preview - 1.0.x, Where Early Access Preview - 1.1 – which is currently the state-of-the-art version.

Further reading

There are a number of great resources scattered around the internet for learning Kotlin. Some of them are:

In future articles I will write about some specific features of the language.

Thanks for reading this post. I am always open to questions and comments. If you have any, please drop them in the comments section below or send me a Tweeter.

If you found this article useful, or know someone who might find it useful, please share it. Thank you!

Comments are closed.