Skip to content

Using Kotlin for Android development

Roger Hu edited this page Jun 20, 2020 · 22 revisions

What is Kotlin?

Kotlin is a language by JetBrains, the company behind IntelliJ IDEA, which Android Studio is based on, and other developer tools. Kotlin is purposely built for large scale software projects to improve upon Java with a focus on readability, correctness, and developer productivity.

The language was created in response to limitations in Java which were hindering development of JetBrains' software products and after an evaluation of all other JVM languages proved unsuitable. Since the goal of Kotlin was for use in improving their products, it focuses very strongly on interop with Java code and the Java standard library.

Why Kotlin?

  • 100% interoperable with Java - Kotlin and Java can co-exist in one project. You can continue to use existing libraries in Java.
  • Concise - Drastically reduce the amount of boilerplate code you need to write.
  • Safe - Avoid entire classes of errors such as null pointer exceptions.
  • It's functional - Kotlin uses many concepts from functional programming, such as lambda expressions.

Take a look at this cheatsheet and quick reference.

Syntax Crash Course

Variables

Defining local variables

Assign-once (read-only) local variable:

val a: Int = 1
val b = 1   // `Int` type is inferred
val c: Int  // Type required when no initializer is provided
c = 1       // definite assignment
int a = 1;
int b = 1;
int c;
c = 1;

Mutable variable:

var x = 5 // `Int` type is inferred
x += 1

Functions

Function having two Int parameters with Int return type:

fun sum(a: Int, b: Int) :Int {
	return a + b
}

Function with an expression body and inferred return type:

fun sum(a: Int, b: Int) = a + b

Function returning no meaningful value:

fun printSum(a: Int, b: Int): Unit {
  print(a + b)
}

Unit return type can be omitted:

fun printSum(a: Int, b: Int) {
  print(a + b)
}

Using collections

Iterating over a collection:

for (name in names)
  println(name)

Checking if a collection contains an object using in operator:

if (text in names) // names.contains(text) is called
  print("Yes")

Using lambda expressions to filter and map collections:

names
    .filter { it.startsWith("A") }
    .sortedBy { it }
    .map { it.toUpperCase() }
    .forEach { print(it) }

Null Safety

val x: String? = "Hi"
x.length // Won't compile
val y: String = null // Won't compile

Dealing with null

// using the safe call operator ?.
x?.length // This returns x.length if x is not null, and null otherwise

// Elvis Operator ?:
val len = x?.length ?: -1 // This will return -1 if x is null

Configure your development environment

Recent version of Android Studio now provide Kotlin as the default language when creating new projects. Just make sure to leave the default programming language as Kotlin!

Your build.gradle file will look like this example:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.example.hellokotlin"
        minSdkVersion 10
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    implementation 'com.android.support:appcompat-v7:23.4.0'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
repositories {
    jcenter()
}

Note the implementation reference of org.jetbrains.kotlin:kotlin-stdlib-jdk7. For more understanding about the differences between kotlin-stdlib, kotlin-sdklib-jdk7, and kotlin-sdklib-jdk8, see this link for more information.

Writing your first Kotlin Code

You can start by converting your existing Java File to Kotlin file. Open your Java File -> Click on Code menu item -> select Convert Java File to Kotlin File. Your converted file would look like this:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

}

Starting from Kotlin version 1.0.2, action to create new activity in Kotlin has been added. To create new Android Kotlin activity, Go to File -> New->Kotlin Activity.

Attribution

This guide was originally put together by Kirk Saviour (@savekirk) as referenced on this thread.

References

Finding these guides helpful?

We need help from the broader community to improve these guides, add new topics and keep the topics up-to-date. See our contribution guidelines here and our topic issues list for great ways to help out.

Check these same guides through our standalone viewer for a better browsing experience and an improved search. Follow us on twitter @codepath for access to more useful Android development resources.

Clone this wiki locally