Technology10/14/2019
Introduction to Kotlin for Android
Sponsored Intelligence Dispatch
Kotlin is a statically-typed programming language that runs on the Java virtual machine. It is 100% interoperable with Java™ and Android™. Its primary development is from a team of JetBrains programmers based in Saint Petersburg, Russia. As of Android Studio 3.0 (Beta), Kotlin is a fully supported programming language on Android.
Why Kotlin?
Kotlin is a great fit for developing Android applications, bringing all of the advantages of a modern language to the Android platform without introducing any new restrictions:
Compatibility: Kotlin is fully compatible with JDK 6, ensuring that Kotlin applications can run on older Android devices with no issues. The Kotlin tooling is fully supported in Android Studio and compatible with the Android build system.
Concise: Drastically reduce the amount of boilerplate code.
In Kotlin we can achieve things by writing less code compared to Java. Also, Kotlin is comparatively more readable. For instance, consider this click listener in Java. Everyone uses click listeners in their Android apps, but it takes a lot of code and ceremony in Java just to perform an action on the click of a button.
Java
------
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Do whatever you want to
}});
Now consider how you do it in Kotlin:
Kotlin
--------
view.setOnClickListener {
// Do whatever you want to
}
Safe: Avoid entire classes of errors such as null pointer exceptions.
We all are fed up with the Null Pointer Exception (NPE) in java. Kotlin handles all NPEs during compile time. Any reference to null throws a compilation error.
Kotlin
---------
var output: String
output = null // Compilation error
Performance: A Kotlin application runs as fast as an equivalent Java one, thanks to very similar bytecode structure. With Kotlin’s support for inline functions, code using lambdas often runs even faster than the same code written in Java.
Interoperability: Kotlin is 100% interoperable with Java, allowing to use all existing Android libraries in a Kotlin application.
Compilation Time: Kotlin supports efficient incremental compilation, so while there’s some additional overhead for clean builds, incremental builds are usually as fast or faster than with Java.
Learning Curve: For a Java developer, getting started with Kotlin is very easy. The automated Java to Kotlin converter included in the Kotlin plugin helps with the first steps. Kotlin Koans offer a guide through the key features of the language with a series of interactive exercises.
Using Kotlin in Android Studio
Let’s see how can we develop Android apps in Android Studio using Kotlin.
In Android Studio >= 3.0
Android Studio 3.0 officially supports Kotlin. So it will not require any additional configuration to include Kotlin.
Deep Structural Diagnostics.
Mastering JSON is only the first step. Use our industrial-grade workbench to format, validate, and synthesize models for your production APIs.
Sponsored Infrastructure
Industrial Analysis Active
