Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness...

65
Kotlin 1.4 Online Event October, 2020 FMuntenescu State of Kotlin in Android Florina Muntenescu

Transcript of Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness...

Page 1: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Kotlin 1.4 Online Event

October, 2020FMuntenescu

State of Kotlin in AndroidFlorina Muntenescu

Page 2: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Image safe area

Why Android ❤ Kotlin

Expressiveness

Page 3: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Image safe area

Why Android ❤ Kotlin

Expressiveness Safety

Page 4: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

20%Less likely to crash

Kotlin on Android in 2020

Source: Google Internal Data, July 2020

Page 5: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Image safe area

Why Android ❤ Kotlin

Expressiveness Safety

Interoperability

Page 6: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Image safe area

Why Android ❤ Kotlin

Expressiveness Safety

Interoperability Structured concurrency

Page 7: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

50%More likely to be very satisfied

Kotlin on Android in 2020

Source: Google Internal Data, May 2020

Page 8: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Pro Android developers use Kotlin

Source: Google Internal Data, May 2020

60%

Kotlin on Android in 2020

Page 9: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

70%+Top 1k apps contain Kotlin code

Source: Google Internal Data, May 2020

Kotlin on Android in 2020

Page 10: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Co-leading Kotlin Foundation

Contributing to the Kotlin

compiler

Developing Kotlin related tooling

and libraries

Google’s contributions to Kotlin

Page 11: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Docs - d.android.com/kotlin

Samples - github.com/android

Codelabs - goo.gle/kotlin-codelabs

Articles - goo.gle/kotlin-posts

Videos - goo.gle/kotlin-videos

Google’s contributions to teaching Kotlin

Page 12: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Docs - d.android.com/kotlin

Samples - github.com/android

Codelabs - goo.gle/kotlin-codelabs

Articles - goo.gle/kotlin-posts

Videos - goo.gle/kotlin-videos

Faculty training

Google’s contributions to teaching Kotlin

Page 13: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Since last year’s Google I/O...Libraries Tools Learning

Nullness annotations

@

New lint checks

Kotlin live templates

R8 Optimizations

New KTX releases

Kotlin-first libraries Vocabulary series

Samples and docs

Kotlin/Everywhere

Page 14: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Libraries Tools Learning

Nullness annotations

@

New lint checks

Kotlin live templates

R8 Optimizations

New KTX releases

Kotlin-first libraries Vocabulary series

Samples and docs

Kotlin/Everywhere

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

Page 15: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Libraries Tools Learning

Nullness annotations

@

New lint checks

Kotlin live templates

R8 Optimizations

New KTX releases

Kotlin-first libraries Vocabulary series

Samples and docs

Kotlin/Everywhere

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

Page 16: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Libraries Tools Learning

Nullness annotations

@

New lint checks

Kotlin live templates

R8 Optimizations

New KTX releases

Kotlin-first libraries Vocabulary series

Samples and docs

Kotlin/Everywhere

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

Page 17: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Libraries Tools Learning

Nullness annotations

@

New lint checks

Kotlin live templates

R8 Optimizations

New KTX releases

Kotlin-first libraries Vocabulary series

Samples and docs

Kotlin/Everywhere

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

Page 18: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Libraries Tools Learning

Nullness annotations

@

New lint checks

Kotlin live templates

R8 Optimizations

New KTX releases

Kotlin-first libraries Vocabulary series

Samples and docs

Kotlin/Everywhere

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

Page 19: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Libraries Tools Learning

Nullness annotations

@

New lint checks

Kotlin live templates

R8 Optimizations

New KTX releases

Kotlin-first libraries Vocabulary series

Samples and docs

Kotlin/Everywhere

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

Page 20: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Libraries Tools Learning

Nullness annotations

@

New lint checks

Kotlin live templates

R8 Optimizations

New KTX releases

Kotlin-first libraries Vocabulary series

Samples and docs

Kotlin/Everywhere

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

Page 21: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Libraries Tools Learning

Nullness annotations

@

New lint checks

Kotlin live templates

R8 Optimizations

New KTX releases

Kotlin-first libraries Vocabulary series

Samples and docs

Kotlin/Everywhere

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

Page 22: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Libraries Tools Learning

Nullness annotations

@

New lint checks

Kotlin live templates

R8 Optimizations

New KTX releases

Kotlin-first libraries Vocabulary series

Samples and docs

Kotlin/Everywhere

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

Page 23: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

● Modern, declarative UI toolkit

● Built on Kotlin

● Unbundled

Jetpack Compose

Page 24: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Build time (development host) Runtime (on device)

Compose Compiler PluginCode generation extensions

kotlincUpstream Kotlin compiler (1.4)

Compose MaterialSurface, Buttons, Tabs, Themes

Compose FoundationStandard layouts, interactions

Compose UIInput, Measure, Layout, Drawing

Compose RuntimeTree management, Effects

Android StudioLive preview, Apply Changes

Compose project structure

