$30 off During Our Annual Pro Sale. View Details »

What's New In Kotlin? - Seattle GDG I/O'19 Extended

What's New In Kotlin? - Seattle GDG I/O'19 Extended

A look at some of the newest Kotlin features and announcements from 2019 and Google I/O '19.

- New Features
- IO19
- New Learning Opportunities

Specific topics include:
- kotlinx.coroutines
- kotlinx.coroutines.flow
- kotlinx.serialization
- Jetpack Compose
- Kotlin/Everywhere
- etc...

Nate Ebel

May 22, 2019
Tweet

More Decks by Nate Ebel

Other Decks in Programming

Transcript

  1. What’s New In
    Kotlin?
    Nate Ebel
    @n8ebel

    View Slide

  2. New
    Opportunities
    to Learn
    Kotlin at #io19
    New Features

    View Slide

  3. New
    Opportunities
    to Learn
    Kotlin at #io19

    View Slide

  4. New
    Opportunities
    to Learn

    View Slide

  5. ● Slides/Links available via Twitter & Speakerdeck
    ● @n8ebel

    View Slide

  6. New(ish) Features

    View Slide

  7. ● Coroutines
    ● Flow
    ● Serialization
    ● And more….

    View Slide

  8. Coroutines
    ● “asynchronous, non-blocking code”
    ● kotlinx.coroutines
    ● launch, aysnc
    ● ViewModelScope

    View Slide

  9. import kotlinx.coroutines.*
    fun main() = runBlocking {
    launch { doWorld() }
    println("Hello,")
    }
    // this is your first suspending function
    suspend fun doWorld() {
    delay(1000L)
    println("World!")
    }
    Simple Coroutine

    View Slide

  10. class MyViewModel: ViewModel() {
    init {
    viewModelScope.launch {
    // Coroutine that will be canceled
    // when the ViewModel is cleared.
    }
    }
    }
    New ViewModelScope

    View Slide

  11. Understand Kotlin
    Coroutines on Android
    I/O Talk

    View Slide

  12. Flow
    ● “asynchronous cold streams”
    ● kotlinx.coroutines.flow
    ● filter, fold, map

    View Slide

  13. ● Similar to RxJava
    ● Channels = hot
    ● Flow = cold
    ● Multiplatform support

    View Slide

  14. val ints: Flow = flow {
    for (i in 1..10) {
    delay(100)
    emit(i)
    }
    }
    // takes 1 second, prints 10 ints
    ints.collect { println(it) }
    Flow In Action
    https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/index.html

    View Slide

  15. kotlinx.
    serialization
    ● “cross-platform /
    multi-format reflectionless
    serialization”
    ● kotlinx.serialization

    View Slide

  16. import kotlinx.serialization.*
    import kotlinx.serialization.json.Json
    /**
    * From https://github.com/Kotlin/kotlinx.serialization
    */
    @Serializable
    data class Data(val a: Int, val b: String = "42")
    fun main(args: Array) {
    // serializing objects
    val jsonData = Json.stringify(Data.serializer(), Data(42))
    // serializing lists
    val jsonList = Json.stringify(Data.serializer().list, listOf(Data(42)))
    println(jsonData) // {"a": 42, "b": "42"}
    println(jsonList) // [{"a": 42, "b": "42"}]
    // parsing data back
    // b is optional since it has default value
    val obj = Json.parse(Data.serializer(), """{"a":42}""")
    println(obj) // Data(a=42, b="42")
    }
    Multiplatform Serialization
    https://github.com/Kotlin/kotlinx.serializationl

    View Slide

  17. And more...
    ● Incremental annotation
    processing for kapt
    ● Unsigned numbers
    ● Cocoapod support for
    XCode
    ● Kotlin Gradle DSL support
    for multiplatform projects

    View Slide

  18. Kotlin at #io19

    View Slide

  19. Kotlin at
    #io19
    Kotlin First

    View Slide

  20. View Slide

  21. Kotlin at
    #io19
    Architecture
    Components

    View Slide

  22. View Slide

  23. Kotlin at
    #io19
    Jetpack Compose

    View Slide

  24. ● New, reactive UI toolkit written in Kotlin
    ● Unbundled from framework
    ● Compose UI Library & Compose Compiler

    View Slide

  25. @Model class CounterState(var count: Int)
    @Composable fun Counter() {
    // introduce a CounterState value.
    // Instance will be preserved across compositions.
    // Note: the `+` syntax is temporary
    val count = +memo { CounterState(0) }
    // use it to compose your UI. pass it into other composables as parameters
    Text(text="Count: ${count.count}")
    // modify the value inside of event handlers, for instance
    Button(text="Increment, onClick = { count.count += 1; })
    }
    Build Composable UI
    https://developer.android.com/jetpack/compose

    View Slide

  26. class UserListActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
    AppComponent()
    }
    }
    }
    Build Composable UI
    https://developer.android.com/jetpack/compose

    View Slide

  27. Kotlin at
    #io19
    Notable Talks

    View Slide

  28. ● Understand Kotlin Coroutines on Android
    ● Mobile Backends with Kotlin and Google Cloud
    ● What's New in Android
    ● Kotlin Under the Hood: Understand the Internals
    ● Declarative UI Patterns
    ● What’s New In Kotlin On Android, 2 Years In

    View Slide

  29. New Learning Opportunities

    View Slide

  30. New Learning
    Opportunities
    Developing Android
    Apps with Kotlin
    by Google

    View Slide

  31. ● Developing Android Apps with Kotlin by Google
    ● New course from Google & Udacity
    ● Free

    View Slide

  32. New Learning
    Opportunities
    Kotlin/Everywhere

    View Slide

  33. View Slide

  34. ● Kotlin/Everywhere
    ● Community-led events
    ● Learn about Kotlin for Android, Cloud, and
    Multi-platform development
    ● Apply for support from Google & JetBrains

    View Slide

  35. That’s All Folks
    Thanks!

    View Slide

  36. ● Kotlin-related talks from Google I/O
    ● Reference for kotlinx.coroutines.flow
    ● Post regarding goals/need for Flow
    ● Discussion around Flow vs Channels vs RxJava
    ● Reference for kotlinx.serialization
    ● Developing Android Apps With Kotlin Course
    ● KOTLIN/Everywhere
    ● ASOP page for Jetpack Compose
    ● Reference for Jetpack Compose
    @n8ebel

    View Slide