Edit Page

Getting started with Android and Kotlin

This tutorial walks us through creating a simple Kotlin application for Android using Android Studio.

Installing the Kotlin plugin

First, if using Android Studio, you'll need to install the Kotlin plugin. Go to File | Settings | Plugins | Install JetBrains plugin… and then search for and install Kotlin. If you are looking at the "Welcome to Android Studio" screen, choose Configure | Plugins | Install JetBrains plugin… You'll need to restart the IDE after this completes.

Creating a project

It’s extremely easy to start using Kotlin for Android development. In this tutorial we’ll follow the warming up process with Android Studio. If using Intellij IDEA with Android, the process is almost the same.

First let's create a new project. Choose Start a new Android Studio project or File | New project. The following dialogs walk you through the process of new project creation. You need to name the project and choose which Android SDK version you have installed. Most options can be left with their default values, so you can press 'Enter' several times.

Name the project: Dialog 1

Choose the Android version:

Dialog 2

Choose creating an activity that will be generated for you:

Dialog 3

Name the activity:

Dialog 4

You've created a new project with one Java activity that was generated for you.

But now we'd like to add some code in Kotlin. The easiest way to start using Kotlin is to convert automatically Java activity into Kotlin one. Please note that anytime instead of looking through documentation for a new way to express an old pattern, you can write it in Java, then copy-paste Java code into Kotlin file, and Intellij (or Android Studio) will suggest to convert it.

Converting Java code to Kotlin

Open MainActivity.java file. Then invoke action Convert Java File to Kotlin File. You can do it by several ways. The easiest one is to invoke Find Action and start typing an action name (like in a screencast below). Alternatively you can call this option via the Code | Convert Java File to Kotlin File menu entry or by using the corresponding shortcut (you can find it at the menu entry).

Convert

After the conversion you should have an activity written in Kotlin.

Koltin-Activity

Configuring Kotlin in the project

If you start editing this file, Android Studio shows you a prompt that Kotlin is not configured, so you can configure it.

Config-Kotlin

You are then prompted for the version of Kotlin. Choose the latest available from the list of installed versions.

Config-Kotlin-Details

After you configure Kotlin, build.gradle file for the application should be updated. Now you can see that apply plugin: 'kotlin-android' and the dependencies were added.

(For more details how to set up gradle for your project, please check Using Gradle)

Sync-Project-With-Gradle

The last thing to do is to sync the project. You can press 'Sync Now' in a prompt or invoke an action Sync Project with Gradle Files.

Sync-Project-With-Gradle-2

Building and publishing the Kotlin application for Android

You are now ready to build the application and run it on an emulator or device. This works in exactly the same way as in Java. You can make a release of the application and sign it similarly to what you do for an Android application written in Java.

Kotlin has a rather small runtime file size: the library is approximately 859KB (as of 1.1.2). This means Kotlin adds just a little to .apk file size.

Kotlin compiler produces byte-code, thus there really is no difference in terms of look and feel of Kotlin applications versus those written in Java.

What's next?

Read about Kotlin Android Extensions plugin. If you want to learn different Kotlin features, try Kotlin Koans.