Tools

Working with Kotlin in Android Studio

With the release of M6, we announced support for Android Studio. Let’s take a deeper look at how to get up and running with Android Studio and Kotlin.

Installing the Kotlin Plugin

Much like with IntelliJ IDEA, to install the Kotlin plugin you need to click on Preferences (Settings) and select the Plugins entry. Easiest way is to just start typing plugin as soon as the dialog pops up.

image

Although we can download the plugin and install from disk, the easiest way is to click on the Install JetBrains plugin… and select Kotlin from the list. Right-click and choose Download and Install

image

Follow the instructions to restart the IDE.

Setting up the Project

Android Studio uses Gradle as its build system and part of the effort involved in supporting this environment was adding Gradle support for Kotlin.

Adding Source folder for Kotlin

A typical Android Project has the following layout

image

where the source code for the project is located under the folder main/java. Since we want to use Kotlin (we can mix and match both Java and Kotlin the same project), we need to create a new folder under main, named kotlin. In the Gradle script we’ll later define this folder as a source root.

image

 

Configuring Gradle

We need to set up some dependencies and source folders in the Gradle configuration. Open the build.gradle file and copy the following

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:0.6.+'
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}

apply plugin: 'android'
apply plugin: 'kotlin-android'

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.jetbrains.kotlin:kotlin-stdlib:0.6.+'
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
}

Update: Replace “0.6.+” with the version of Kotlin plugin you have installed.

The parts relative to Kotlin are highlighted in bold:

  • Select the Kotlin plugin for Gradle and the version we’re using. The M6 release corresponds to 0.6.69.
  • Apply the kotlin-android plugin
  • Import the Kotlin’s standard library
  • Specifiy where the source code files are located

Specifying the sourceSet will automatically mark the folder we created previously (main/kotlin) as a Source Root in Android Studio.

Once we have the Gradle file updated, we should be able to successfully build Android project written in Kotlin.

Hello World with Kotlin

The easiest way to try out Kotlin with Android is to create a default Android project and convert the code to Kotlin, which the IDE can do for us.

  1. Create a new Android Project (we’ll omit the steps for creating new Android projects).

  2. Add the kotlin folder and update the Gradle file as indicated above.

  3. Select the MainActivity file and from the Code menu select Convert Java File to Kotlin File. We can omit creating a backup.

  4. As soon as we do this, Android Studio automatically detects that this is a Kotlin file and asks us if we want to move it to the kotlin folder. We do so.

Once the project is built we should now be able to run it.

Most likely going to be asked questions

Can I combine Java and Kotlin in the same Android Project?
Absolutely. In fact, check out our GitHub repository for some samples.

Can I use nightly builds of Kotlin?
Yes you can. Just update the Gradle project to point to the snapshots and add the corresponding repository.

buildscript {
    repositories {
        mavenCentral()
     maven {
      url 'http://oss.sonatype.org/content/repositories/snapshots'
        }
    }
    dependencies {
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:0.1-SNAPSHOT'

    }
}
. . .
repositories {
    mavenCentral()
    maven {
      url 'http://oss.sonatype.org/content/repositories/snapshots'
    }
}

dependencies {
    compile 'org.jetbrains.kotlin:kotlin-stdlib:0.1-SNAPSHOT'
}

Are there any more goodies you forgot to mention?
Now that you ask, we also have some External Annotations for the Android JDK. Android Studio will automatically prompt you to add these to the project.

Although we’ve been using Android Studio in this post, please note that doing Android development with Kotlin is also possible with IntelliJ IDEA and has been available for quite some time. As always, we appreciate any and all feedback.

image description