Page 25: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Button( onClick = { /* do an action */ }, modifier = Modifier.padding(vertical = 20.dp) // default: Modifier) { Text(text = "Click me")}

Jetpack Compose API

Page 26: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Source: Google Internal Data, May 2020

Kotlin at Google

Page 27: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

• > 2M lines of Kotlin code

• Used on server-side

• Kotlin in OSS projects:

○ gRPC

○ ProtoBuf

(coming soon)

Kotlin at Google

Page 28: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Kotlin 1.4 preview

Page 29: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

“Faster Please!”

Kotlin Tooling Performance

Page 30: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

• Incremental Annotation

Processors

Kotlin Tooling PerformanceKotlin Tooling Performance

Page 31: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

• Incremental Annotation

Processors

• IDE Performance

Kotlin Tooling Performance

Page 32: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

• Incremental Annotation

Processors

• IDE Performance

• Gradle improvements

Kotlin Tooling Performance

Page 33: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

• Explicit API mode in Kotlin 1.4

• Kotlin Symbol Processing

• R8 Kotlin Metadata support

Kotlin for library authors

Page 34: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

• Be intentional about your

public API

• Explicitly declare:

○ Visibility modifiers

○ Type

Explicit API mode

Page 35: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

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

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

Explicit API modebuild.gradle

Page 36: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

• Explicit API mode in Kotlin 1.4

• Kotlin Symbol Processing

• R8 Kotlin Metadata support

Kotlin for library authors

Page 37: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Title safe

Kotlin Symbol Processing

Image safe area

Developer Preview

We need your feedback!

goo.gle/ksp

Page 38: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

• Support for

Kotlin metadata rewriting

• Android Gradle Plugin

4.1.0-beta03

R8

Page 39: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

This class was deprecated in API level R (Android 11)Use the standard java.util.concurrent or Kotlin concurrency utilities instead.

AsyncTaskDeprecated in Android 11

Page 40: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Async programmingWhat should we use?

Page 41: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Coroutines

Coroutines are the Kotlin way to do asynchronous programming.

Compiler support is stable since Kotlin 1.3, together with a robust kotlinx.coroutines library.

Brief overview

Page 42: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Professional developers who use Coroutines have reported seeing increased productivity

Coroutines

Source: Google Internal Data, May 2020

Page 43: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

CoroutinesviewModelScope.launch { val status = withContext(Dispatchers.IO) { val office = officeDetailsService.findOffice("US-BVE") db.officeDetails().insert(office) office.status } val isOpenText = if (status == OfficeStatus.OPEN) { "open :)" } else { "closed :(" } officeStatusLiveData.value = "Your office is $isOpenText"}

Page 44: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

viewModelScope.launch { val status = withContext(Dispatchers.IO) { val office = officeDetailsService.findOffice("US-BVE") db.officeDetails().insert(office) office.status } val isOpenText = if (status == OfficeStatus.OPEN) { "open :)" } else { "closed :(" } officeStatusLiveData.value = "Your office is $isOpenText"}

Coroutines

Page 45: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

viewModelScope.launch { val status = withContext(Dispatchers.IO) { val office = officeDetailsService.findOffice("US-BVE") db.officeDetails().insert(office) office.status } val isOpenText = if (status == OfficeStatus.OPEN) { "open :)" } else { "closed :(" } officeStatusLiveData.value = "Your office is $isOpenText"}

Coroutines

Page 46: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

viewModelScope.launch { val status = withContext(Dispatchers.IO) { val office = officeDetailsService.findOffice("US-BVE") db.officeDetails().insert(office) office.status } val isOpenText = if (status == OfficeStatus.OPEN) { "open :)" } else { "closed :(" } officeStatusLiveData.value = "Your office is $isOpenText"}

Coroutines

Page 47: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

viewModelScope.launch { val status = withContext(Dispatchers.IO) { val office = officeDetailsService.findOffice("US-BVE") db.officeDetails().insert(office) office.status } val isOpenText = if (status == OfficeStatus.OPEN) { "open :)" } else { "closed :(" } officeStatusLiveData.value = "Your office is $isOpenText"}

Coroutines

Page 48: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

viewModelScope.launch { val status = withContext(Dispatchers.IO) { val office = officeDetailsService.findOffice("US-BVE") db.officeDetails().insert(office) office.status } val isOpenText = if (status == OfficeStatus.OPEN) { "open :)" } else { "closed :(" } officeStatusLiveData.value = "Your office is $isOpenText"}

Coroutines

Page 49: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Kotlin coroutines are the recommended solution for async code

Page 50: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Structured concurrency

Non-blocking, sequential code

Cancellation propagation

Natural exception handling

Android 💚 coroutines

Page 51: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Coroutines & Jetpack

Page 52: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

RoomDatabase queries

@Daointerface 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>

}

Page 53: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

RoomDatabase queries

@Daointerface UsersDao {

@Query("SELECT * FROM users") suspend fun getUsers(): List<User>

@Query("SELECT * FROM users") fun getUsers(): Flow<List<User>>

}

Page 54: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

WorkManagerCoroutineWorker

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

override suspend fun doWork(): Result { val data = readFromDb() val serverData = uploadToServer(data) writeToDb(serverData)

return Result.success() }}

Page 55: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

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

override suspend fun load( params: LoadParams<Int> ): LoadResult<Int, User> {

... val response = backend.getUsers(page) return ... }}

PagingPagingSource

Page 56: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

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

PagingPager

Page 57: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

● Data storage solution

● SharedPreferences replacement

● Proto DataStore - typed objects

● Preferences DataStore -

key-value pairs

DataStore

Page 58: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

// Using Proto DataStore

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

DataStorePreferences DataStore and Proto DataStore

Page 59: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

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

Jetpack Compose

Page 60: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Kotlin coroutines are the recommended solution for async code

Page 61: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Title safe

Coroutines interop

Image safe area

For Java ↔ Kotlin users

Page 62: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

Coroutines learninggoo.gle/coroutines-posts● Coroutines: First things first● Cancellation in coroutines● Exceptions in Coroutines● The suspend modifier — 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

Page 63: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases
Page 64: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

• Productive Language

• Growing support

• New releases & tooling

• Coroutines recommended for

async programming

• Expanding docs, codelabs &

videos

Android 💚 Kotlin

Page 65: Kotlin 1.4 Online Event Florina Muntenescu · 2 days ago · Libraries Tools Learning Nullness annotations @ New lint checks Kotlin live templates R8 Optimizations New KTX releases

FMuntenescu

Thanks!Have a nice Kotlin