Technology
8 min read

Kotlin on Android - The Better Programming Language

Kotlin has been used by leading companies like Pinterest, Uber, and Coursera for their mobile app development. At Modeso, we used it to build an instant messaging app and now we would like to share our experience. In this article, we described the main pros and cons of Kotlin and what sets it apart from Java.
Written by
Samuel Schmid
Published on
September 12, 2017
Read time
8 min read

Introduction

At Modeso we keep up new technologies and devices. We started a pilot project to learn, evaluate and explore Kotlin’s potential — the newly adopted Google language. Our blog post will cover the following topics:

  • Introduction
  • Main Differences between Kotlin & Java
  • Pros & Cons of using Kotlin instead of Java
  • Conclusion

We employed our learnings & findings as a part of a MVP. In order to get the best result we decided to implement an Instant Messaging App. This application is completely build on Kotlin, using RabbitMQ as a backend for message relaying. It enables you to exchange text, voice messages, files, images and so on. We chose this sort of app because it requires considerable amount of code to handle asynchronous operations processing, IO-handling (database and file processing etc) and complex UI designs. In the end, tackling all these challenges in Kotlin on the Android platform was a straight forward process as we will show later on in this blog post.

Kotlin is developed based on the Apache 2 license, targeting JVM, JavaScript and Native development. It is designed to be a pragmatic language that can be used for any kind of development such as desktop, web, server-side and mobile (Android) applications. It is also created to be fully interoperated with Java. Thus, you don’t have to throw old code away, but rather gradually start using Kotlin. Also, Kotlin is heavily inspired by Java, Scala, C# and Groovy. Thus, it will take approximately 2–3 weeks to be up and running.

Useful sources to start with
Successful Apps on the Market
Frameworks & Libraries
  • Ktor is a framework for quickly creating web applications in Kotlin with minimal effort.
  • Anko is a Kotlin library which makes Android application development faster and easier. It makes your code clean and easy to read, and lets you forget about rough edges of the Android SDK for Java.
  • RxKotlin is a lightweight library that adds convenient extension functions to RxJava.
  • Kotpref Android SharedPreference delegation for Kotlin.
  • Kotlin Android Extensions is a compiler extension that allows you to get rid of findViewById() calls in your code and to replace them with synthetic compiler-generated properties.

Main Difference between Kotlin & Java

Kotlin fixes series issues that Java suffers from:
  • Null reference: Kotlin type system is designed to eliminate null pointer exception
  • Raw type: Kotlin convert raw Java types into star projection to avoid using raw types
  • Array in Kotlin are invariant: meaning that Kotlin doesn’t allow to assign Array<String> to an Array<Any> to prevent runtime failure
  • Function types: Kotlin has higher order functions — no need to use Java’s SAM-conversions
  • Use-site variance without wildcards
  • No checked exceptions
Kotlin’s new features:
  • Higher-Order Functions: a function can take other functions as parameters or returns a function
  • Improving lambda expressions performance with inline functions by copy and paste the function in the caller site which reduce overhead
  • Extension functions: provide the ability to extend a class with new functionality without having to inherit from the class
  • Null-safety: Kotlin’s type system is aimed to eliminate the danger of null references from code
  • Smart casts: in many cases, you don’t need to use explicit cast operators in Kotlin, because the compiler tracks the checks for immutable values and inserts (safe) casts automatically
  • Properties: classes in Kotlin can have properties, which can be declared as mutable by using the var keyword or read-only. To use a property, simply refer to it by name, as if it were a field in Java ex.(person.name)
  • Primary constructors: the primary constructor is a part of the class header — it goes after the class name
  • First-class delegation: the delegation pattern has proven to be a good alternative to implementation inheritance and Kotlin supports it natively requiring zero boilerplate code
  • Type inference for variable and property types: automatic deduction of the data type
  • Singletons: Kotlin (after Scala) makes it easy to declare singletons
  • Declaration-site variance & type projections:
    declaration-site variance: use out modifier to annotate parameter “T” of generic class/interface which insure that it is only returned (produced) from members of that class/Interface and never consumed.
    – Type projections allow us to specify variance at the use-site instead.
  • Range expressions: range expressions are formed with rangeTo functions that have the operator form (..) which is complemented by in and !in ex.(for (i in 0..100))
  • Operator overloading: Kotlin allows us to provide implementations for a predefined set of operators on our types
  • Companion objects: in Kotlin, unlike Java or C#, classes do not have static methods. In most cases, it’s recommended to simply use package-level functions instead. If you need to write a function that can be called without having a class instance but needs access to the internals of a class (for example, a factory method), you can write it as a member of an Companion objects inside that class
  • Data classes: we frequently create a class to do nothing but hold data. In such a class some standard functionality is often mechanically derivable from the data. The compiler automatically derives the following members from all properties declared in the primary constructor:
    – equals()/hashCode() pair,
    – toString()
    – componentN() functions corresponding to the properties in their order of declaration
    – copy() function
  • Separate interfaces for read-only and mutable collections: unlike many languages, Kotlin distinguishes between mutable and immutable collections (lists, sets, maps, etc.)
  • Coroutines: Coroutines simplify asynchronous programming by putting the complications into libraries. The logic of the program can be expressed sequentially in a coroutine and the underlying library will figure out the asynchrony for us

