Ever since Google announced default support for Kotlin in Android during their IO conference, there is big buzz in the industry. Already 10 million lines of code written in Kotlin within just one year , I had even seen statements like “java is dead” and Kotlin is used by 160 K users
So I felt it is important to learn this new language to understand major features.
By default, android is known for two important languages – java and XML
XML is being replaced by Anko and java by Kotlin soon.
Google’s strong support for Kotlin is not surprise as Oracle sued them for using Java (Bit of clarity lacking which part of java is open source and which part is not)
The flow of the blog is : What is Kotlin, why is it needed, How to set it up and run programs
What is Kotlin?
Why Kotin ? Why it became so popular?
Reason starts with comparing it with Java 7.0
1. java is too verbose, even for small feature, many lines of code is required. Kotlin uses less code (No wonder, Developers liked it)
2. Famous dreaded fear of java is NPE (null pointer exception) is handled effectively by Kotlin
3. Kotlin is functional programming language
4. Java does not support lambda (natively), Try with resources is missing (Version 7 and above seems to support these features – Thanks to Paul Baker)
5. Kotlin is semicolon free language
Setting up and sample program:
I downloaded below items to explore:
1. Android studio Version 3.0 Canary 4 (which has default support for Kotlin)
2. Anko plugin
you don’t need to replace your stable version of android studio to install 3.0, they can run side by side , you can reuse your SDK tool chain as well
Unzip the contents of downloaded zip file and run bin/stduio64.exe
Overall it took around 3 hours to set all things up (like downloading, installing, configuring)
One can see default option now is “include Kotlin support”
Few other things to be done in gradle build file
I started writing my first Hello world example in Kotlin.
Apart from Kotlin, it is interesting to use Anko for XML replacement
Benefits of Anko
- Everything is in one place. Instead of splitting layouts into static (XML) and dynamic parts and then trying to tie them together, we can just write everything we want using Kotlin. Compile-time type checking is a sweet bonus.
- Anko can make our code more concise and readable.
- It allows for easy re-use. We can just extract a part of the DSL into a function and use it multiple times.
Please share your Kotlin (or any other new language story)