Kotlin First - Taking Advantage of Kotlin for Android Development // 360AnDev 2020

Kotlin First - Taking Advantage of Kotlin for Android Development // 360AnDev 2020

What does it mean to be "Kotlin First"?

How can you start taking full advantage of Kotlin for Android development?

Df78ed2e647f52dadaac996721a6bdfb?s=128

Nate Ebel

July 23, 2020
Tweet

Transcript

  1. Kotlin First Taking Full Advantage of Kotlin for Android Development

    ANDROID DEVELOPER @n8ebel www.goobar.io Nate Ebel
  2. Kotlin First

  3. How can we take full advantage of Kotlin?

  4. Taking Full Advantage of Kotlin Migrating Java Syntax & Features

    Kotlin Standard Library Coroutines & Flow Jetpack Compose Kotlin Multiplatform
  5. Taking Full Advantage of Kotlin New To Kotlin Kotlin Practitioner

    Kotlin Veteran
  6. The Kotlin Journey

  7. How do I start? Am I doing this right? I’m

    convinced I love Kotlin
  8. Taking Full Advantage of Kotlin Get Started With Kotlin Idiomatic

    Kotlin Going Full Kotlin Kotlin Beyond Android
  9. Taking Full Advantage of Kotlin Idiomatic Kotlin Going Full Kotlin

    Kotlin Beyond Android New Projects Converting Java Experimentation
  10. Taking Full Advantage of Kotlin Going Full Kotlin Kotlin Beyond

    Android New Projects Converting Java Experimentation Syntax Kotlin Standard Library Language Features
  11. Taking Full Advantage of Kotlin Kotlin Beyond Android New Projects

    Converting Java Experimentation Syntax Kotlin Standard Library Language Features Android Jetpack Coroutines/Flow Buildscripts/Libraries
  12. Taking Full Advantage of Kotlin New Projects Converting Java Experimentation

    Syntax Kotlin Standard Library Language Features Android Jetpack Coroutines/Flow Buildscripts/Libraries Kotlin Multiplatform Server-side Kotlin Kotlin Scripting
  13. Taking Advantage of Kotlin for Android Development

  14. Getting Started With Kotlin How do I start?

  15. Create A New Android Studio Project

  16. None
  17. Convert Existing Java to Kotlin

  18. Paste Java Into a Kotlin File

  19. Quickly Evaluate Kotlin Code

  20. Quickly Evaluate Kotlin Code •Kotlin scratch files •Kotlin REPL •Add

    a main() function
  21. Idiomatic Kotlin Am I doing this right?

  22. Useful Syntax

  23. Useful Syntax •Use non-null types •Avoid #!! •Use lateinit var

    •Utility functions without classes
  24. Kotlin Standard Library

  25. • arrayOf() / listOf() / mapOf() • filter() • map()

    • orEmpty() • getOrElse() • isNullOrEmpty() / isNullOrBlank() Kotlin Standard Library
  26. val awesomeThings = listOf("360AnDev", null, "Kotlin")

  27. val awesomeThings = listOf("360AnDev", null, "Kotlin") .filterNotNull()

  28. val awesomeThings = listOf("360AnDev", null, "Kotlin") .filterNotNull() .filter { it.length

    > 6 }
  29. val awesomeThing = listOf("360AnDev", null, "Kotlin") .filterNotNull() .filter { it.length

    > 6 } .getOrElse(0) { "There were no awesome things longer than 6 chars" }
  30. val awesomeThing = listOf("360AnDev", null, "Kotlin") .filterNotNull() .filter { it.length

    > 6 } .getOrElse(0) { "There were no awesome things longer than 6 chars" } .let { AwesomeThing(it) }
  31. Special Language Features

  32. • Data classes • Sealed classes • Extension functions Special

    Language Features
  33. data class Repo( val name: String, val owner: Owner, val

    stars: Int ) Data Classes
  34. sealed class ViewState { object Loading : ViewState() data class

    Error(val error: Throwable?) data class Success(val data: List<String>) } Sealed Classes
  35. fun Context.longToast(msg: String) { Toast.makeText(this, msg, Toast.LENGTH_LONG).show() } Extension Functions

  36. Going Full Kotlin Kotlin all the things!

  37. Android Jetpack

  38. Android Jetpack •Paging 3 •Benchmark •Android KTX

  39. Android KTX

  40. Kotlin extensions for Jetpack Leverage Kotlin features •Extension functions/properties •Lambdas

    •Coroutines Android KTX
  41. sharedPreferences.edit { putString(“key”, “with KTX”) putBoolean(“lessBoilerplat”, true) } KTX Core

  42. fragmentManager().commit { addToBackstack(“#..”) add(…) } Fragment KTX

  43. val viewState: LiveData<ViewState> = liveData { emit(defaultViewState()) } LiveDate KTX

  44. @Query(“SELECT * FROM Repos”) suspend fun getRepos(): List<Repo> @Query(“SELECT *

    FROM Repos”) Fun getRepos(): Flow<List<Repo#>> Room KTX
  45. Class RepoListViewModel : ViewModel() { init { viewModelScope.launch { #//

    load async data } } } ViewModel KTX
  46. Kotlin Coroutines

  47. Coroutines are the recommended solution for async programming on Android

  48. @UiThread suspend fun showTasks() { val tasks = loadTasksFromDisk() displayTasks(tasks)

    } suspend fun loadTasksFromDisk: List<Task>
  49. Class TaskListViewModel : ViewModel() { init { viewModelScope.launch { showTasks()

    } } }
  50. interface TasksApi { @GET(“/tasks/user”) suspend fun loadTasksForUser(id: String) }

  51. Class TaskListViewModel : ViewModel() { init { viewModelScope.launch { showTasks()

    val networkTasks = loadTasksForUser() } } }
  52. Kotlin Flow

  53. A Flow is an async data stream leveraging coroutines

  54. @Query(“SELECT * FROM Repos”) fun getRepos(): Flow<List<Repo#>>

  55. val repos: LiveData<List<Repo#>> = repoDao.getRepos().asLiveData()

  56. class TaskListViewModel : ViewModel() { private val _viewState = MutableStateFlow(ViewState())

    val viewState: StateFlow<ViewState> = _viewState }
  57. Gradle Kotlin DSL

  58. import org.jetbrains.kotlin.config.KotlinCompilerVersion plugins { id(“com.android.application”) kotlin(“android”) kotlin(“android.extensions”) } android {

    compileSdkVersion(29) … } dependencies { iplementation(“com.android.support:appcompat-v7:27.1.1”) } build.gradle.kts
  59. Jetpack Compose

  60. Jetpack Compose is the future of UI development for Android

  61. @Composable fun HelloWorld() { Test(“Hello World”) }

  62. @Composable fun RepoItem() { Column { Text(“repo name”) Text(“repo owner”)

    } }
  63. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState) setContent { RepoItem() } } }
  64. Bonus • Koin • SQLDelight • Moshi • Coil •

    MockK
  65. Kotlin Beyond Android What else can I build with Kotlin?

  66. Kotlin Multiplatform

  67. Share core logic across platforms using Kotlin Multiplatform

  68. Kotlin Multiplatform Write common Kotlin code Package/Consume for multiple targets

    •Android/JVM •iOS/MacOS •JavaScript •Native
  69. Kotlin Multiplatform Does not replace native UI Relatively low risk

    for Android
  70. Server Side Kotlin

  71. Server Side Kotlin Multiple supported frameworks •Ktor •Spring Boot •Quarkus

  72. fun main(args: Array<String>) { val server = embeddedServer(Netty, 8080) {

    routing { get(“/“) { call.respondText(“Hello World!”, ContentType.Text.HTML) } get(“/tasks”) { #// return tasks } } } server.start(wait = true) } Simple Ktor service
  73. More Kotlin

  74. More Kotlin Additional ways to use Kotlin •JavaScript •Kotlin Native

    •Kotlin scripting using script
  75. Kotlin First

  76. Taking Full Advantage of Kotlin Project Samples & Defaults Libraries

    & Tooling Recommended Language & Patterns Usefulness Beyond Android
  77. How do I start? Am I doing this right? I’m

    convinced I love Kotlin
  78. ANDROID DEVELOPER @n8ebel www.goobar.io Nate Ebel Thanks for Watching