Why You Should Start Using Kotlin to Supercharge Your Android Development in 2017

Aritra Roy
Aritra's Musings
Published in
10 min readMar 7, 2017

--

The most popular and the most widely used language for Android development is undoubtedly Java. But that certainly doesn’t mean it is the best choice for the developers out there today. The biggest problem with Java is that it is old, verbose, error-prone and not a “modern language” in any way.

They are trying to bridge the gap with Java 8, bringing some modern flavors to the language but Android certainly doesn’t use all the features of Java 8 and we are still stuck in the old Java 7 and 6-ish world which is probably not going to improve much in the foreseeable future.

But the good thing is that we have Kotlin to the rescue and in this article, we will be learning why it’s the right time to start using the more modern, sophisticated and pragmatic language for Android development.

What’s Really Wrong With Java?

I know you have been using Java for years (maybe even decades) and you are extremely familiar with it. You know the language from corner-to-corner and also several undocumented things which can only come up with years of experience.

And now when a new language comes in town and someone tells you to switch to it, you are quite skeptical about it and believe me, I was too. I have been using Java for quite a long time now and have formed a love-hate relationship with it.

I was not at all convinced to switch from Java initially but when I started looking into the bigger picture with an open mind, things really took the turn.

Java is Old, Very Old

Java was really one of the best languages back then but things do change with time. And things are even worse in Android where we don’t have support for lambdas, method references, streams, try-with-resources (minSdk ≥ 19), javax.time APIs in the old Java 7 or 6 world.

But there are some third-party ways of backporting some of these features like RetroLambda, Streams backport, ThreeTenABP, but that’s always a hassle, isn’t it?

Android Nougat also made a bold attempt of bringing some of the Java 8 features using the Jack compiler but most of them are only usable if you target minSdkVersion of 24, which you really shouldn’t considering how slow Android users are in getting updates.

Check out the platform version distribution chart here.

Java is Error Prone

One of the biggest flaws in Java is the way it handles “null” leading to the dreaded NulPointerException (popularly known as The Billion Dollar Mistake).

I call it my billion-dollar mistake. It was the invention of the null reference in 1965…This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years. — Sir Charles Antony Richard Hoare

One of the most common reasons for crashes in Android apps today is the NullPointerException. It’s really a big deal in the industry today. It is almost impossible to have an app in production without a single NullPointerException (if you do, please tell me about it, I would be interested to hear ;-)).

And Nullability is an even bigger problem in Android. Null is a very efficient and simple way of representing the absence of a value and Android uses it everywhere in its framework and its APIs making it even more difficult for us to handle them.

Another problem we often ignore is the way Java implements non-static Inner classes and Anonymous Inner classes which always keeps an implicit reference to the outer class thereby making your app susceptible to memory leaks.

There are several other language design flaws making it a really painful experience for developers sometimes.

Recommended Reading

Verbosity and Ceremony

We developers always love clean and concise code. Less code takes less time to write and is less susceptible to bugs. But with Java, you always have to write a lot of code to get even the “simplest” thing done. You should have already noticed this, didn’t you?

There is a lot of “ceremony” involved in Java APIs and Android makes it even worse by burdening the developers to do a lot of things in a particular order to get most things done, like accessing the database, fragment transactions and so many more.

Fixing all of these can certainly improve the experience and productivity of the developers considerably.

(Source: kotlinlang.org)

Kotlin To The Rescue

The good thing is that we have Kotlin, a statically-typed JVM based language developed by JetBrains. It has been developed completely as an open-source project giving special importance to Android development.

Statically typed programming language
for the JVM, Android and the browser — Kotlin

Kotlin comes from the good folks at JetBrains, the guys behind the best IDEs like IntelliJ and our beloved Android Studio. They understand the pain we developers face in our day-to-day development workflow and are trying to address the real use cases in this language.

Not only that, they are also using Kotlin in production to develop their own products as well so it is highly unlikely that this project will get abandoned anytime soon.

Whereas the other JVM based languages tend to completely forget that Java (and the plethora of Java libraries) even exists and wants to re-implement everything, Kotlin takes the complete opposite path where it acknowledges the fact that Java is a “big thing” and improves over its limitations.

Kotlin is 100% Interoperable

This is the first thing I loved when I heard about Kotlin. You can call Java code from Kotlin and vice-versa seamlessly. Both Kotlin and Java generate the same bytecode and you have nothing to worry that you are shipping something completely different with Kotlin.

You can start using Kotlin in your existing project with all your old Java code straight away. Start by writing some simple and small parts of your app in Kotlin as you slowly start getting familiar with the language, its constructs and its syntax (which is btw, super simple :-)).

