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

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.

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.

This entry was posted in General and tagged , . Bookmark the permalink.

5 Responses to Working with Kotlin in Android Studio

  1. What is the overhead? Compile Time, APK size, with or without proguard.

    Note that with Scala the cumbersome part is not the language itself but scala-library.jar. Which is why I ask for proguard.

    • Hadi Hariri says:

      We’ve not run specific tests yet for Android, but, unlike Scala, our runtime is small and the code performs as fast as Java on most benchmarks.

  2. evilbloodydemon says:

    Why app and kotlin library sources is placed to resulting .apk?

  3. Stuart Rackham says:

    I also had to set the kotlin folder as belonging to project sources otherwise you’ll get a “default activity not found” error message (I’m using Studio 0.2.8 with Kotlin plugin 0.6.350 on Linux), the plugin did not do this automatically as the article suggests. Right-click on the kotlin directory (in the Project Tool window) and select the ‘Mark Directory As->Sources Root’ menu command (you can also manage this from the ‘Project Structure’ dialog.

  4. Stuart Rackham says:

    Mark the kotlin folder as a source root at step 2 (in ‘Hello World with Kotlin’ above) to ensure you get automatically prompted to move the converted Kotlin file to the kotlin folder.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">