Kotlin announced as a first-class citizen in Android. Why does it matter?
With Google blessing you can now safely start using Kotlin in production Android apps to increase productivity and bring joy to your day-to-day work.
Ok, what actually happened?
But, there is one more thing (…) One thing our team has never done for developers. We have never added a new programming language to Android and today, we’re making Kotlin an officially supported language in Android.
— Stephanie Saad Cuthberston, Director, Product Management, Android
During annual Google I/O conference Kotlin programming language has been announced as a first-class citizen in Android moving forward. It was warmly welcomed by audience with great applause. Kotlin is going to be supported on the similar terms Java is right now. If you wonder what it means for the future of Android development, let me share my thoughts with you.
What is Kotlin anyway?
Kotlin is a statically typed programming language for modern applications running on JVM. It is concise, expressive, null-safe, fun and provides 100% interoperability with Java. It has been designed with mobile first in mind with Android developers being a really large target audience.
Being developed for last 6 years and reaching version 1.0 in February 2016 it has proven to be ready for production. It was created at JetBrains, Kotlin development company also known for creating the suite of modern IDEs including IntelliJ IDEA, RubyMine and many more…
If you have ever wondered, its name originates from a small Russian island close to Saint Petersburg :)
Why is it a perfect fit for Android?
Device fragmentation is a known issue in the Android world. Different hardware features, screen sizes, Android versions and even Java Runtime Environments make it difficult to write a concise and clean code using latest features. Here is where Kotlin really shines, because it boosts developers productivity, let them use latest language features, i.e. lambda expressions, and is still being able to run across all devices due to full bytecode compatibility with Java 6.
Shipping apps on mobile requires a thoughtful approach to application size, especially for emerging markets, where the internet bandwidth might be a constraint. Kotlin adds only around 1 MB (compared to 7–30 MB for Swift depending on libraries used) to the final size of an application binary.
Thanks to full interoperability with Java it can be gradually introduced to any Android project, so there is no need to stop developing new features in order to convert entire application at once. You can start using Kotlin for Android app development right now.
Gradle, main build system for Android, is configured and can be extended by plugins written in Groovy. This is a pain point for developers, who cannot rely on tools to get proper autocompletion and documentation they are used to in Java. Since 2015 Gradle team is working to bring in support for Kotlin, which will ship with both autocompletion and documentation right in the IDE. There is still some work to be done, but the progress has been significant.
Using Android API requires a lot of ceremony. Certain methods need to be called in proper order and it’s error prone, because whenever one method call is omitted, bugs are introduced. Kotlin can remedy that with inline extension function expressions, which can encapsulate all of the ceremony without any impact in runtime.
Kotlin helps to avoid two most common errors on Android — NullPointerException and leaking context. It has null-safety built-in meaning developer has to handle null cases wherever they might happen, which leads to more maintainable code without running into defensive programming. Context leaks are often caused by instances of anonymous classes (or lambda expressions) which hold references to outer context in Java. Lambda expressions in Kotlin are compiled to static classes unless they explicitly use a reference from outer context, hence the risk is much lower.
Current state of affairs
If you look at popularity of programming languages, you will notice that Kotlin is not widely adopted to say at least. On stackoverflow.com there are only 2.6 thousands questions regarding Kotlin. It is not very impressive when you compare it with Swift, mentioned over 50 times more often. It’s a huge difference taking into account that Android is twice as popular as iOS is in terms of questions asked.
Companies as well as developers in general were not so keen to introduce Kotlin to their primary applications, because its future on Android was uncertain. Of course, there are some early adopters, i.e. Basecamp, who have started to migrate their products soon after Kotlin 1.0 was released.
What does the announcement change?
Let’s discuss first what is a first-class citizen. The term was coined in the 1960s by Christopher Strachey. In simple words it means that both Java and Kotlin will receive fair treatment, so you can do with the latter everything that you could do with the former.
The announcement has already increased popularity of Kotlin significantly. Number of stars on GitHub Kotlin repository has already risen by 50% reaching level of 12 thousands. Traffic on kotlinlang.org has grown by 263% in terms of organic search according to SEMRush.
In parallel to the Google I/O keynote, examples for Kotlin have been added to the official Android documentation for the latest release — O. I expect the examples coverage to grow steadily from now on. It means easier and most likely wider adoption for newcomers.
Bigger traction means growing community. The potential for growth is huge as Android developers were kind of stuck with Java. Even IDE helps to reduce visual clutter caused by its verbosity, i.e. displaying single-method anonymous interface instances as lambda expressions.
Growing community leads to better libraries as more and more great developers find an urge to improve upon existing solutions in order to increase productivity and bring joy into their day-to-day work.
Needless to say, there is a lot of room for improvements, especially in Android API itself. I expect developers to use extension functions to work with cleaner facade on top of existing interfaces. It gives Android team a chance to rework underlying APIs much easier and faster once proper adoption level is reached. It also leads to lower costs of Kotlin app development.
Companies can now safely start investing resources in hiring Kotlin developers as there is going to be more and more support from Android team from now on.
Google is willing to establish and participate in Kotlin Foundation to secure future of the language and push new features forward.
This is a good time for Java developers to try Kotlin, as it is more fun to work with without necessity to resign from vast ecosystem of Java libraries thanks to full interoperability. Moreover it’s really easy to get started now as Android Studio provides a plugin to convert existing Java code to Kotlin with just one click. It might not be the most idiomatic Kotlin code, but it’s definitely a perfect way to get familiar with it.
As can be seen in Android Studio 3.0 Preview, Kotlin has been already fully incorporated. There will be no longer need to install yet another plugin. It will just work. IDE will be shipped with Kotlin samples ready to use out of the box.
Conclusion
There has never been a better moment to dive into Android development with Kotlin. Appreciating Kotlin as a first-class citizen is a huge step forward, as developers can now safely move towards it and benefit from higher productivity. As a community we can help to redefine Android development and bring Android APIs to the next level to make it easier to create high quality apps. I am looking forward to hear from you as well as to be part of the movement.
Originally published at railsware.com on May 25, 2017.