Kotlin for Android development
The most popular and widely used language for Android is undoubtedly Java. Java is very stable language and stand in market from more than 20 years. It is also official language supported for Android. But java has some pitfalls while working with Android. Although java 8 comes with added features to fill the gaps, Android only supported subset of java 8. Java as language also has some limitations like null-unsafety, endless try-catch use, syntax are pretty verbose, particularly when compared to many modern programming languages.
JetBrains has come up with new language named Kotlin. Here they are trying to add modern language feature for application development. They want more productivity by switching to a more expressive language. At the same time, they cannot accept compromises in terms of either Java interoperability (the new language is going to be introduced gradually, and needs to interoperate smoothly with the existing code base) or compilation speed. Kotlin is becoming a boom in market and it is appreciated by many Android developers. In Google I/O 2017 conference held on 17 May, Google announce official support for Kotlin as development language for Android.
In this blog we are trying to highlight Kotlin and Java comparison, how to start using kotlin for Android development and key features added in kotlin. We are not addressing kotlin for web development.
Kotlin as a language
Kotlin is statically typed programming language that runs on JVM. If you want to try kotlin in your projects, you don’t have to convert existing project completely in kotlin. Kotlin provide 100% interoperability to Java.
Some Java issues addressed in Kotlin
- Null references are controlled by the type system
- No raw types
- Kotlin does not have checked exceptions
What Java has that Kotlin does not
- Primitive types that are not classes
- Static members
- Checked exceptions
What Kotlin has that Java does not
- Null safety
- Extension functions
- Smart cast
- Lambda functions
- Range expression
- Data classes
- Operator overloading
Switching to Kotlin
Android studio 3.0 comes with Kotlin support. So it is very easy to start development with Kotlin.
When we start with new project it ask to include kotlin support. In case someone is using Android studio 2.X, they need to install Kotlin plugin. But upgrading Android studio is recommended.
Interoperability with Java
The advantage of kotlin is that, it provides complete interoperability with Java. You can keep Java and Kotlin code together in same project. You can also call Java code from Kotlin and vice versa. So if you want to move to Kotlin, you don’t have to convert all previous Java code to Kotlin. Kotlin also supports all existing Java libraries.
The most common mistake developers do, is by accessing null references which causes NullPointerException, also called Billion dollar mistake. Kotlin aimed to remove danger of null references. Kotlin distinguishes variables between nullable and non-nullable references. By default variables in Kotlin are non-nullable. They don’t allow to store null value.
If developer want to store null value, then one has to declare variable as nullable explicitly using ‘?’.
Kotlin compiler is smart to indicate error, if user is accessing null variable.
If you access nullable variable with safe(?.) operator then it will not throw null pointer exception, it returns ‘null’ as value.
Kotlin Android Extensions
findViewById() is most frequently used for android development and it is without a doubt, a source of potential bugs (not type-safe) and nasty code which is hard to read. While there are several libraries available that provide solutions to this problem, being libraries dependent on runtime, they require annotating fields for each View. Kotlin provide same experience without adding any code overhead, you have to only import dependencies in your Activity.
For Android Extension, you need to enable Android Extension Gradle Plugin in your project’s module build.gradle.
apply plugin: ‘kotlin-android-extensions’
Now you can import dependencies from your xml into Activity.
All the Views declared in layout file becomes extended properties of Activity.
Kotlin come up with some interesting libraries, one is Anko. The problem with xml design is, it has to be parsed and it add performance overhead. Anko provides DSL function to design layouts with simple and manageable code. You can use all Views provided by Android to design layouts, i.e. Navigation Drawer,RecyclerView.
Here’s simple example of layout design using Anko
Anko provide Preview Plugin but the major drawback is, it is not supported to Android Studio 2.1 or higher versions. Anko is not only for designing layouts, it also has following components
- Anko Commons: a lightweight library full of helpers for intents, dialogs, logging and so on
- Anko Layouts: a fast and type-safe way to write dynamic Android layouts
- Anko SQLite: a query DSL and parser collection for Android SQLite
- Anko Coroutines: utilities based on the kotlinx.coroutines library
Kotlin is becoming a first-class citizen in the Android world. Kotlin is a chance to use a modern and powerful language, helping solve common headaches such as runtime exceptions, null pointers and source code verbosity. Kotlin is easy to get started with and can be gradually introduced into existing projects, which means that your existing skills and technology investments are preserved. Kotlin learning curve is very easy, so that Android developers who are comfortable with Java can start with Kotlin with very little efforts.