You don’t need to convert your entire project to Kotlin right from the beginning. Like, I have started using Kotlin in a significantly large project where I have started writing some minimal UI components and simple business logic in Kotlin at first. Currently, only 4–5% of the entire codebase is in Kotlin and the rest is still the legacy Java code I have been writing for months (which I will convert eventually).

But the mix and match of Java and Kotlin is working really well in my project. The interoperability is truly a blessing in disguise.

No More NullPointerExceptions

I have been developing Android apps for around 3 years now and one of the most common reasons for crashes in our apps have been the NullPointerExceptions.

We have spent a lot of time and resources fixing a plethora of NPEs in several of ours apps. Guarding your code with null checks everywhere becomes seriously a very time-consuming and boring task which could have been spent in a more productive way otherwise.

And the great thing about Kotlin is that null-safety is a part of the type system itself. It is so much better to catch nulls during the compile time than crashing your app at runtime.

For example, in Kotlin, all variables default to non-null and if you want a nullable variable you mark it with a “?”. Suppose you have a String variable and you are trying to assign a value to it, everything works just fine,

var message: String
message = “Have a great day”

Now, if you try to do this,

var message: String
message = null

The compiler fails to compile and gives you this message, “Null can not be a value of a non-null type String”. This happens because all variables default to non-null and you have to explicitly tell the compiler that you want a nullable variable.

var message: String?
message = null

And now it compiles fine but you have to be careful while accessing this variable. With this type system in place, it is very difficult for NPEs to occur in your app at runtime as they will be caught and addressed at compile-time itself.

Recommended Reading

Great IDE and Tooling Support

You have very little to worry about in this department when you know that Kotlin is a JetBrains product, the ones who specialize in creating some of the best and most used IDEs in the world.

There is a plugin that you can install in Android Studio to get Kotlin working in your project. Yes, it’s that simple. They have an Eclipse plugin if you are still using Eclipse (seriously, are you?).

All IDE features work perfectly in Kotlin. You can mix and match Kotlin and Java code in the same project and everything still works great. The IDE support is really great with Kotlin where other JVM based languages don’t even come close.

Another amazing feature that the Kotlin plugin brings to Android Studio is “Convert Java File to Kotlin Feature” and I loved it so much when I saw it for the first time. All you need to do is give it your old Java file and in one-click, it can convert it to its equivalent Kotlin file.

I have converted several Java files to Kotlin using this feature and have found it working really well 99% of the time. Read more about it here.

Write Less Code, Be Happy

Now with Kotlin you write significantly less amount of code than you have ever written with Java. Kotlin files are significantly smaller and more concise than their Java equivalents.

Writing less code, takes less time and is less prone to bugs or human errors. The Java files consistently shrink even when you convert them to Kotlin using the plugin in Android Studio. I have converted several Java files of various sizes into Kotlin in my project and I am yet to find a case where this is not true.

The Kotlin syntax is not verbose like that of Java. It is very crisp, concise and reduces a lot of boilerplate code that we developers have to write every day.

For instance, just take a look at this. We all use click listeners in our Android apps,

view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Do whatever you want to
}
});

Just to perform an action on the click of a button, there is so much code and ceremony involved. Now just have a look at the Kotlin equivalent,

view.setOnClickListener {
// Do whatever you want to
}

See for yourself how simple and tight the syntax is. Just have a quick look at the Kotlin language reference and you will surely love the conciseness and simplicity of it even more.

Kotlin Is Here To Solve Real World Problems

Kotlin is suddenly not the new kid in town. It has been there for a few years now but JetBrains kept it closed to themselves until now. Kotlin has been developed to solve the everyday real-world problems that we developers have to deal with.

Kotlin, unlike some other JVM-based languages doesn’t come from an academic or research standpoint. A programming language is simply a “tool” that we developers use to build applications and the idea is to take real use cases in mind and improve this “tool” further.

It is always tempting to rebuild or re-implement everything from scratch and JetBrains could have done that with Kotlin but they didn’t. The goal is not that, but the goal is to build a “truly useful programming language” that will make our lives easier.

Recommended Reading

Conclusion

By now you should be quite convinced as why Kotlin is the kind of language you had always wished for and you finally have it. Kotlin makes Android development a lot more fun again. It’s like giving a new toy to a kid who was bored for a long time the same old one and when the new toy is so much better than the old one.

So what are you waiting for? Go ahead, open Android Studio, download the Kotlin plugin and get started right away. This article was originally published on TechBeacon.

Click the 💚 below to show your support and share it with other fellow Medium users.

--

--

Design-focused Engineer | Android Developer | Open-Source Enthusiast | Part-time Blogger | Catch him at https://about.me/aritra.roy