Kotlin lacks of:

  • Primitive types that are not classes: unlike Java, Kotlin doen’t have any primitive types that are not classes. Every types in Kotlin are classes
  • Static members: Kotlin doen’t have a static method in classes. It is recommended to use package-level method instead. In some cases it is required to have static method like factory method. You can use companion object
  • Non-private fields: classes in Kotlin don’t have fields, but they come with backing fields in case of custom assessors

Pros & Cons of using Kotlin instead of JavaHere are some advantages that makes Kotlin stand out from other languages:

  • Fully interoperate with Java
    Kotlin offers a full interoperation with Java, which allows the use of Java libraries and frameworks. Also, you can apply Java and Kotlin in the same project without any difficulties. Thus, you can gradually start using Kotlin in your existing project.
  • Easy Learning Curve
    Kotlin is inspired by Java, Scala, C# and Groovy, which makes it easy to learn particularly for Android developers.
  • Functional and object-oriented programming
    Kotlin is functional programming and object-oriented programming so it has the advantage of both paradigms
  • Android Studio Support
    Kotlin’s plugin on Android Studio offers auto configure project with Kotlin and convert Java classes to Kotlin.
  • Concise Code
    Kotlin aims to be very concise, less boilerplate code which give developers more time to concentrate on their codebase. If you compare the codebase of Java and Kotlin doing the same thing you will find that Kotlin code is smaller than the Java one.
  • Google support
    After Google announced Kotlin support for Android on Google I/O 2017, Kotlin now is an official language on Android and it might be the future of Android development.

Every programming language has its drawbacks, and here what you have to keep in mind while using Kotlin:

  • Kotlin’s Standard Library sizes of 800 KB
    When using Kotlin in Android, the Kotlin standard library will give your APK an extra size of around 800 KB.
  • Smaller community
    Since Kotlin is a relatively new language, it still has a smaller community than Java.
  • Initial readability
    Due to Kotlin’s concise code it makes it at first time a bit hard to understand.

ConclusionHere we would like to share some outcomes for Kotlin beginners:

  • If you have basic experience with OOP languages like Java/C#, it will be a painless thing for you to study & learn Kotlin. It will take you approximately 2–3 weeks to be up and running.
  • Don’t start with complex IDE’s like AS or Intellij. Instead use the simple online compiler or download the compiler
  • Apply the command line as a start to focus more on the syntax of the language regardless of the targeted platform.
  • The language adds some out-of-Java features that you must know at first.
  • The Kotline official reference should be your first choice when searching for a detailed info about some feature.

Summarized, Kotlin is a fantastic language for both, Android beginners or Android veterans. It will reduce your codebase significantly, has higher order functions and Java 8 features, which in turn reduces time, effort and cost. It is a statically typed language that is heavily inspired by Java, Scala, C# and Groovy. Thus, you won’t spend too much time learning it. At Modeso, we expect to use Kotlin for our upcoming applications without question.Credits: Modeso’s Mobile Engineers Mahmoud Abd El Fattah & Hazem Emad

TABLE OF CONTENT
Weekly newsletter
No spam. Just the latest releases and tips, interesting articles, and exclusive interviews in your inbox every week.
Read about our privacy policy.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Header image
Preferences

Privacy is important to us, so you have the option of disabling certain types of storage that may not be necessary for the basic functioning of the website. Blocking categories may impact your experience on the website. More information

Accept all cookies

These items are required to enable basic website functionality.

Always active

These items are used to deliver advertising that is more relevant to you and your interests.

These items allow the website to remember choices you make (such as your user name, language, or the region you are in) and provide enhanced, more personal features.

These items help the website operator understand how its website performs, how visitors interact with the site, and whether there may be technical issues.

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.