Why Kotlin Is the Hero Android Needs
19.05.2017 | 6 min read
On May 17th Google announced Kotlin as the officially supported language for Android. It was no surprise for us at 10Clouds. It’s been the most important shift in Android development since many ditched the generally hated Eclipse for Android Studio, based on IntelliJ, a beloved IDE among Java Developers.
Let’s face it – Java is old. A breath of fresh air was all we were waiting for. Since February 2016, when Kotlin 1.0 shipped, we at 10Clouds kept a close eye on its development.
It’s easy to start with Kotlin
To paraphrase the famous author John Green, I fell in love in Kotlin the way you fall asleep: slowly, then all at once. I started learning Kotlin with Kotlin Koans. It’s a series of unit tests that require making them pass. It took some time for a Java head like me to get a grasp of the new concepts, but eventually, all pieces clicked into right places.
Everything was new and, at the same time, familiar. When I didn’t know how to write something in Kotlin, I would write code in Java. Then I’d use a Java to Kotlin converter shipped alongside the Kotlin plugin. It was a huge help. After a few hours, I was so confident in my skills in Kotlin that I didn’t want to go back to Java ever again.
Kotlin makes writing Android apps fun again
As JetBrains likes to say, Kotlin is a language created from industry to industry. They understand problems that modern software developers run into and offer great solutions to help us deliver reliable software in a much quicker and enjoyable way. Writing apps in Java has always been connected with generating loads of boilerplate code. Remember POJOs? With the introduction of Kotlin’s data classes, we can say goodbye to generating the getters and setters for all object fields and writing implementations of toString(), hashCode() and equals() methods. Instead of 50+ LOC in Java, we can reduce that number to 1 (!) in Kotlin.
Remember filtering lists with for loops? Thanks to the functional approach to collections, we can also reduce all the complex, multiline instructions to only one line of code.
Remember Singletons? Even this mighty design pattern gets taken care of in Kotlin thanks to object declarations.
Creating layouts programmatically in Java could result in a mess. Thanks to type-safe Builders, we got one of the most interesting libraries in Kotlin: Anko Layouts, which makes building layouts from code less complicated. Builders are also great for generating HTML files as well as many other DSLs.
Java can be a struggle. There’s no pattern matching, typealiases, extension functions. We had to overload many methods because we couldn’t declare default parameter values and so on, and so on. With Kotlin, most of these problems are gone. All its simplistic, syntactic goodness comes together, creating a new enjoyable experience for developing Android apps.
Kotlin helps create more reliable apps
Every app ever written in Java struggled with the dreaded NullPointerException (infamously called as the billion dollar bug). It interferes with the user experience, often driving her/him to uninstall the app and never use it again. With Kotlin and its introduction of nullable types, it (almost) eliminates the danger of null references in the code. From now on, the compiler plays the role of a guardian and all possible threats of NPE are treated as compile time errors.
You can use Kotlin solutions within your old Java codebase and vice versa
Introducing Kotlin to an existing Java codebase costs almost nothing. Do you want to call Java code from Kotlin class? No problem. Oh, the other way around? Easy.
Behind the scenes, Kotlin is compiled to Java 6 bytecode. You don’t have to worry that you are adding something incompatible to the existing codebase. We introduced Kotlin to many of our projects that were Java-only and it worked seamlessly. We started very simple, converted our POJO classes to Kotlin. Then we added some logic, wrote couple of extension functions (getting rid of Decorator pattern in the process). Finally, we removed bloated Retrolambda library. At first we encountered some problems with Kotlin annotation processor. It only took some tweaking with Gradle build files to make our project work again.
» At 10Clouds we are experts in creating Android apps. Check out our case studies to learn more!
Kotlin works well with both most popular architectures: MVP (thanks to Kotlin Android extensions) and MVVM. But, this subject is so vast that we will cover it in future blog posts.
It has Jetbrains seal of quality
We all trust JetBrains with providing us tools that we use on a daily basis. IntelliJ, Android Studio, PyCharm, and AppCode are just a couple of the most popular IDEs used by developers all around the world. These products are the reason why JetBrains earned trust within developers community.
Speaking of which, the Kotlin community is open and helpful to newcomers. I spent a couple of months on Kotlin Slack and learned a lot about language itself as well as the future plans JetBrains holds.
Becoming a first-class language on Android: What this means for developers
I am almost sure that the applause after the Kotlin announcement on IO 2017 was the loudest during the entire evening. Kotlin will be now shipped with Android Studio working out of the box, starting with version 3.0. No extra installations needed. No more incompatible plugins. All thanks to close collaboration between JetBrains and Google.
For developers, it’s a chance to use this fresh yet mature language to reduce problems associated with rusty Java. The elegant syntax will for sure attract more developers to write apps for Android platform. So did Swift introduction for iOS.
What the future holds for Kotlin, Android, and other platforms
Is Kotlin a production-ready language? Definitely. We used it in our projects and never looked back. With such an amazing community, great effort from JetBrains and support by Google, there is almost no excuse for not trying it out yourself. Without further ado, let’s get back to work!
Need a Kotlin developer? Get in touch with 10Clouds, we’d love to work together!