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...

Df78ed2e647f52dadaac996721a6bdfb?s=128

Nate Ebel

May 22, 2019
Tweet

Transcript

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

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

  3. New Opportunities to Learn Kotlin at #io19

  4. New Opportunities to Learn

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

  6. New(ish) Features

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

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

    • ViewModelScope
  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
  10. class MyViewModel: ViewModel() { init { viewModelScope.launch { // Coroutine

    that will be canceled // when the ViewModel is cleared. } } } New ViewModelScope
  11. Understand Kotlin Coroutines on Android I/O Talk

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

    map
  13. • Similar to RxJava • Channels = hot • Flow

    = cold • Multiplatform support
  14. val ints: Flow<Int> = 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
  15. kotlinx. serialization • “cross-platform / multi-format reflectionless serialization” • kotlinx.serialization

  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<String>) { // 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
  17. And more... • Incremental annotation processing for kapt • Unsigned

    numbers • Cocoapod support for XCode • Kotlin Gradle DSL support for multiplatform projects
  18. Kotlin at #io19

  19. Kotlin at #io19 Kotlin First

  20. None
  21. Kotlin at #io19 Architecture Components

  22. None
  23. Kotlin at #io19 Jetpack Compose

  24. • New, reactive UI toolkit written in Kotlin • Unbundled

    from framework • Compose UI Library & Compose Compiler
  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
  26. class UserListActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContent { AppComponent() } } } Build Composable UI https://developer.android.com/jetpack/compose
  27. Kotlin at #io19 Notable Talks

  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
  29. New Learning Opportunities

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

  31. • Developing Android Apps with Kotlin by Google • New

    course from Google & Udacity • Free
  32. New Learning Opportunities Kotlin/Everywhere

  33. None
  34. • Kotlin/Everywhere • Community-led events • Learn about Kotlin for

    Android, Cloud, and Multi-platform development • Apply for support from Google & JetBrains
  35. That’s All Folks Thanks!

  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