Kotlin- The Next-Gen Android App Development Language

Android app development is a major part of today’s technical and economical world. People use different languages for developing apps, though Java remains the most used language to develop logic. While running a Java application, the app is compiled into a set of instructions called Bytecode and runs in a virtual machine. In last few years, new programming languages have been introduced which also run on Java virtual machine. The reason behind using these languages is that the resulting app looks same for the virtual machine with simple code and some fixes that developers face while coding in Java. Image result for kotlin   Recently, one such language is introduced by JetBrains, known for IntelliJ IDEA (Android Studio is based on IntelliJ), called Kotlin. It is a statically typed programming language that runs on the JVM. It can be compiled to JavaScript source code. It has many other amazing features, and we will be knowing about them in this article.

Why Kotlin instead of any other language?

Since Java is the only language most of the people use while developing the mobile apps, developers had no actual alternatives to Java app. Java comes with a lot of historical baggage and some issues. Java 8 solved some of those problems, and some were corrected in Java 10. But these two options also have loopholes. One of them is in order to use these two versions, one must have the minimum SDK to Android 24 to use Java 8 which is not an option for many developers. And Java 10 is even far from their reach. So, Kotlin was made to fill these gaps through model language. There are some core principles that Kotlin work with-
  1. Concise- Reduce amount of boilerplate code
  2. Safe- Avoid entire classes of error such as null pointer
  3. Smart- Make your code more readable and understandable
  4. Tool-friendly- One can choose any Java IDE or build from the command line
  5. Interoperable- Leverage existing frameworks and libraries of JVM, Android, and browser.

How to work on Kotlin?

Download the starter project. Extract and open the starter project in Android Studio. It is a simple app that allows users to search for books, book covers and share books with friends to explore Kotlin. Image result for kotlin vs java

It consist of three source code files, let us check it out-

MainActivity.java- an activity that displays the screen for searching and displaying a list of books. DetailActivity.java- an Activity that displays the book cover for the ID passed to it. JSONAdapter.java- a custom BaseAdapter that transforms a JSON object into a listview item. Build and run the project to see what you’re working with.

How to set up the environment of Kotlin?

Android Studio doesn’t have any idea of what to do with Kotlin. So the first thing you need to do is to install the Kotlin plugin and configure Kotlin in your project.

-Installing the Plugin

1. Go to Android Studio/ Preferences and Select the Plugins entry. kotlin for android 32. Click on Install JetBrains plugin on Plugins screen intro_to_kotlin_4 3. Search and select Kotlin from the list and click Install. intro_to_kotlin_5 4. When you are done with downloading and installing Kotlin, then you need to follow the prompts to restart the IDE. intro_to_kotlin_28

– Configure Kotlin in Project

Now the IDE knows what to do with Kotline, but your project app doesn’t, so you have to modify the project’s build configuration. How do modify the project’s build configuration? Let us check out- 1. Go to Tools-> Kotlin-> Configure Koltin in Project intro_to_kotlin_7 2. A Choose Configurator pop up will appear, select Android with Gradle from the drop down. intro_to_kotlin_8 3. On Configure Kotlin in Project popup, select the plugin version you want to use and click OK. Configure Kotlin in Project 4. These actions will make some changes to your build.gradle files.   build.gradle (Project: omg-android-starter): buildscript {  ext.kotlin_version = ‘1.0.3’ // 1  repositories {    jcenter()  }  dependencies {    classpath ‘com.android.tools.build:gradle:2.1.3′    classpath “org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version // 2    // NOTE: Do not place your application dependencies here; they belong    // in the individual module build.gradle files  } } allprojects {  repositories {    jcenter()  } } build.gradle (Module: OMG Android):    apply plugin: ‘com.android.application’ apply plugin: ‘kotlin-android’ // 3 android {    compileSdkVersion 23    buildToolsVersion “24.0.2”    defaultConfig {        minSdkVersion 14        targetSdkVersion 23    }  sourceSets {    main.java.srcDirs += ‘src/main/kotlin’ // 4  } } dependencies {  compile ‘com.android.support:appcompat-v7:23.2.0′  compile ‘com.loopj.android:android-async-http:1.4.4′  compile ‘com.squareup.picasso:picasso:2.1.1′  compile “org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version // 5 } repositories {  mavenCentral() }   Here is a summary of what this code is about-  
  1. Declares configured Kotlin version
  2. Declares classpath dependency artifact that contains the Kotlin Gradle plugin with the version declared earlier
  3. Specifies the use of Kotlin Android plugin via apply plugin command
  4. Defines that source files found in src/main/Kotlin will be compiled. Gradle will compile source Kotlin files found in src/main/java.
  5. Added the Kotlin Standard library as a compile-time dependency to the project.
    Click on Sync Now to build a project and run it! intro_to_kotlin_25 Nothing changed visually here, it just made your work bit easier!

Here are 10 reason why Kotlin is cool-

Image result for kotlin
  1. Kotlin is open source
  2. Full Java Interoperability
  3. Kotlin compiles to JVM bytecode or JS
  4. Data Classes in Kotlin
  5. Defaulted parameters
  6. Kotlin imposes no runtime overhead
  7. Null Safety in Kotlin
  8. Extension functions
  9. Swift is like Kotlin
  10. Kotlin wants you to write less code
Inputs & Source