Kotlin and Anko for your Android!

Kotlin is a great JVM language that you can use for Android.

The language is often compared with Swift for the iOS and OSX. I’m not much acquainted with the isolated Apple platform, however, I have tried out the open-source version of Swift and its very similar to Kotlin. What’s even cooler about Kotlin is that its very compatible with your existing Java code base. Interop is great and works nicely both ways.

The Kotlin project was initiated by JetBrains, the company behind the platform that powers Android Studio. It has a great tooling support for the language, one of which allows you to covert your Java to Kotlin, but this does not mean that the conversion is necessary.

Get started

Kotlin is very lightweight, its runtime library is under 400K minus the ProGuard minification. Also, installation is very simple. All you have to do is browse the plugin repository and get the official Kotlin plugin. You also had to install Kotlin Android Extensions as well, required for Android of course, but not until recently it has been merged with the Kotlin plugin and is now obsolete.

Settings > Plugins > Browse Repositories > Search Kotlin and install
If you have the Kotlin plugin installed, this is what should be in your plugins list

To configure Kotlin in your project, convert any source file to Kotlin first.

Select a Java file > Hit Ctrl+Shift+A > “convert to kotlin”
> Hit enter

You might see something like this, if you were to start with a clean default MainActivity.java

package com.sample.androidkt

import android.os.Bundle
import android.support.v7.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}

Then, ask IntelliJ to configure Kotlin in your project.

Tools > Kotlin > Configure Kotlin in Project

And, its set! More info on it here.

Android UI with Anko

Anko is a library made in Kotlin that is a great utility for Android development. It consists of DSL wrappers and other nice extensions that make development easier. The prime value of Anko is that it allows you to embed UI layouts inside your source code, which makes it type-safe and allows programmatic transformation.

To test this library I made a simple toy project. Nothing too fancy, it consists of a RecyclerView with an EditText and a Button to add lines of text to the RecyclerView.

Started by letting Gradle know some dependencies, one set for the Support Library, another for the Kotlin runtime, and one more set for Anko obviously.

Reference those dependencies

One thing to be noted here, the Anko base library, i.e., the anko-sdk* lib, you should add on the basis of your minimum SDK version and the rest of the other dependencies, you add on the basis of the Support Library that you wish to extend with Anko. For instance, add anko-design for design, which is from the Support Library.

Write some code

The MainActivity class, basic Activity affairs

That’s the code for the MainActivity, its apparent here how Kotlin lets you handle nullable elements. The let function block works only if savedInstanceState is not null. Scenario is similar inside onSaveInstanceState. Apart from these, the only novelty here is the MainActivityUI object.

If you’ve read the docs at Github for Anko, you will know that you can already write some Anko DSL directly inside onCreate() and have your UI setup right there. But, there might be cases when you wish to place your UI code in a different class. For that you implement AnkoComponent interface, which I’ve done as follows.

MainActivityUI AnkoComponent, core UI and related logic

The lines above represent the entire UI layout (excluding Adapter and Holder) including the interaction logic. Its sort of long, but everything is inline, all done right there. No traversing of views. Direct programmatic logic mixed with expressive declarative DSL.

ViewHolder and Adapter implementation for the RecyclerView

For the sake of completeness, the remaining code that includes the RecyclerView ViewHolder and Adapter has been pasted above.

Oh, and by the way, there’s yet another plugin called the Anko DSL Preview which allows you to view your layouts in your IntelliJ, similar to the one packaged with Android Studio. It also converts your existing XML layouts to the Anko styled code.

This article is not a guide, but just my experience on trying out Kotlin with Anko for Android. I highly recommend checking out the projects for yourself, there’s a lot of stuff out there. Anko is not just for the UI, its basically a good utility for Android development made possible with Kotlin.

Hope you find this new language useful!