Kotlin in real Android project

Anton Rutkevich

JunoLab

Why?

We need

safe &

readable

code

What's interesting in

Kotlin

Libraries

?

Kotlin

val / var, not null

// Constant
val a: Int = 1
// Variable
var b: Int = 2
b++

// Nullable variable
var c: String? = null
c?.charAt(0) // returns Char?

( Demo 1 )

Fighting nulls!

Collections

val list: List<String> 
    = arrayListOf("1", "2", "3")

list.add("4") // No such method :)


val mutableList: MutableList<String> 
    = arrayListOf("4", "5", "6")

mutableList.add("7") // OK

Lambdas

// Lambda instead of anonymous class

val button: Button = ...
button.setOnClickListener {
    // Do something
}

Collections + lambdas !

val list = 
    = arrayListOf("one", "two", "three")

list.filter { 
    it.length() < 5 
}.map { 
    it.reverse()
}.forEach {
    Log.d(TAG, "String: ${it}")
}

Data classes

data class Purchase (
        val url: String,
        val id: String,
        val amount: Int = 1
)

// Inside a method...
purchase = purchase.copy(amount = newAmount)

Return if / when / try

fun textColorRes(): Int {
    return if (isValid()) {
        R.color.green
    } else {
        R.color.red
    }
}

Some notes on

properties

public

protected

open

ternary operator :(

Libraries

Must make code easier to

write

read

test

and safer!

We are using

RxJava

Dagger 2

Retrofit

Gson

and some others

RxJava
is awesome!

( Demo 2 )

Dagger 2 and dependency injection

( Demo 3 )

Retrofit

just works :)

Gson, make it safe!

( Demo 4 )

Fun :)

Upgrade M11 to M12

Build.VERSION_CODES

Question? 

Anton Rutkevich,

JunoLab

Made with Slides.com