Slide 1

Slide 1 text

Kotlin for Android A first approach Carlos Daniel Munoz - Android Dev @cdmunozi [email protected] Co-Founder & Co-Organizer @MedellinAndroid May 23 VANCOUVER’S KOTLIN NIGTH #1

Slide 2

Slide 2 text

Why Kotlin in Android?

Slide 3

Slide 3 text

3 Java in Android

Slide 4

Slide 4 text

4 Java in Android ● Java 6.5 (Dic. 2006)

Slide 5

Slide 5 text

5 Java in Android ● Java 6.5 (Dic. 2006) ● Some hard to avoid limitations: Compatibility with past versions of Android

Slide 6

Slide 6 text

6 Java in Android ● Java 6.5 (Dic. 2006) ● Some hard to avoid limitations: Compatibility with past versions of Android ● “Compatibility” with some Java 8 features ○ Virtual Machine Java 6 :( ○ Not ready to support Java 8

Slide 7

Slide 7 text

7 Java in Android ● Java 6.5 (Dic. 2006) ● Some hard to avoid limitations: Compatibility with past versions of Android ● “Compatibility” with some Java 8 features ○ Virtual Machine Java 6 :( ○ Not ready to support Java 8 ● Java 8 is limited, compared to other modern languages

Slide 8

Slide 8 text

8 Problems with Java ● Are we safe with nulls? ○ This compiles perfectly

Slide 9

Slide 9 text

9 Problems with Java ● Are we safe with nulls? ○ This compiles perfectly

Slide 10

Slide 10 text

10 Problems with Java ● Immutability not well implemented

Slide 11

Slide 11 text

11 Problems with Java ● Immutability not well implemented ○ Variables can be modified easily

Slide 12

Slide 12 text

12 Problems with Java ● Immutability not well implemented ○ Variables can be modified easily ○ “Race condition” → who is modifying my variable? testing?

Slide 13

Slide 13 text

13 Problems with Java ● Immutability not well implemented ○ Variables can be modified easily ○ “Race condition” → who is modifying my variable? testing? ○ We can define everything “final”, but it’s hard to use.

Slide 14

Slide 14 text

14 Problems with Java ● Immutability not well implemented ○ Variables can be modified easily ○ “Race condition” → who is modifying my variable? testing? ○ We can define everything “final”, but it’s hard to use. ○ Java framework and libraries use to be mutable. (i.e: Collections and Collections Utils)

Slide 15

Slide 15 text

15 Problems with Java ● First class citizen functions

Slide 16

Slide 16 text

16 Problems with Java ● First class citizen functions ○ Feature supported by Java 8, but in Java 7 there is no support for lambdas and functions as parameters or as a return type

Slide 17

Slide 17 text

17 Problems with Java ● First class citizen functions ○ Feature supported by Java 8, but in Java 7 there is no support for lambdas and functions as parameters or as a return type ○ Functional programming ○ Listeners ○ Callbacks

Slide 18

Slide 18 text

18 Problems with Java ● First class citizen functions ○ Feature supported by Java 8, but in Java 7 there is no support for lambdas and functions as parameters or as a return type ○ Functional programming ○ Listeners ○ Callbacks

Slide 19

Slide 19 text

19 Problems with Java ● First class citizen functions ○ Feature supported by Java 8, but in Java 7 there is no support for lambdas and functions as parameters or as a return type ○ Functional programming ○ Listeners ○ Callbacks

Slide 20

Slide 20 text

20 Problems with Java ● Data classes?

Slide 21

Slide 21 text

21 Problems with Java ● Data classes?

Slide 22

Slide 22 text

22 Problems with Java ● Data classes?

Slide 23

Slide 23 text

23 Problems with Java ● It’s complex to implement a safe singleton

Slide 24

Slide 24 text

24 Problems with Java ● It’s complex to implement a safe singleton ● Object means, class with an unique implementation → Singleton

Slide 25

Slide 25 text

25 Problems with Java ● Is Java obsolete?

Slide 26

Slide 26 text

26 Problems with Java ● Is Java obsolete? ○ Lambdas ○ Functional programming ○ Sealed classes

Slide 27

Slide 27 text

27 Problems with Java ● Is Java obsolete? ○ Lambdas ○ Functional programming ○ Sealed classes ● With Kotlin, can be used without third libraries and even using Java 6’s VM

Slide 28

Slide 28 text

28 Problems with Java ● Is Java obsolete? ○ Lambdas ○ Functional programming ○ Sealed classes ● With Kotlin, can be used without third libraries and even using Java 6’s VM ● 100% interoperable with Java

Slide 29

Slide 29 text

29 Problems with Java ● Is Java obsolete? ○ Lambdas ○ Functional programming ○ Sealed classes ● With Kotlin, can be used without third libraries and even using Java 6’s VM ● 100% interoperable with Java ● Supported by Google for Android

Slide 30

Slide 30 text

Let’s use Kotlin!

Slide 31

Slide 31 text

31 Using Kotlin ● Android Studio

Slide 32

Slide 32 text

32 Using Kotlin ● Android Studio ● Plugin Kotlin ○ Projects from scratch ○ Java projects with some Kotlin

Slide 33

Slide 33 text

33 Using Kotlin ● Android Studio ● Plugin Kotlin ○ Projects from scratch ○ Java projects with some Kotlin ● Lambda expressions

Slide 34

Slide 34 text

34 Using Kotlin ● Android Studio ● Plugin Kotlin ○ Projects from scratch ○ Java projects with some Kotlin ● Lambda expressions ● Inline functions (A new object is not created, the code is replaced at runtime)

Slide 35

Slide 35 text

35 Using Kotlin ● Android Studio ● Plugin Kotlin ○ Projects from scratch ○ Java projects with some Kotlin ● Lambda expressions ● Inline functions (A new object is not created, the code is replaced at runtime) ● Null safety

Slide 36

Slide 36 text

36 Using Kotlin ● Android Studio ● Plugin Kotlin ○ Projects from scratch ○ Java projects with some Kotlin ● Lambda expressions ● Inline functions (A new object is not created, the code is replaced at runtime) ● Null safety ● Smart cast

Slide 37

Slide 37 text

37 Using Kotlin ● Android Studio ● Plugin Kotlin ○ Projects from scratch ○ Java projects with some Kotlin ● Lambda expressions ● Inline functions (A new object is not created, the code is replaced at runtime) ● Null safety ● Smart cast ● String templates

Slide 38

Slide 38 text

38 Using Kotlin ● Android Studio ● Plugin Kotlin ○ Projects from scratch ○ Java projects with some Kotlin ● Lambda expressions ● Inline functions (A new object is not created, the code is replaced at runtime) ● Null safety ● Smart cast ● String templates ● Primary constructors

Slide 39

Slide 39 text

39 Using Kotlin ● Interfaces with code

Slide 40

Slide 40 text

40 Using Kotlin ● Interfaces with code ● Needless: ○ Semicolon ○ “new” ○ Types definitions

Slide 41

Slide 41 text

41 Kotlin Updates ● Google IO 2017 → Google Supports Kotlin for Android Dev REFERENCES: ● Android Developers: https://developer.android.com/kotlin/resources.html ● Kotlin Online https://try.kotlinlang.org ● Code Labs Google: https://codelabs.developers.google.com/?cat=Android

Slide 42

Slide 42 text

42 Kotlin project in Android from scratch

Slide 43

Slide 43 text

43 Kotlin project in Android step by step

Slide 44

Slide 44 text

44 Kotlin project in Android step by step ● Create project in the usual way

Slide 45

Slide 45 text

45 Kotlin project in Android step by step ● Create project in the usual way ● Browse the plugins (Jetbrains) and install Kotlin plugin

Slide 46

Slide 46 text

46 Kotlin project in Android step by step ● Create project in the usual way ● Browse the plugins (Jetbrains) and install Kotlin plugin ● Restart the IDE

Slide 47

Slide 47 text

47 Kotlin project in Android step by step ● Create project in the usual way ● Browse the plugins (Jetbrains) and install Kotlin plugin ● Restart the IDE ● Open the MainActivity ○ Code - Convert Java File to Kotlin File ○ Magic!

Slide 48

Slide 48 text

48 Kotlin project in Android step by step ● Create project in the usual way ● Browse the plugins (Jetbrains) and install Kotlin plugin ● Restart the IDE ● Open the MainActivity ○ Code - Convert Java File to Kotlin File ○ Magic! ● Tools → Kotlin → Configure Kotlin in project → Version ○ Project full modified (including build.gradle - both, app and project)

Slide 49

Slide 49 text

49 Kotlin project in Android step by step ● Create project in the usual way ● Browse the plugins (Jetbrains) and install Kotlin plugin ● Restart the IDE ● Open the MainActivity ○ Code - Convert Java File to Kotlin File ○ Magic! ● Tools → Kotlin → Configure Kotlin in project → Version ○ Project full modified (including build.gradle - both, app and project) ● App build.gradle (add android extension sub-plugin): to access easily view’s elements from Activity ○ apply plugin: ‘kotlin-android-extension’

Slide 50

Slide 50 text

50 A Kotlin Activity ● No public reserved word, in Kotlin everything is public ● Inheritance ● Override as a reserved word rather than an annotation ● In function: first name, then the type ● Null safety with “?”

Slide 51

Slide 51 text

51 Activity and On-Click listener in Kotlin * Taken from https://developer.android.com/kotlin/get-started.html

Slide 52

Slide 52 text

52 Item click listener in Kotlin * Taken from https://developer.android.com/kotlin/get-started.html

Slide 53

Slide 53 text

53 A Kotlin Example ● https://github.com/cdmunoz/KotlinTrainingApp

Slide 54

Slide 54 text

54 Questions?