Slide 1

Slide 1 text

Getting familiar with Kotlin for Android

Slide 2

Slide 2 text

+AkshayChordiya ● Entrepreneur ● Android Developer by heart ● Active community speaker ● Graduated in 2016 Akshay Chordiya @Akshay_Chordiya

Slide 3

Slide 3 text

Agenda ● What’s Kotlin? ● Syntax ● Features ● Live Demos ● Issues ● What’s next? ● And finally… questions

Slide 4

Slide 4 text

Why Kotlin?

Slide 5

Slide 5 text

Kotlin is official language for Android

Slide 6

Slide 6 text

What’s otlin?

Slide 7

Slide 7 text

“Kotlin is a new programming language targeting JVM, Android and even JavaScript.”

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Syntax Crash course

Slide 10

Slide 10 text

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

Slide 11

Slide 11 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 12

Slide 12 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 13

Slide 13 text

Features

Slide 14

Slide 14 text

Null Safety

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 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 18

Slide 18 text

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

Slide 19

Slide 19 text

Lambda Expressions

Slide 20

Slide 20 text

// Kotlin mButton.setOnClickListener { // Your logic } Lambda Expressions // Before Java 8 mButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Your logic } });

Slide 21

Slide 21 text

Extension functions

Slide 22

Slide 22 text

Usage: Extension Function toast(R.string.magic_spell) snackbar(R.id.content, R.string.magic_spell)

Slide 23

Slide 23 text

fun Activity.toast(resId: Int, duration: Int = Toast.LENGTH_SHORT) { Toast.makeText(this, getString(resId), duration).show() } Code: Extension functions

Slide 24

Slide 24 text

Higher Order functions

Slide 25

Slide 25 text

Usage: Higher Order Functions // We want fragmentManager.load { it.replace(R.id.home_content, MyFragment.newInstance()) } // Old way val trans = fragmentManager.beginTransaction() trans.replace(R.id.home_content, MyFragment.newInstance()) trans.commit()

Slide 26

Slide 26 text

Code: Higher Order Functions fun FragmentManager.load(f: (FragmentTransaction) -> Unit) { val transaction = beginTransaction() f(transaction) transaction.commit() } inline fun FragmentManager.load(f: (FragmentTransaction) -> Unit) { val transaction = beginTransaction() f(transaction) transaction.commit() }

Slide 27

Slide 27 text

Extension functions with receiver

Slide 28

Slide 28 text

Usage: Extension functions with receiver // Previously fragmentManager.load { it.replace(R.id.home_content, MyFragment.newInstance()) } // We want fragmentManager.load { replace(R.id.home_content, MyFragment.newInstance()) }

Slide 29

Slide 29 text

Code: Extension functions with receiver inline fun FragmentManager.load(f: FragmentTransaction.() -> Unit) { val transaction = beginTransaction() transaction.f() transaction.commit() }

Slide 30

Slide 30 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 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

Easter functions

Slide 34

Slide 34 text

let block textView?.let { it.text = "Easter" it.textSize = 24F }

Slide 35

Slide 35 text

apply block textView?.apply { text = "Easter" textSize = 24F }

Slide 36

Slide 36 text

measureTimeMillis function // Java long startTime = System.currentTimeMillis(); // Do your task long executionTime = System.currentTimeMillis() - startTime; System.out.println("Execution Time = " + executionTime + "ms"); // Kotlin val executionTime = measureTimeMillis { // Do your task } println("Execution Time = $executionTime ms")

Slide 37

Slide 37 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 38

Slide 38 text

Issues

Slide 39

Slide 39 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 40

Slide 40 text

Conclusion

Slide 41

Slide 41 text

“With Kotlin our code base is shrinked down.”

Slide 42

Slide 42 text

“Plenty of app crashes are cut down”

Slide 43

Slide 43 text

”Kotlin makes Android development a lot more fun again”

Slide 44

Slide 44 text

What’s next?

Slide 45

Slide 45 text

Goodbye findViewById

Slide 46

Slide 46 text

Data classes

Slide 47

Slide 47 text

#ProTips ● Always remember with Kotlin there is always gonna be a better way to do something ● Use Kotlin to Java converter to convert Java files to Kotlin (Shortcut: Ctrl + Alt + Shift + K) ● You can begin writing test cases or data classes for your models to slowly start adding Kotlin to your project

Slide 48

Slide 48 text

Questions?

Slide 49

Slide 49 text

References ● All the images and logos used are trademarks of respective companies. ● Kotlin Official Website ● Kotlin and Android ● Why Kotlin-ize your Android Development Medium - Akshay Chordiya ● Antonio Leiva Blog ● Android Development with Kotlin — Jake Wharton ● Easter Functions in Kotlin ● Being more productive with Kotlin ● Kotlin - Goodbye findViewById

Slide 50

Slide 50 text

Thank you #HappyCoding