Slide 1

Slide 1 text

Introduction to Kotlin - Android Workshop

Slide 2

Slide 2 text

+AkshayChordiya ● Indie Android Developer ● Serial blogger and speaker ● Graduated in 2016 Akshay Chordiya @aky

Slide 3

Slide 3 text

● A Computer program writer ● Love writing tech blogs and contributing back to community ● Mentor college graduates and professional who wants to go for mobile app development ● Visit Hardik Trivedi GitHub Blog

Slide 4

Slide 4 text

● Why Kotlin? ● Problems with Java - Android ● What’s Kotlin? ● Syntax ● Features ● Demo ● Issues ● And finally… questions Agenda

Slide 5

Slide 5 text

Why Kotlin?

Slide 6

Slide 6 text

Kotlin is official language for Android

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Problems with Java -- Android ● Stuck at Java 6-7; Java 8 has now come to rescue ● Restrictions to add functionality to Platform APIs ● Android loves Inheritance ● Lots of boilerplate ● Ceremony of APIs ● Android loves to throw nulls ● Lack of Modern coding style ● And so much more….

Slide 9

Slide 9 text

What’s otlin?

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Kotlin ● Modern & Statically typed ● Targets JVM, Android and JavaScript ● Works everywhere where Java works ● interoperable with Java™ ● Combines Object Oriented and Functional features ● Made by good folks at JetBrains ● Open source

Slide 12

Slide 12 text

Big Players using Kotlin

Slide 13

Slide 13 text

Little bit about the Syntax

Slide 14

Slide 14 text

// String (Implicitly inferred) val spell = "Lumos" // Int val number = 74 // Explicit type var spells: List = ArrayList() Variables // String (Implicitly inferred) val spell = "Lumos" // Int val number = 74 // Explicit type var spells: List = new ArrayList()

Slide 15

Slide 15 text

“Use val for a value that won’t change” - Immutable val vs var “Use var for something that will vary with time.” - Mutable

Slide 16

Slide 16 text

val max = if (a > b) a else b Control Flow val max = if (a > b) { a } else { b }

Slide 17

Slide 17 text

when (x) { in 1..10 -> print("x is in") !in 10..20 -> print("x is outside") else -> print("To infinity & beyond") } when expression when { x.isOdd() -> print("x is Odd") x.isEven() -> print("x is Even") else -> print("x is different") }

Slide 18

Slide 18 text

// De-structured for ((index, value) in avengers.withIndex()) { println("$value at $index") } Loops val avengers = listOf("Iron Man","Thor") // For-each for (avenger in avengers) { println(avenger) }

Slide 19

Slide 19 text

fun add(a: Int, b: Int): Int { return a + b } Functions fun add(a: Int, b: Int) = a + b fun add(a: Int = 0, b: Int = 0) = a + b

Slide 20

Slide 20 text

Class // Define class Spell(name: String = "") // Usage val spell = Spell()

Slide 21

Slide 21 text

class Spell(name: String = "") { var code: Int = 0 constructor(name: String, code: Int): this(name) { this.code = code } } Class

Slide 22

Slide 22 text

class Spell(name: String = "") { var code: Int = 0 init { code = generateCode(name) } } Class

Slide 23

Slide 23 text

Data Class data class Spell(var name: String = "")

Slide 24

Slide 24 text

Singleton in Kotlin object MagicWand { var spell: String? = null } // Class is initialized MagicWand.spell = "Alohomora" // Prints the value println(MagicWand.spell)

Slide 25

Slide 25 text

Features

Slide 26

Slide 26 text

Null Safety

Slide 27

Slide 27 text

// Compile time error val spell: String = null // OK val spell: String? = null Declaring null variables

Slide 28

Slide 28 text

// Compile time error var spell: String = "Lumos" spell = null // OK var spell: String? = "Lumos" spell = null Even...

Slide 29

Slide 29 text

// Compile time error val spell: String? = null val length = spell.length // OK val spell: String? = null if (spell != null) { // Smart cast val length = spell.length } Null Safety

Slide 30

Slide 30 text

// Safe val spell: String? = null val length = spell?.length // Safe with else val length = spell?.length ?: -1 Kotlin-ized way

Slide 31

Slide 31 text

String interpolation

Slide 32

Slide 32 text

String interpolation fun sayHello(message: String) { println("Welcome $message") } fun sayTime() { println("Time: ${System.currentTimeMillis()}") }

Slide 33

Slide 33 text

Lambda Expressions

Slide 34

Slide 34 text

mButton.setOnClickListener { // Your logic } Lambda Expressions mButton.setOnClickListener { // Using the view parameter v -> v.visibility = GONE }

Slide 35

Slide 35 text

fun consume(f: () -> Unit): Boolean { f() return true } Higher Order Functions inline fun consume(f: () -> Unit): Boolean { f() return true }

Slide 36

Slide 36 text

Extension functions

Slide 37

Slide 37 text

fun View.visible() { visibility = VISIBLE } fun View.visible() { visibility = VISIBLE } fun String.isEmail(): Boolean { return Patterns.EMAIL_ADDRESS.matcher(this).matches() } fun String.isEmail(): Boolean { return Patterns.EMAIL_ADDRESS.matcher(this).matches() } Example: Extension functions

Slide 38

Slide 38 text

val email = "[email protected]" if (email.isEmail()) { // Do login } Usage: Extension Function textView.visible()

Slide 39

Slide 39 text

inline fun SharedPreferences. edit(func: SharedPreferences.Editor.() -> Unit) { val editor = edit() editor.func() editor.apply() } inline fun SharedPreferences. edit(func: SharedPreferences.Editor.() -> Unit) { val editor = edit() editor.func() editor.apply() } Extension + Higher Order functions // Usage val pref = PreferenceManager.getDefaultSharedPreferences(context) pref.edit{ putString("first_name", "Akshay") putString("last_name", "Chordiya") remove("last_name") }

Slide 40

Slide 40 text

Smart Cast // Java way if (view instanceof TextView) { ((TextView) view).setText("Avifors"); } // Kotlin way if (view is TextView) { // Smart casting view.text = "Avifors" }

Slide 41

Slide 41 text

Ninja functions

Slide 42

Slide 42 text

let block textView?.let { textView.visible() }

Slide 43

Slide 43 text

apply block textView?.apply { visible() }

Slide 44

Slide 44 text

with function with(textView) { text = "Accio" visible() } val textView = with(TextView(this)) { text = "Accio" visible() this }

Slide 45

Slide 45 text

Let’s code!

Slide 46

Slide 46 text

Issues

Slide 47

Slide 47 text

Issues ● Slightly increased build time ● Adds Dex Count (6K) to your app ● No static methods and variables. The same functionality can be achieved using companion objects ● Difficulty in coding in functional paradigm

Slide 48

Slide 48 text

Conclusion

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

What’s next?

Slide 53

Slide 53 text

Questions?

Slide 54

Slide 54 text

References ● All the images and logos used are trademarks of respective companies. ● Kotlin Official Website ● Why Kotlin-ize your Android Development Medium - Akshay Chordiya ● Antonio Leiva Blog ● Why You Should Start Using Kotlin to Supercharge Your Android Development in 2017 ● Android Development with Kotlin — Jake Wharton ● Kotlin - Android document by Square ● Droidcon NYC 2016 - Kotlin in Production ● Being more productive with Kotlin

Slide 55

Slide 55 text

Thank you