Setup Kotlin for Android Studio

Elye
Elye
Oct 23, 2016 · 3 min read

Since most of my articles are using code in Kotlin, I thought perhaps would be appropriate for me to introduce the simple steps for setting up Kotlin in Android Studio. Hopes this would encourage you to at least set it up if you haven’t, so that you could experiment and test whenever you like.

One great news of moving to Kotlin is, you do not need to start from a new project. Your could add it to your existing Java Android project. The language is interoperaable with Java. So you could use my steps below either for a new or existing project.

Prerequisite

This is assuming you already have Android Studio (version 2.2 at the time of writing), its Android SDK (version 24) and Java SDK (at least 7) installed.

Step 1: Setup the Kotlin Plugin in Android Studio

In order to ensure Android Studio support Kotlin, the first thing is to install the Kotlin Plugin for your Android Studio.

Plugins Browser, where you can find Kotlin Plugin for you to install.

That’s all the generically applied to your Android Studio. Only needed to do once per installation of Android Studio.

Step 2: Add Kotlin classpath to project Build.Gradle

For gradle to have Kotlin support, add the two classpaths below, i.e. Kotlin-Gradle-Plugin and Kotlin-Android-Extensions. Also in this file I setup the variable to define Kotlin version, that could be shared by all.

buildscript {
ext.kotlin_version = "1.1.1"
ext.supportLibVersion = "25.3.0"
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"


// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

Step 3: Add Kotlin library and apply Kotlin Plugins in your module Build.gradle.

In the module that will use Kotlin, you will add the Kotlin library into it’s Build.gradle. Also remember to apply both the Kotlin Android and it’s extension plugin to your project (I often forgot this, after add the library).

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
// ... various gradle setup
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile "com.android.support:appcompat-v7:$supportLibVersion"
compile "com.android.support:recyclerview-v7:$supportLibVersion"
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

Step 4: Ready to go…

Now you have setup Kotlin for your app development, you could start writing Kotlin code (in .kt extension). Another way is convert your Java file to Kotlin, using Shift-Alt-Cmd-K or Shift-Shift + search Convert Java File to Kotlin File.

Thanks for reading. You can check out my other topics here.

You can follow me on Medium, Twitter, Facebook, and Reddit for little tips and learning on mobile development, medium writing, etc related topics. ~Elye~

Mobile App Development Publication

Sharing Mobile App Development and Learning

Elye

Written by

Elye

Passionate about learning, and sharing mobile development and others https://twitter.com/elye_project https://www.facebook.com/elye.proj

Mobile App Development Publication

Sharing iOS, Android and relevant Mobile App Development Technology and Learning

Elye

Written by

Elye

Passionate about learning, and sharing mobile development and others https://twitter.com/elye_project https://www.facebook.com/elye.proj

Mobile App Development Publication

Sharing iOS, Android and relevant Mobile App Development Technology and Learning

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store