Slide 1

Slide 1 text

Kotlin-ize your Android Development

Slide 2

Slide 2 text

Akshay Chordiya +AkshayChordiya ● Indie Android Developer ● Graduated in 2016 Who am I?

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

Why another language?

Slide 5

Slide 5 text

“Java is old, too much verbose and don’t forget the infamous NullPointerException“

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

What’s otlin?

Slide 8

Slide 8 text

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

Slide 9

Slide 9 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 10

Slide 10 text

Big Players using Kotlin

Slide 11

Slide 11 text

Little bit about the Syntax

Slide 12

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

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

Slide 14 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 15

Slide 15 text

Features

Slide 16

Slide 16 text

Null Safety

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 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 20

Slide 20 text

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

Slide 21

Slide 21 text

String interpolation

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

Lambda Expressions

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

Extension functions

Slide 27

Slide 27 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 28

Slide 28 text

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

Slide 29

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

Ninja functions

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

apply block textView?.apply { visible() }

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

when expression override fun onOptionsItemSelected (item: MenuItem) = when (item.itemId) { R.id.action_about -> consume { startActivity(Intent( baseContext, AboutActivity:: class.java)) } else -> super.onOptionsItemSelected(item) }

Slide 36

Slide 36 text

Issues

Slide 37

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

Slide 38 text

Conclusion

Slide 39

Slide 39 text

“With Kotlin our code base is shrinked down.”

Slide 40

Slide 40 text

“Plenty of app crashes are cut down”

Slide 41

Slide 41 text

”Kotlin makes Android development a lot more fun again”

Slide 42

Slide 42 text

What’s next?

Slide 43

Slide 43 text

Questions?

Slide 44

Slide 44 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 45

Slide 45 text

Thank you #HappyCoding