How to use Kotlin in Android Studio

Ajay
4 min readMay 18, 2017

--

Today at Google IO keynote, Google has announced Kotlin an official language on Android. Kotlin, is a statically typed programming language for the JVM, Android and the browser. Its primary development is from a team of JetBrains programmers.

Why Kotlin ?

Why did the Android team decide to support Kotlin? Most importantly, it was because we think Kotlin is a great language that will make writing Android apps easier and more enjoyable.
Kotlin is also a great match for the existing Android ecosystem. It is 100% compatible with the Java programming language. You can add as little or as much Kotlin into your existing codebase as you want and mix the two languages freely within the same project. Calling out to Kotlin code from code written in the Java programming language Just Works™. Going the other direction usually works without any developer effort too via some automatically applied translation conventions (for example, things like property getters and setters are created for you). With the help of a few Kotlin annotations, you can also customize how the translation is performed.
Finally, many, many developers have told us they love the Kotlin language. (Many of our own developers on the Android team have also been saying similar things.) There is already an enthusiastic community of Kotlin developers for Android, and the Android team has been routinely peppered with questions about Kotlin at public events. The Android community has spoken, and we listened.

So, If you are on Android Studio 3.0 you are not required to add Kotlin plugin as they have bundled directly into it. Prior to this version, you need to install Kotlin plugin.

How to install Kotlin plugin ?

Go to File|Settings|Plugins|Install JetBrains plugin… then search “Kotlin” and install it. If you are looking at “Welcome to Android Studio” screen, choose Configure|Plugins|Install JetBrains plugin… You’ll to restart the Android Studio to see changes after this completes.

Alright, you are set to use Kotlin in Android Studio!!

Let’s create a new project. Choose Start a new Android Studio project. This will start the process to create project. You need to name the project, package name, company domain etc.

From Android Studio 3.0, it has an option to Include Kotlin support as below snapshot. Android Studio will do hard work for you to download plugins, dependencies and configure your project with Kotlin. Now, you can skip below steps, just sync the project and Run it!

Choose the android version on this screen:

Choose an activity that will be generated for you. Android Studio helps to generate empty layout, some snippet of code of your activity.

Now, you are almost done, name the activity here:

Prior Android Studio 3.0, it will create an activity in Java and then you can use to converter tool to convert it java code into Kotlin.

How to convert Java code to Kotlin:

Open your activity which is MainActivity.java here, you need to invoke an action. Press Ctrl+Shift+A and start typing an action name as like below snapshot.

You will see something like this and with a .kt as extension of file instead .java now.

package com.nagarro.newapp

import android.os.Bundle
import android.support.v7.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}

Alright! Everything is going well. Just a last step, you need to configure Kotlin. If you build an application and run it on device or try to edit this file, Android Studio will prompt you that Kotlin not configured, you need to configure it.

You will be prompted to choose Kotlin compiler and runtime version. Choose the latest version from a list:

You will find it has made changes in module and project build.gradle file. It has added apply plugin: ‘kotlin-android’ and added dependencies compile “org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version”

See what changes made in project build.gradle file

Perfect! You are done!! Sync the project and Run it.

Please, if you find something to improve or any suggestion, don’t hesitate to contact me, I’ll try to do my best to answer any question or improve this tutorial.

See you in next story!

--

--