My affair with Kotlin and Anko
|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?
Kotlin is a new programming language targeting JVM, Android and even JavaScript. It’s made by JetBrainsthe maker of Suite of the Best IDEs
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.
References:
https://android.jlelse.eu/why-kotlin-ize-your-android-development-96daece20c1d
https://www.youtube.com/watch?v=9B5OXpt53DY
https://android.jlelse.eu/why-kotlin-ize-your-android-development-96daece20c1d
https://www.youtube.com/watch?v=-nz-zwfhrLg
https://www.youtube.com/watch?v=6shERB3mzzQ
https://blog.jetbrains.com/kotlin/2015/04/announci…
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)
Unfortunately, your statement “Java does not support lambda (natively), Try with resources is missing” is not correct.
Try with resources is a Java 7 feature (https://docs.oracle.com/javase/8/docs/technotes/guides/language/try-with-resources.html)
and Lambdas/Method-References are a Java 8 feature (https://docs.oracle.com/javase/8/docs/technotes/guides/language/try-with-resources.html)
This post is one year old post. Java 7 is widely used as it was popular earlier and used for longer duration. My knowledge is limited to 7. I shall update the post.
Though your comment says ver 7 support for try-with-resources, and you pointed it to ver 8 documentation.
Thanks for your comment, I shall update the post.
Java 9 included many features including on-blocking back pressure.