State of Kotlin in Android Florina Muntenescu FMuntenescu - - PowerPoint PPT Presentation

state of kotlin in android florina muntenescu
SMART_READER_LITE
LIVE PREVIEW

State of Kotlin in Android Florina Muntenescu FMuntenescu - - PowerPoint PPT Presentation

Kotlin 1.4 Online Event State of Kotlin in Android Florina Muntenescu FMuntenescu October, 2020 Why Android Kotlin Expressiveness Image safe area Why Android Kotlin Safety Expressiveness Image safe area Kotlin on Android in


slide-1
SLIDE 1

Kotlin 1.4 Online Event October, 2020 FMuntenescu

State of Kotlin in Android Florina Muntenescu

slide-2
SLIDE 2

Image safe area

Why Android ❤ Kotlin

Expressiveness

slide-3
SLIDE 3

Image safe area

Why Android ❤ Kotlin

Expressiveness Safety

slide-4
SLIDE 4

20%

Less likely to crash

Kotlin on Android in 2020

Source: Google Internal Data, July 2020
slide-5
SLIDE 5

Image safe area

Why Android ❤ Kotlin

Expressiveness Safety Interoperability

slide-6
SLIDE 6

Image safe area

Why Android ❤ Kotlin

Expressiveness Safety Interoperability Structured concurrency

slide-7
SLIDE 7

50%

More likely to be very satisfjed

Kotlin on Android in 2020

Source: Google Internal Data, May 2020
slide-8
SLIDE 8

Pro Android developers use Kotlin

Source: Google Internal Data, May 2020

60%

Kotlin on Android in 2020

slide-9
SLIDE 9

70%+

Top 1k apps contain Kotlin code

Source: Google Internal Data, May 2020

Kotlin on Android in 2020

slide-10
SLIDE 10

Co-leading Kotlin Foundation Contributing to the Kotlin compiler Developing Kotlin related tooling and libraries

Google’s contributions to Kotlin

slide-11
SLIDE 11

Docs - d.android.com/kotlin Samples - github.com/android Codelabs - goo.gle/kotlin-codelabs Aruicles - goo.gle/kotlin-posts Videos - goo.gle/kotlin-videos

Google’s contributions to teaching Kotlin

slide-12
SLIDE 12

Docs - d.android.com/kotlin Samples - github.com/android Codelabs - goo.gle/kotlin-codelabs Aruicles - goo.gle/kotlin-posts Videos - goo.gle/kotlin-videos Faculty training

Google’s contributions to teaching Kotlin

slide-13
SLIDE 13

Since last year’s Google I/O...

Libraries Tools Learning Nullness annotations

@

New lint checks Kotlin live templates R8 Optimizations New KTX releases Kotlin-fjrst libraries Vocabulary series Samples and docs Kotlin/Everywhere

slide-14
SLIDE 14

Libraries Tools Learning Nullness annotations

@

New lint checks Kotlin live templates R8 Optimizations New KTX releases Kotlin-fjrst libraries Vocabulary series Samples and docs Kotlin/Everywhere

Since last year’s Google I/O...

slide-15
SLIDE 15

Libraries Tools Learning Nullness annotations

@

New lint checks Kotlin live templates R8 Optimizations New KTX releases Kotlin-fjrst libraries Vocabulary series Samples and docs Kotlin/Everywhere

Since last year’s Google I/O...

slide-16
SLIDE 16

Libraries Tools Learning Nullness annotations

@

New lint checks Kotlin live templates R8 Optimizations New KTX releases Kotlin-fjrst libraries Vocabulary series Samples and docs Kotlin/Everywhere

Since last year’s Google I/O...

slide-17
SLIDE 17

Libraries Tools Learning Nullness annotations

@

New lint checks Kotlin live templates R8 Optimizations New KTX releases Kotlin-fjrst libraries Vocabulary series Samples and docs Kotlin/Everywhere

Since last year’s Google I/O...

slide-18
SLIDE 18

Libraries Tools Learning Nullness annotations

@

New lint checks Kotlin live templates R8 Optimizations New KTX releases Kotlin-fjrst libraries Vocabulary series Samples and docs Kotlin/Everywhere

Since last year’s Google I/O...

slide-19
SLIDE 19

Libraries Tools Learning Nullness annotations

@

New lint checks Kotlin live templates R8 Optimizations New KTX releases Kotlin-fjrst libraries Vocabulary series Samples and docs Kotlin/Everywhere

Since last year’s Google I/O...

slide-20
SLIDE 20

Libraries Tools Learning Nullness annotations

@

New lint checks Kotlin live templates R8 Optimizations New KTX releases Kotlin-fjrst libraries Vocabulary series Samples and docs Kotlin/Everywhere

Since last year’s Google I/O...

slide-21
SLIDE 21

Libraries Tools Learning Nullness annotations

@

