Posted on

Android Kotlin Integration

Android Kotlin Integration

Designed by Freepik

Hey guy, today I want to talk about the language that can be a replacement for Java. Although running on JVM, it has completely different syntax than Java. If you consider C# minimalistic to Java, then Kotlin’s even more! Plus it’s fully Java compatible and has great IDE support. Let’s see why and how you should start using Kotlin in your projects.


First of all let’s start with setup. If you already using Kotlin, then you can skip this part. You just need to add those lines to your module’s build.gradle

Sync, and then Android Studio will ask you to install Kotlin plugin, after that you’re done, And if you’re using Data Binding, add those lines


If you read my post about Android Templates, then you know how much they speed up setting up new¬†projects. Basically all the stuff from above can be automatically generated when you create new projects. I’m not going to get into details of that, just want to point to this repo. It has Data Binding (gradle, Activity templates, layouts), Retrolambda, Kotlin enabled by default. You just copy the whole repo and replace you templates directory with this one. If you don’t know where to find templates folder, you can check this post in a glance.

Why Would I?

The question comes: why would I want to spend time to learn Kotlin, if you haven’t of course. Since you’re doing totally fine without. Am I bored of Java or what? Well, that’s the great point about Kotlin: you don’t have to switch the whole code base to Kotlin, it’s fully Java compatible. You can call Java from Kotlin and vice versa. Ooo! And one more little thing: Android Studio will convert all your Java classes to Kotlin! Code -> Convert Java to Kotlin or Ctrc+Alt+Shift+K. Just select all Java classes and use that shortcut. Could it be even easier?


Ok, Ok, I get it, it’s super easy to setup, but is it better? Surprise, just by opening any tab from this reference you’re convinced – Kotlin is the next level. JetBrains guys put good amount of thinking into covering every aspect of Java parts which are not at level with modern languages.


What I want to put accent on if POJO. How often do you create ones? Quite often, right? For every model up there. Let’s say we have this user model

As you see, I have inline multiple variables declared, but what happens when you create constructor with all the variables? It looks cluttered, 60 lines of code. C# has that nice syntax for getters/setters, but just look at Kotlin

Wha what? Just one line – ¬†constructor, getters, setters. Public is the default visibility. I think Java 9 will have var as well. And in Java you get all the methods that you would get. And the best thing is that IDE is you reference for syntax, don’t know something, just write in Java, convert to Kotlin, learn. Most likely it will generate good code. Would I say that you should learn Kotlin and then Java? Nope, in that case you’d be like : why do I even want to use Java? I love Java though.


How often do you wrap your code with if not null? Kotlin has this awesome solution – nullable and non-nullable references. First one is with ? after variable type. So what’s nice about it is by having this code you’ll never get NPE, it’s just like if not null statement

Kotlin has a huge reference for Null safety, just check that out. And there’s improvements in every aspects. What I wanted to point is how easy and flexible it is.


Alright, thanks for reading. You can get source code here… Nope, not today. Just add those setup lines to your project, convert it and you’ve got a Kotlin project! Don’t forget to subscribe, follow me on G+, Twitter, Facebook and share with friends if you think they will benefit from it!