Slide 1

Slide 1 text

Kotlin in 2019 Pietro Maggi @pfmaggi

Slide 2

Slide 2 text

Kotlin

Slide 3

Slide 3 text

Language design is cast in stone, but this stone is reasonably soft, and with some effort we can reshape it later. Kotlin Design Team

Slide 4

Slide 4 text

Pragmatic Ergonomic Constant feedback loop

Slide 5

Slide 5 text

Kotlin ● Object Oriented and Functional ● Static typing with type inference ● Safe ● Concise

Slide 6

Slide 6 text

package hello; class HelloWorld { public static void main(String args[]) { System.out.println("Hello, World"); } } Kotlin ● Object Oriented and Functional ● Static typing with type inference ● Safe ● Concise

Slide 7

Slide 7 text

package hello fun main() { println("Hello World") } Kotlin ● Object Oriented and Functional ● Static typing with type inference ● Safe ● Concise

Slide 8

Slide 8 text

History Kotlin is announced Creation of Kotlin by Jetbrains 2011 2012 2013 2014 2015 2016 2017 2018 2019 ?

Slide 9

Slide 9 text

History Kotlin is announced Creation of Kotlin by Jetbrains 2011 2012 2013 2014 2015 2016 2017 2018 2019 ? Kotlin is released Development tools are open source

Slide 10

Slide 10 text

History Kotlin is announced Creation of Kotlin by Jetbrains 2011 2012 2013 2014 2015 2016 2017 2018 2019 ? Kotlin is released Development tools are open source Kotlin 1.0 First stable

Slide 11

Slide 11 text

History Kotlin is announced Creation of Kotlin by Jetbrains Kotlin 1.1 (March) JavaScript support Coroutines introduction 2011 2012 2013 2014 2015 2016 2017 2018 2019 ? Kotlin is released Development tools are open source Kotlin 1.0 First stable

Slide 12

Slide 12 text

History Kotlin is announced Creation of Kotlin by Jetbrains Kotlin 1.1 (March) JavaScript support Coroutines introduction 2011 2012 2013 2014 2015 2016 2017 2018 2019 ? Kotlin is released Development tools are open source Kotlin 1.0 First stable Kotlin 1.2 (November) Code sharing between platforms

Slide 13

Slide 13 text

History Kotlin is announced Creation of Kotlin by Jetbrains Kotlin 1.3 (October) Coroutines stable (JVM) Kotlin/Native beta Kotlin 1.1 (March) JavaScript support Coroutines introduction 2011 2012 2013 2014 2015 2016 2017 2018 2019 ? Kotlin is released Development tools are open source Kotlin 1.0 First stable Kotlin 1.2 (November) Code sharing between platforms

Slide 14

Slide 14 text

History Kotlin is announced Creation of Kotlin by Jetbrains Kotlin 1.3 (October) Coroutines stable (JVM) Kotlin/Native beta Kotlin 1.1 (March) JavaScript support Coroutines introduction 2011 2012 2013 2014 2015 2016 2017 2018 2019 ? Kotlin is released Development tools are open source Kotlin 1.0 First stable Kotlin 1.2 (November) Code sharing between platforms Kotlin 1.4 ?

Slide 15

Slide 15 text

Android SDK Java application development 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 Android Development

Slide 16

Slide 16 text

Android SDK Java application development 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 Android NDK C and C ++ support for games and cross-platform library/applications Android Development

Slide 17

Slide 17 text

Android SDK Java application development 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 Kotlin on Android Official support Android NDK C and C ++ support for games and cross-platform library/applications Android Development

Slide 18

Slide 18 text

Android SDK Java application development 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 Kotlin on Android Official support Android NDK C and C ++ support for games and cross-platform library/applications Android Development Kotlin First Kotlin-First APIs

Slide 19

Slide 19 text

Kotlin First

Slide 20

Slide 20 text

Kotlin First

Slide 21

Slide 21 text

Personal Kotlin’s Favorites

Slide 22

Slide 22 text

Null Safety Aims to eliminate NullPointerException

Slide 23

Slide 23 text

var a: String = "abc" a = null Null Safety Aims to eliminate NullPointerException

Slide 24

Slide 24 text

var a: String = "abc" a = null Null Safety Aims to eliminate NullPointerException > Null can not be a value of a non-null type String

Slide 25

Slide 25 text

var a: String = "abc" a = null Null Safety Aims to eliminate NullPointerException > Null can not be a value of a non-null type String var b: String? = "abc" b = null print(b)

Slide 26

Slide 26 text

var a: String = "abc" a = null Null Safety Aims to eliminate NullPointerException > Null can not be a value of a non-null type String var b: String? = "abc" b = null print(b) > null

Slide 27

Slide 27 text

Asynchronous Operations Coroutines are light-weight threads

Slide 28

Slide 28 text

import kotlinx.coroutines.* fun main() { GlobalScope.launch { delay(1000L) println("World!") } print("Hello, ") Thread.sleep(2000L) } Asynchronous Operations Coroutines are light-weight threads