New lint checks Kotlin live templates R8 Optimizations New KTX releases Kotlin-fjrst libraries Vocabulary series Samples and docs Kotlin/Everywhere

Since last year’s Google I/O...

slide-22
SLIDE 22

Libraries Tools Learning Nullness annotations

@

New lint checks Kotlin live templates R8 Optimizations New KTX releases Kotlin-fjrst libraries Vocabulary series Samples and docs Kotlin/Everywhere

Since last year’s Google I/O...

slide-23
SLIDE 23
  • Modern, declarative UI toolkit
  • Built on Kotlin
  • Unbundled

Jetpack Compose

slide-24
SLIDE 24

Build time (development host) Runtime (on device)

Compose Compiler Plugin

Code generation extensions

kotlinc

Upstream Kotlin compiler (1.4)

Compose Material

Surface, Buttons, Tabs, Themes

Compose Foundation

Standard layouts, interactions

Compose UI

Input, Measure, Layout, Drawing

Compose Runtime

Tree management, Effects

Android Studio

Live preview, Apply Changes

Compose project structure

slide-25
SLIDE 25

Button(

  • nClick = { /* do an action */ },

modifier = Modifier.padding(vertical = 20.dp) // default: Modifier ) { Text(text = "Click me") }

Jetpack Compose API

slide-26
SLIDE 26 Source: Google Internal Data, May 2020

Kotlin at Google

slide-27
SLIDE 27
  • > 2M lines of Kotlin code
  • Used on server-side
  • Kotlin in OSS projects:

○ gRPC ○ ProtoBuf (coming soon)

Kotlin at Google

slide-28
SLIDE 28

Kotlin 1.4 preview

slide-29
SLIDE 29

“Faster Please!” Kotlin Tooling Performance

slide-30
SLIDE 30
  • Incremental Annotation

Processors

Kotlin Tooling Pergormance

Kotlin Tooling Performance

slide-31
SLIDE 31
  • Incremental Annotation

Processors

  • IDE Pergormance

Kotlin Tooling Performance

slide-32
SLIDE 32
  • Incremental Annotation

Processors

  • IDE Pergormance
  • Gradle improvements

Kotlin Tooling Performance

slide-33
SLIDE 33
  • Explicit API mode in Kotlin 1.4
  • Kotlin Symbol Processing
  • R8 Kotlin Metadata supporu

Kotlin for library authors

slide-34
SLIDE 34
  • Be intentional about your

public API

  • Explicitly declare:

○ Visibility modifjers ○ Type

Explicit API mode

slide-35
SLIDE 35

kotlin { // for strict mode explicitApi() // or explicitApi = 'strict' // for warning mode explicitApiWarning() // or explicitApi = 'warning' }

Explicit API mode

build.gradle

slide-36
SLIDE 36
  • Explicit API mode in Kotlin 1.4
  • Kotlin Symbol Processing
  • R8 Kotlin Metadata supporu

Kotlin for library authors

slide-37
SLIDE 37 Title safe

Kotlin Symbol Processing

Image safe area

Developer Preview We need your feedback! goo.gle/ksp

slide-38
SLIDE 38
  • Supporu for

Kotlin metadata rewriting

  • Android Gradle Plugin

4.1.0-beta03

R8

slide-39
SLIDE 39

This class was deprecated in API level R (Android 11)

Use the standard java.util.concurrent or Kotlin concurrency utilities instead.

AsyncTask

Deprecated in Android 11

slide-40
SLIDE 40

Async programming

What should we use?

slide-41
SLIDE 41

Coroutines

Coroutines are the Kotlin way to do asynchronous programming. Compiler supporu is stable since Kotlin 1.3, together with a robust kotlinx.coroutines library.

Brief overview

slide-42
SLIDE 42

Professional developers who use Coroutines have reporued seeing increased productivity

Coroutines

Source: Google Internal Data, May 2020
slide-43
SLIDE 43

Coroutines

viewModelScope.launch { val status = withContext(Dispatchers.IO) { val office = officeDetailsService.findOffice("US-BVE") db.officeDetails().insert(office)

  • ffice.status

} val isOpenText = if (status == OfficeStatus.OPEN) { "open :)" } else { "closed :(" }

  • fficeStatusLiveData.value = "Your office is $isOpenText"

}

slide-44
SLIDE 44

viewModelScope.launch { val status = withContext(Dispatchers.IO) { val office = officeDetailsService.findOffice("US-BVE") db.officeDetails().insert(office)

  • ffice.status

} val isOpenText = if (status == OfficeStatus.OPEN) { "open :)" } else { "closed :(" }

  • fficeStatusLiveData.value = "Your office is $isOpenText"

}

Coroutines

slide-45
SLIDE 45

viewModelScope.launch { val status = withContext(Dispatchers.IO) { val office = officeDetailsService.findOffice("US-BVE") db.officeDetails().insert(office)

  • ffice.status

} val isOpenText = if (status == OfficeStatus.OPEN) { "open :)" } else { "closed :(" }

  • fficeStatusLiveData.value = "Your office is $isOpenText"

}

Coroutines

slide-46
SLIDE 46

viewModelScope.launch { val status = withContext(Dispatchers.IO) { val office = officeDetailsService.findOffice("US-BVE") db.officeDetails().insert(office)

  • ffice.status

} val isOpenText = if (status == OfficeStatus.OPEN) { "open :)" } else { "closed :(" }

  • fficeStatusLiveData.value = "Your office is $isOpenText"

}

Coroutines

slide-47
SLIDE 47

viewModelScope.launch { val status = withContext(Dispatchers.IO) { val office = officeDetailsService.findOffice("US-BVE") db.officeDetails().insert(office)

  • ffice.status

} val isOpenText = if (status == OfficeStatus.OPEN) { "open :)" } else { "closed :(" }

  • fficeStatusLiveData.value = "Your office is $isOpenText"

}

Coroutines

slide-48
SLIDE 48

viewModelScope.launch { val status = withContext(Dispatchers.IO) { val office = officeDetailsService.findOffice("US-BVE") db.officeDetails().insert(office)

  • ffice.status

} val isOpenText = if (status == OfficeStatus.OPEN) { "open :)" } else { "closed :(" }

  • fficeStatusLiveData.value = "Your office is $isOpenText"

}

Coroutines

slide-49
SLIDE 49

Kotlin coroutines are the recommended solution for async code

slide-50
SLIDE 50

Structured concurrency Non-blocking, sequential code Cancellation propagation Natural exception handling

Android 💛 coroutines

slide-51
SLIDE 51

Coroutines & Jetpack

slide-52
SLIDE 52

Room

Database queries

@Dao interface UsersDao { @Insert suspend fun insertUsers(vararg users: User) @Update suspend fun updateUsers(vararg users: User) @Delete suspend fun deleteUsers(vararg users: User) @Query("SELECT * FROM users") suspend fun getUsers(): List<User> }

slide-53
SLIDE 53

Room

Database queries

@Dao interface UsersDao { @Query("SELECT * FROM users") suspend fun getUsers(): List<User> @Query("SELECT * FROM users") fun getUsers(): Flow<List<User>> }

slide-54
SLIDE 54

WorkManager

CoroutineWorker

class PeriodicSyncWorker( appContext: Context, workerParams: WorkerParameters ) : CoroutineWorker(appContext, workerParams) {

  • verride suspend fun doWork(): Result {

val data = readFromDb() val serverData = uploadToServer(data) writeToDb(serverData) return Result.success() } }

slide-55
SLIDE 55

class UsersPagingSource(val backend: UsersService) : PagingSource<Int, User>() {

  • verride suspend fun load(

params: LoadParams<Int> ): LoadResult<Int, User> { ... val response = backend.getUsers(page) return ... } }

Paging

PagingSource

slide-56
SLIDE 56

val users: Flow<PagingData<User>> = Pager( config = PagingConfig(pageSize = 10) ) { UsersPagingSource(usersService) }.flow

Paging

Pager

slide-57
SLIDE 57
  • Data storage solution
  • SharedPreferences replacement
  • Proto DataStore - typed objects
  • Preferences DataStore -

key-value pairs

DataStore

slide-58
SLIDE 58

// Using Proto DataStore val myCounterFlow: Flow<Int> = settingsDataStore.data .map { settings -> settings.myCounter }

DataStore

Preferences DataStore and Proto DataStore

slide-59
SLIDE 59

@Composable fun Search( // ... ) { launchInComposition(state.query.text) { state.searching = true state.searchResults = SearchRepo.search(state.query.text) state.searching = false } }

Jetpack Compose

slide-60
SLIDE 60

Kotlin coroutines are the recommended solution for async code

slide-61
SLIDE 61 Title safe

Coroutines interop

Image safe area

For Java ↔ Kotlin users

slide-62
SLIDE 62

Coroutines learning

goo.gle/coroutines-posts

  • Coroutines: First things fjrst
  • Cancellation in coroutines
  • Exceptions in Coroutines
  • The suspend modifjer — Under the hood

goo.gle/coroutines-videos

  • Coroutines 101
  • Android Coroutines: How to manage async tasks in Kotlin

goo.gle/kotlin-codelabs

  • Use Kotlin Coroutines in your Android App
  • Learn advanced coroutines with Kotlin Flow and LiveData
  • Building a Kotlin extensions library
slide-63
SLIDE 63
slide-64
SLIDE 64
  • Productive Language
  • Growing supporu
  • New releases & tooling
  • Coroutines recommended for

async programming

  • Expanding docs, codelabs &

videos

Android 💛 Kotlin

slide-65
SLIDE 65

FMuntenescu

Thanks! Have a nice Kotlin