Android Programming with Kotlin

Ahmed Rizwan
5 min readMay 4, 2015

Behold, there is a programming language out there called Kotlin, which actually makes life easier for us Java & Android developers. Kotlin is developed by JetBrains (the same people who built the awesome Android Studio IDE). *_*

It’s certainly a reason to rejoice as most Java developers are fed up with the boilerplate code Java comes with. I know I am!

The claim of Kotlin is that it is 100% interoperable with Java code, and does not have any major performance degradation at Run-Time. So you can pretty much integrate it into your existing projects and it’ll work perfectly! And also, the compilation time isn’t too bad either (unlike Scala-For-Android). I got about 4–6 seconds of added compilation time. Which is not that bad considering that you save a lot more time while writing code. ☺

Another good part of Kotlin is its RunTime, which is extremely compact (about ~300KBs).

You can find more details about the language here. And here you’ll find the instructions on how you can set it up with Android Studio.

I’ll just try and demonstrate how Kotlin can be a good choice for writing Android code.

Better Syntax and a lot less Code

Lets start with a simple Activity onCreate method. First the Java code :-

Anonymous classes = Not good

Same code on Kotlin :-

Lambdas = Awesomeness!
Note: There’s a plugin called Kotlin Android Extensions, which even lets you get rid of the entire findViewById() line up there. More details about it near the end of this article.

In Kotlin, you can create your own Extension Methods to make your code even prettier. Here’s an example: Outside the class definition, create method extensions through a reference of any class. In this case, I’m creating an extension for ActionBarActivity.

Kotlin File
Note: You can also create a separate file for your extensions. And you’ll still have access to those extensions globally.

Now all you do is call this method from anywhere inside your classes which inherit from ActionBarActivity, like this :-

Back in our Activity class, we can now access the extension method directly, how cool is that! ☺

Learn about the syntax and other features of the language here (as I won’t go into details in the article).

Anko!

Anko is a new Kotlin library for Android. It lets you write the layout-code in DSL format instead of XML, inside the Kotlin class itself. How is that beneficial you might ask! Well the reason is XML itself :-

It is not typesafe

It is not null-safe

It forces you to write almost the same code for every layout you make

XML is parsed on the device wasting CPU time and battery

Most of all, it allows no code reuse.

So with Anko you can get rid of all the XMLs. And the best thing about it is that you’ll be able to re-use your layouts!

Here’s an example of the above Activity code written with Anko (without using any XML layout)

Kotlin code and Anko DSL code go hand-in-hand together

As you can see in the image, DSL is very expressive and you can also preview the layout code while coding (but you’ll need to refresh the preview manually).

There’s a lot more cool stuff you can do with Anko, so I would recommend visiting this link, there you’ll find all the features it offers. And trust me, you’ll be impressed!

Functional Programming on Android

Functional programming is a wonderful thing and with Java (6 and 7), it could be extremely cumbersome to implement. Here’s an example where I filter out Even, Odd and Larger-Than-2 numbers from a list, based on predicates. Be warned! The Java code you’re about to see, is not very pretty.

Yikes! Java doesn’t provide any filter method for collections, so we have to write our own ☹

The same can be achieved in Kotlin with built-in filter methods, which shrinks down the code significantly. And you can do a lot more than that, without adding hundreds of lines of code. Kotlin also gives you methods like map, flatMap, fold, merge, forEach, reduce etc. ☺

Here’s the same code in Kotlin :-

Wow. Such Kotlin. Much Code!

Sure, you can add libraries like StreamSupport and Retrolambda to your Android Projects and get a flavor of Java 8 even in Java 6–7, but I think these features should already be a part of the language itself (which is obviously not the case for Android at the moment). So if you absolutely have to stick with Java, I would highly recommend these two libraries. They will make your life much easier.

I’ll wrap up things now, and will keep on adding examples to the article with time. But before ending, let me introduce you to another very beautiful little thing called Kotlin Android Extensions. It’s a plugin like Anko, also created by JetBrains. If you prefer writing XMLs, then you can use this plugin.

By including it in your project, you can accesss the layout views directly from an Activity or a Fragment. Yeah! That’s right! Directly!

The layout file.

If this is the layout file, then you can access the button by just importing the layout like :

Import the layout, and that’s it!

So, no more findViewByIds and no more casting. And this actually prevents us from accessing Views, that are not inside the ViewGroup associated with our Fragment/Activity. What that means is, no more NullPointerExceptions at run-time, and much much cleaner code!

That’s it for now. Happy coding! ☺

--

--