Slide 29

Slide 29 text

import kotlinx.coroutines.* fun main() { GlobalScope.launch { delay(1000L) println("World!") } print("Hello, ") Thread.sleep(2000L) } Asynchronous Operations Coroutines are light-weight threads

Slide 30

Slide 30 text

import kotlinx.coroutines.* fun main() { GlobalScope.launch { delay(1000L) println("World!") } print("Hello, ") Thread.sleep(2000L) } Asynchronous Operations Coroutines are light-weight threads > Hello,

Slide 31

Slide 31 text

import kotlinx.coroutines.* fun main() { GlobalScope.launch { delay(1000L) println("World!") } print("Hello, ") Thread.sleep(2000L) } Asynchronous Operations Coroutines are light-weight threads > Hello, World!

Slide 32

Slide 32 text

import kotlinx.coroutines.* fun main() { GlobalScope.launch { delay(1000L) println("World!") } print("Hello, ") Thread.sleep(2000L) } Asynchronous Operations Coroutines are light-weight threads > Hello, World!

Slide 33

Slide 33 text

Extension Functions Add functionality to a type, without directly modifying the type’s definition

Slide 34

Slide 34 text

fun Byte.toUnsigned(): Int { return if (this < 0) this + 256 else this.toInt() } Extension Functions Add functionality to a type, without directly modifying the type’s definition

Slide 35

Slide 35 text

fun Byte.toUnsigned(): Int { return if (this < 0) this + 256 else this.toInt() } Extension Functions Add functionality to a type, without directly modifying the type’s definition val x: Byte = -1 println(x.toUnsigned()) > 255

Slide 36

Slide 36 text

Developer Experience

Slide 37

Slide 37 text

Android Studio Kotlin ● Kotlin plugin is part of Android Studio ● Lint checks & refactoring support ● Project templates and samples ● Kotlin-friendly Android SDK (nullability)

Slide 38

Slide 38 text

Android Studio Kotlin ● Kotlin plugin is part of Android Studio ● Lint checks & refactoring support ● Project templates and samples ● Kotlin-friendly Android SDK (nullability)

Slide 39

Slide 39 text

Kapt is incremental now

Slide 40

Slide 40 text

Android Studio Kotlin

Slide 41

Slide 41 text

R8 Kotlin

Slide 42

Slide 42 text

Kotlin’s Companion Object fun main() { println(Greeter.hello().greet("Olive")) } class Greeter(val greeting: String) { fun greet(name: String) = "$greeting, $name!" companion object { fun hello() = Greeter("Hello") } }

Slide 43

Slide 43 text

Companion Object… as seen from Java public final class Greeter { public static final Companion Companion = new Companion(); private final String greeting; public Greeter(String greeting) { this.greeting = greeting; } public String getGreeting() { return greeting; } public String greet(String name) { return greeting + ", " + name; } public static final class Companion { private Companion() {} public Greeter hello() { return new Greeter("Hello"); } } }

Slide 44

Slide 44 text

public final class Greeter { - public static final Companion Companion = new Companion(); private final String greeting; - public static final class Companion { - private Companion() {} - - public Greeter hello() { - return new Greeter("Hello"); - } - } + public static Greeter hello() { + return new Greeter("Hello"); + } public String greet(String name) { return greeting + ", " + name; } } Fortunately there’s R8

Slide 45

Slide 45 text

Kotlin’s Adoption

Slide 46

Slide 46 text

Kotlin’s Adoption

Slide 47

Slide 47 text

Google Apps Kotlin

Slide 48

Slide 48 text

Android Kotlin

Slide 49

Slide 49 text

Android Jetpack Kotlin & Coroutines ● Kotlin-friendly APIs ● Room suspend functions ● WorkManager CoroutineWorker ● Lifecycles & ViewModel coroutine scopes ● + more KTX libraries...

Slide 50

Slide 50 text

Android KTX (core) sharedPreferences .edit() // create an Editor .putBoolean("key", value) .apply() // write to disk asynchronously Default APIs

Slide 51

Slide 51 text

Android KTX (core) sharedPreferences .edit() // create an Editor .putBoolean("key", value) .apply() // write to disk asynchronously sharedPreferences.edit { putBoolean("key", value) } Default APIs with core-ktx, syntactic sugar on top of the same APIs

Slide 52

Slide 52 text

youtu.be/BOHK_w09pVA

Slide 53

Slide 53 text

Jetpack Compose ● Reactive UI toolkit ● Unbundled from Android Framework ● Developer Preview ● Android Studio v4.0

Slide 54

Slide 54 text

@Composable fun Greeting(name: String) { Text("Hello $name") } Jetpack Compose ● Reactive UI toolkit ● Unbundled from Android Framework ● Developer Preview ● Android Studio v4.0

Slide 55

Slide 55 text

