Why Kotlin For Android?
Since Android took the world by storm, developers have had no alternatives to Java app development. Although its usage is widespread, Java comes with a lot of historical baggage.
Java 8 solved some language issues and corrected even more with Java 10. In order to get much of a benefit from the corrections made in these two releases, you have to set the minimum SDK to Android 24 just to use Java 8, which isn’t an option for many developers. For almost everybody, Java 10 isn’t even on the radar.
Kotlin aims to fill that gap of a missing modern language for the Android platform. There are a few core tenets that Kotlin lives by; it strives to be:
1.Concise to reduce the amount of boilerplate code you need to write.
2.Expressive to make your code more readable and understandable.
3.Safe to avoid entire classes of errors such as null pointer exceptions.
4.Versatile for building server-side applications, Android apps or frontend code running in the browser.
5.Interoperable to leverage existing frameworks and libraries of the JVM with 100 percent Java interoperability.
Above all, it’s a new language! What could be more exciting? iOS developers can’t have all the fun. :]
Setting up Your Environment
By default, Android Studio has no idea what to do with Kotlin, so the first step is to install the Kotlin plugin and configure Kotlin in your project.
Installing the Kotlin Plugin
Go to Android Studio\Preferences and select the Plugins entry.
On the Plugins screen, click on Install JetBrains plugin…
Search for and select Kotlin from the list and click Install.
When you’re finished with downloading and installing, the next step is following the prompts to restart the IDE.
Configure Kotlin in Project
Now the IDE knows what to do with Kotlin, but your project app doesn’t, so your next move is to modify the project’s build configuration.
Go to Tools\Kotlin\Configure Kotlin in Project.
Select Android with Gradle from the Choose Configurator popup that appears.
On the Configure Kotlin in Project popup, select the plugin version you want to use (at the time of writing this tutorial, the current version is 1.0.3) and click OK.
This action will make a number of 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()
}