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.

3dfa913cbcfe896af79e0484084c316b?s=128

Pietro F. Maggi

November 29, 2019
Tweet

Transcript

  1. Kotlin in 2019 Pietro Maggi @pfmaggi

  2. Kotlin

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

    reasonably soft, and with some effort we can reshape it later. Kotlin Design Team
  4. Pragmatic Ergonomic Constant feedback loop

  5. Kotlin • Object Oriented and Functional • Static typing with

    type inference • Safe • Concise
  6. 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
  7. package hello fun main() { println("Hello World") } Kotlin •

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

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

    2013 2014 2015 2016 2017 2018 2019 2020 Android Development
  16. 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
  17. 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
  18. 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
  19. Kotlin First

  20. Kotlin First

  21. Personal Kotlin’s Favorites

  22. Null Safety Aims to eliminate NullPointerException

  23. var a: String = "abc" a = null Null Safety

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

    Aims to eliminate NullPointerException > Null can not be a value of a non-null type String
  25. 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)
  26. 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
  27. Asynchronous Operations Coroutines are light-weight threads

  28. import kotlinx.coroutines.* fun main() { GlobalScope.launch { delay(1000L) println("World!") }

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

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

    print("Hello, ") Thread.sleep(2000L) } Asynchronous Operations Coroutines are light-weight threads > Hello,
  31. 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!
  32. 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!
  33. Extension Functions Add functionality to a type, without directly modifying

    the type’s definition
  34. 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
  35. 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
  36. Developer Experience

  37. Android Studio Kotlin • Kotlin plugin is part of Android

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

    Studio • Lint checks & refactoring support • Project templates and samples • Kotlin-friendly Android SDK (nullability)
  39. Kapt is incremental now

  40. Android Studio Kotlin

  41. R8 Kotlin

  42. 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") } }
  43. 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"); } } }
  44. 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
  45. Kotlin’s Adoption

  46. Kotlin’s Adoption

  47. Google Apps Kotlin

  48. Android Kotlin

  49. Android Jetpack Kotlin & Coroutines • Kotlin-friendly APIs • Room

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

    value) .apply() // write to disk asynchronously Default APIs
  51. 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
  52. youtu.be/BOHK_w09pVA

  53. Jetpack Compose • Reactive UI toolkit • Unbundled from Android

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

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

    Framework • Developer Preview • Android Studio v4.0 github.com/android/compose-samples/tree/master/JetNews
  56. youtu.be/dtm2h-_sNDQ

  57. Documentation Kotlin • Migration to Kotlin pages • Code snippets

    in Kotlin • Switch between Java and Kotlin
  58. • 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
  59. • 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
  60. Community Kotlin

  61. 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() }
  62. 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
  63. 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
  64. Kotlin multiplatform

  65. 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
  66. 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
  67. Is Kotlin mobile only

  68. • 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) }
  69. Spring boot and Kotlin spring.io/guides/tutorials/spring-boot-kotlin/

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

  71. Kotlin future?

  72. • More Coroutines! • More Multi-Platform • Even better developer

    experience
  73. Thanks Pietro Maggi @pfmaggi