1 of 12

Kotlin:

The Swift of Android

Mike Gouline

@mikestruct

2 of 12

Unfair!

iOS gets a new language, why can’t Android?

3 of 12

Problems with Java 7

  • Missing modern features
    • Lambdas
  • Crash-prone
    • NullPointerException
  • Boilerplate code

4 of 12

Current options

  • Scala/Groovy
    • Huge runtime
    • Lacking full Android support
  • Xamarin/RubyMotion
    • Not fully native
    • Expensive
  • And...

5 of 12

Kotlin

  • Island near St. Petersburg, Russia
  • Programming language
    • Made by JetBrains (IntelliJ IDEA, Android Studio)
    • Runs on the JVM
    • Built into Android Studio (and IntelliJ IDEA)
    • Light, compact and modern

6 of 12

Named/optional arguments

// Argument ‘stroke’ is optional

fun circle(x: Int, y: Int, rad: Int, stroke: Int = 1) {

...

}

// Arguments are named and ‘stroke’ defaults to 1

circle(x = 0, y = 0, rad = 5)

7 of 12

Lambdas

// Remove even integers from a list: (Int) -> Boolean

val list = list(1, 2, 3, 4, 5)

print(list.filter {it % 2 == 0}) // Output: [ 2, 4 ]

// Add a click event to a button: (View?) -> Unit

val btnGo = findViewById(R.id.btn_go) as Button

btnGo?.setOnClickListener({ v ->

doSomething()

})

8 of 12

NullPointerException-safe

var str1: String? = null

str1?.trim() // trim() doesn’t run on a null

str1 = “Not null anymore”

str1?.trim() // Now trim() does run

val str2: String = “I am not null”

str2.trim() // No need for “?.” and trim() runs

9 of 12

Data objects

// Complete class definition, no omissions!

data class Island(val name: String? = null)

val island = Island(“Kotlin”)

print(island.toString()) // Output: Island(name=Kotlin)

var copy = Island(“Kotlin”)

print(island.equals(copy)) // Output: true

10 of 12

Extension functions

// Extension to String (think Objective-C categories)

fun String.encodeSpaces(): String {

return this.replaceAll(“ “, “_”)

}

print(“these are spaces”.encodeSpaces())

// Output: these_are_spaces

11 of 12

Cost (version 0.8.11)

  • Method count - up to 6,063
    • Guava (18.0-rc1) - 14,835
    • Google Play Services (5.0.77) - 20,298
  • Package size - up to 341 KB
  • ProGuard usually decreases both by ~90%

12 of 12

Thank you!