Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin in 2019

Kotlin in 2019

For most developers who used Kotlin, their experience started on mobile with Android when the official support was announced in 2017. Since then, the language has evolved and its support has continuously improved. In this presentation, I will talk about the current state of Kotlin on Android but also on other platforms and a preview of its future.

Pietro F. Maggi

November 29, 2019
Tweet

More Decks by Pietro F. Maggi

Other Decks in Technology

Transcript

  1. Language design is cast in stone, but this stone is

    reasonably soft, and with some effort we can reshape it later. Kotlin Design Team
  2. 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
  3. package hello fun main() { println("Hello World") } Kotlin •

    Object Oriented and Functional • Static typing with type inference • Safe • Concise
  4. History Kotlin is announced Creation of Kotlin by Jetbrains 2011

    2012 2013 2014 2015 2016 2017 2018 2019 ?
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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 ?
  11. Android SDK Java application development 2008 2009 2010 2011 2012

    2013 2014 2015 2016 2017 2018 2019 2020 Android Development
  12. 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
  13. 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
  14. 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
  15. var a: String = "abc" a = null Null Safety

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

    Aims to eliminate NullPointerException > Null can not be a value of a non-null type String
  17. 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)
  18. 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
  19. import kotlinx.coroutines.* fun main() { GlobalScope.launch { delay(1000L) println("World!") }

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

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

    print("Hello, ") Thread.sleep(2000L) } Asynchronous Operations Coroutines are light-weight threads > Hello,
  22. 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!
  23. 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!
  24. 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
  25. 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
  26. Android Studio Kotlin • Kotlin plugin is part of Android

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

    Studio • Lint checks & refactoring support • Project templates and samples • Kotlin-friendly Android SDK (nullability)
  28. 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") } }
  29. 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"); } } }
  30. 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
  31. Android Jetpack Kotlin & Coroutines • Kotlin-friendly APIs • Room

    suspend functions • WorkManager CoroutineWorker • Lifecycles & ViewModel coroutine scopes • + more KTX libraries...
  32. Android KTX (core) sharedPreferences .edit() // create an Editor .putBoolean("key",

    value) .apply() // write to disk asynchronously Default APIs
  33. 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
  34. Jetpack Compose • Reactive UI toolkit • Unbundled from Android

    Framework • Developer Preview • Android Studio v4.0
  35. @Composable fun Greeting(name: String) { Text("Hello $name") } Jetpack Compose

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

    Framework • Developer Preview • Android Studio v4.0 github.com/android/compose-samples/tree/master/JetNews
  37. Documentation Kotlin • Migration to Kotlin pages • Code snippets

    in Kotlin • Switch between Java and Kotlin
  38. • 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
  39. • 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
  40. 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() }
  41. 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
  42. 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
  43. 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
  44. 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
  45. • Framework Web • Client/Server • 100% Kotlin • Multi-platform

    fun main(args: Array<String>) { 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) }