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. Kotlin in 2019
    Pietro Maggi
    @pfmaggi

    View Slide

  2. Kotlin

    View Slide

  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

    View Slide

  4. Pragmatic
    Ergonomic
    Constant feedback loop

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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
    ?

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  19. Kotlin First

    View Slide

  20. Kotlin First

    View Slide

  21. Personal Kotlin’s
    Favorites

    View Slide

  22. Null Safety
    Aims to eliminate NullPointerException

    View Slide

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

    View Slide

  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

    View Slide

  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)

    View Slide

  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

    View Slide

  27. Asynchronous Operations
    Coroutines are light-weight threads

    View Slide

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

    View Slide

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

    View Slide

  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,

    View Slide

  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!

    View Slide

  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!

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  36. Developer
    Experience

    View Slide

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

    View Slide

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

    View Slide

  39. Kapt is incremental now

    View Slide

  40. Android Studio Kotlin

    View Slide

  41. R8 Kotlin

    View Slide

  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")
    }
    }

    View Slide

  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");
    }
    }
    }

    View Slide

  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

    View Slide

  45. Kotlin’s Adoption

    View Slide

  46. Kotlin’s Adoption

    View Slide

  47. Google Apps Kotlin

    View Slide

  48. Android Kotlin

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  52. youtu.be/BOHK_w09pVA

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  56. youtu.be/dtm2h-_sNDQ

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  60. Community

    Kotlin

    View Slide

  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()
    }

    View Slide

  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

    View Slide

  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

    View Slide

  64. Kotlin multiplatform

    View Slide

  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

    View Slide

  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

    View Slide

  67. Is Kotlin mobile only

    View Slide

  68. ● 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)
    }

    View Slide

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

    View Slide

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

    View Slide

  71. Kotlin
    future?

    View Slide


  72. More Coroutines!

    More Multi-Platform

    Even better developer experience

    View Slide

  73. Thanks
    Pietro Maggi
    @pfmaggi

    View Slide