Getting started with Android and Kotlin
|Author||Philip Torchinsky, Svetlana Isakova|
|Last Updated||25 July 2015|
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. 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. We choose Start a new Android Studio project or File | New project. The following dialogs walk us through the process of new project creation. We need to name the project and choose which Android SDK version we have installed. Most options can be left with their default values, so we can press 'Enter' several times.
Name the project:
Choose the Android version:
Choose creating an activity that will be generated for you:
Name the activity:
We've created a new project with one Java activity that was generated for us.
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 we can call this option via the Code | Convert Java File to Kotlin File menu entry or by using the corresponding shortcut (we can find it at the menu entry).
After the conversion we should have an activity written in Kotlin.
Configuring Kotlin in the project
When adding a new Kotlin file, IntelliJ IDEA (and Android Studio) automatically prompts us as to whether we'd like to configure the Kotlin runtime for the project. However, currently, converting existing Java file does not prompt this action. Therefore we have to invoke it manually (via Find Action):
We are then prompted for the version of Kotlin. Choose the latest available from the list of installed versions.
After we configure Kotlin, build.gradle file for the application should be updated. Now we 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)
The last thing to do is to sync the project. We can press 'Sync Now' in a prompt or invoke an action Sync Project with Gradle Files.
Making user interface changes
Now that the project has been set up, we can work with the layout using IntelliJ IDEA's visual designer. There is nothing different in terms of layout when working with Kotlin, thus this is similar to regular Android Java development. While using layout designer in IntelliJ IDEA, note there are two tabs in the designer: Text and Design. The latter shows how the layout looks, while the former allows fine tuning with XML editing.
Building and publishing the Kotlin application for Android
Kotlin has a rather small runtime file size: the library is approximately 858KB (as of 1.1.1). This means Kotlin adds just a little to .apk file size.
We are now ready to build the application in debug mode (<Shift+F9>), run it on an emulator or device (<Shift+F10>), or build signed release of the application to upload it to Google Play or another application store.
We can make a release of the application and sign it similarly to what we do for an Android application written in Java.
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.