Jetpack Compose ● Reactive UI toolkit ● Unbundled from Android Framework ● Developer Preview ● Android Studio v4.0 github.com/android/compose-samples/tree/master/JetNews

Slide 56

Slide 56 text

youtu.be/dtm2h-_sNDQ

Slide 57

Slide 57 text

Documentation Kotlin ● Migration to Kotlin pages ● Code snippets in Kotlin ● Switch between Java and Kotlin

Slide 58

Slide 58 text

● Kotlin Koans: kotlinlang.org/docs/tutorials/koans.html ● Kotlin Playground play.kotlinlang.org ● Udacity ○ Kotlin Bootcamp for Programmers ○ Developing Android Apps with Kotlin ○ Advanced Android with Kotlin Learning Kotlin

Slide 59

Slide 59 text

● Google I/O and ADS apps: github.com/google/iosched ● Sunflower: github.com/android/sunflower ● Plaid: github.com/android/plaid ● More Samples: developer.android.com/samples?language=kotlin Samples Kotlin

Slide 60

Slide 60 text

Community Kotlin

Slide 61

Slide 61 text

OkHttp ● Client HTTP ● Created by Square ● 100% Kotlin since version 4 fun post(url: String, json: String): String { val mediaType = MediaType.parse("application/json;charset=utf-8") val client = OkHttpClient() val body = RequestBody.create(mediaType, json) val request = Request.Builder().url(url).post(body).build() val response = client.newCall(request).execute() return response.body().string() }

Slide 62

Slide 62 text

SQLDelight ● Generates typesafe Kotlin APIs from SQL ● Created by Square ● 100% Kotlin ● Multi-platform (supported SQLite drivers on Android, JVM and iOS) selectAll: SELECT * FROM hockeyPlayer; insert: INSERT INTO hockeyPlayer(player_number, full_name) VALUES (?, ?); insertFullPlayerObject: INSERT INTO hockeyPlayer(player_number, full_name) VALUES ?; Player.sq

Slide 63

Slide 63 text

SQLDelight ● Generates typesafe Kotlin APIs from SQL ● Created by Square ● 100% Kotlin ● Multi-platform (supported SQLite drivers on Android, JVM and iOS) val database = Database(driver) val playerQueries: PlayerQueries = database.playerQueries println(playerQueries.selectAll().executeAsList()) // Prints [HockeyPlayer.Impl(15, "Ryan Getzlaf")] playerQueries.insert(player_number = 10, full_name = "Corey Perry") println(playerQueries.selectAll().executeAsList()) // Prints [HockeyPlayer.Impl(15, "Ryan Getzlaf"), ...] val player = HockeyPlayer(10, "Ronald McDonald") playerQueries.insertFullPlayerObject(player) PlayerQueries.kt

Slide 64

Slide 64 text

Kotlin multiplatform

Slide 65

Slide 65 text

Component Status entered at Mode for Sources Mode for Binaries Kotlin/JVM v1.0 Fully Stable Fully Stable Kotlin/JS v1.1 AIR* Moving Fast Kotlin/Native v1.3 AIR* Moving Fast Kotlin Scripts (*.kts) v1.2 AIR* Moving Fast Multi-Platform Projects v1.2 Moving Fast Moving Fast Coroutines v1.3 Fully Stable Fully Stable Stability of Different Components * Additions in Incremental Releases

Slide 66

Slide 66 text

Libraries ● SQLDelight: SQLite model facilitation library ● SQLiter: SQLite access driver. Powering the SQLDelight native driver ● Multiplatform Settings: Save simple key-value data ● Kotlinx.Serialization: cross-platform / multi-format reectionless serialization ● Kotlinx.Coroutines: Support library for coroutines ● Stately: State utility library Kotlin Multiplatform Samples ● Kotlinconf App: Kotlinconf schedule app, iOS/Android/Backend ● DroidconKotlin: Droidcon NY/SF schedule app, iOS/Android/Web ● Sudoku: iOS/Android sample game

Slide 67

Slide 67 text

Is Kotlin mobile only

Slide 68

Slide 68 text

● Framework Web ● Client/Server ● 100% Kotlin ● Multi-platform fun main(args: Array) { val server = embeddedServer(Netty, port = 8080) { routing { get("/") { call.respondText( "Hello World!", ContentType.Text.Plain ) } get("/demo") { call.respondText("HELLO WORLD!") } } } server.start(wait = true) }

Slide 69

Slide 69 text

Spring boot and Kotlin spring.io/guides/tutorials/spring-boot-kotlin/

Slide 70

Slide 70 text

KOTLIN ON GOOGLE CLOUD PLATFORM A community-supported project codelabs.developers.google.com/codelabs/cloud-kotlin-emojify/

Slide 71

Slide 71 text

Kotlin future?

Slide 72

Slide 72 text

● More Coroutines! ● More Multi-Platform ● Even better developer experience

Slide 73

Slide 73 text

Thanks Pietro Maggi @pfmaggi