Kotlin 1.4 Online Event Florina Muntenescu

Transcription

Kotlin 1.4 Online EventState of Kotlin inAndroidFlorina Muntenescu FMuntenescuOctober, 2020

Why Android KotlinExpressivenessImage safe area

Why Android KotlinSafetyExpressivenessImage safe area

Kotlin on Android in 202020%Less likely to crashSource: Google Internal Data, July 2020

Why Android KotlinSafetyExpressivenessImage safe areaInteroperability

Why Android KotlinSafetyExpressivenessImage safe areaInteroperabilityStructuredconcurrency

Kotlin on Android in 202050%More likely to bevery satisfiedSource: Google Internal Data, May 2020

Kotlin on Android in 202060%Pro Android developersuse KotlinSource: Google Internal Data, May 2020

Kotlin on Android in 202070% Top 1k apps containKotlin codeSource: Google Internal Data, May 2020

Google’s contributionsto KotlinCo-leading Kotlin FoundationContributing to the KotlincompilerDeveloping Kotlin related toolingand libraries

Google’s contributionsto teaching KotlinDocs - d.android.com/kotlinSamples - github.com/androidCodelabs - goo.gle/kotlin-codelabsArticles - goo.gle/kotlin-postsVideos - goo.gle/kotlin-videos

Google’s contributionsto teaching KotlinDocs - d.android.com/kotlinSamples - github.com/androidCodelabs - goo.gle/kotlin-codelabsArticles - goo.gle/kotlin-postsVideos - goo.gle/kotlin-videosFaculty training

Since last year’s Google I/O.LibrariesToolsLearningKotlin-first librariesKotlin live templatesVocabulary seriesNew KTX releasesNew lint checksSamples and docsR8 OptimizationsKotlin/Everywhere@Nullnessannotations

Since last year’s Google I/O.LibrariesToolsLearningKotlin-first librariesKotlin live templatesVocabulary seriesNew KTX releasesNew lint checksSamples and docsR8 OptimizationsKotlin/Everywhere@Nullnessannotations

Since last year’s Google I/O.LibrariesToolsLearningKotlin-first librariesKotlin live templatesVocabulary seriesNew KTX releasesNew lint checksSamples and docsR8 OptimizationsKotlin/Everywhere@Nullnessannotations

Since last year’s Google I/O.LibrariesToolsLearningKotlin-first librariesKotlin live templatesVocabulary seriesNew KTX releasesNew lint checksSamples and docsR8 OptimizationsKotlin/Everywhere@Nullnessannotations

Since last year’s Google I/O.LibrariesToolsLearningKotlin-first librariesKotlin live templatesVocabulary seriesNew KTX releasesNew lint checksSamples and docsR8 OptimizationsKotlin/Everywhere@Nullnessannotations

Since last year’s Google I/O.LibrariesToolsLearningKotlin-first librariesKotlin live templatesVocabulary seriesNew KTX releasesNew lint checksSamples and docsR8 OptimizationsKotlin/Everywhere@Nullnessannotations

Since last year’s Google I/O.LibrariesToolsLearningKotlin-first librariesKotlin live templatesVocabulary seriesNew KTX releasesNew lint checksSamples and docsR8 OptimizationsKotlin/Everywhere@Nullnessannotations

Since last year’s Google I/O.LibrariesToolsLearningKotlin-first librariesKotlin live templatesVocabulary seriesNew KTX releasesNew lint checksSamples and docsR8 OptimizationsKotlin/Everywhere@Nullnessannotations

Since last year’s Google I/O.LibrariesToolsLearningKotlin-first librariesKotlin live templatesVocabulary seriesNew KTX releasesNew lint checksSamples and docsR8 OptimizationsKotlin/Everywhere@Nullnessannotations

Since last year’s Google I/O.LibrariesToolsLearningKotlin-first librariesKotlin live templatesVocabulary seriesNew KTX releasesNew lint checksSamples and docsR8 OptimizationsKotlin/Everywhere@Nullnessannotations

Jetpack Compose Modern, declarative UI toolkit Built on Kotlin Unbundled

Compose projectstructureCompose MaterialSurface, Buttons, Tabs, ThemesAndroid StudioCompose FoundationLive preview, Apply ChangesStandard layouts, interactionsCompose Compiler PluginCompose UICode generation extensionsInput, Measure, Layout, DrawingkotlincCompose RuntimeUpstream Kotlin compiler (1.4)Tree management, EffectsBuild time (development host)Runtime (on device)

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

Kotlin at GoogleSource: Google Internal Data, May 2020

Kotlin at Google 2M lines of Kotlin code Used on server-side Kotlin in OSS projects: gRPC ProtoBuf(coming soon)

Kotlin 1.4 preview

Kotlin Tooling Performance“Faster Please!”

KotlinToolingPerformanceKotlin ToolingPerformance Incremental AnnotationProcessors

Kotlin Tooling Performance Incremental AnnotationProcessors IDE Performance

Kotlin Tooling Performance Incremental AnnotationProcessors IDE Performance Gradle improvements

Kotlin for library authors Explicit API mode in Kotlin 1.4 Kotlin Symbol Processing R8 Kotlin Metadata support

Explicit API mode Be intentional about yourpublic API Explicitly declare: Visibility modifiers Type

Explicit API modebuild.gradlekotlin {// for strict modeexplicitApi()// orexplicitApi 'strict'// for warning modeexplicitApiWarning()// orexplicitApi 'warning'}

Kotlin for library authors Explicit API mode in Kotlin 1.4 Kotlin Symbol Processing R8 Kotlin Metadata support

Kotlin Symbol ProcessingDeveloper PreviewImage safe areaWe need your feedback!Titlesafegoo.gle/ksp

R8 Support forKotlin metadata rewriting Android Gradle Plugin4.1.0-beta03

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

Async programmingWhat should we use?

CoroutinesBrief overviewCoroutines are the Kotlin wayto do asynchronous programming.Compiler support is stable sinceKotlin 1.3,together with a robustkotlinx.coroutines library.

CoroutinesProfessional developerswho use Coroutineshave reported seeingincreased productivitySource: Google Internal Data, May 2020

CoroutinesviewModelScope.launch {val status withContext(Dispatchers.IO) {val office Details().insert(office)office.status}val isOpenText if (status OfficeStatus.OPEN) {"open :)" } else { "closed :(" }officeStatusLiveData.value "Your office is isOpenText"}

CoroutinesviewModelScope.launch {val status withContext(Dispatchers.IO) {val office Details().insert(office)office.status}val isOpenText if (status OfficeStatus.OPEN) {"open :)" } else { "closed :(" }officeStatusLiveData.value "Your office is isOpenText"}

CoroutinesviewModelScope.launch {val status withContext(Dispatchers.IO) {val office Details().insert(office)office.status}val isOpenText if (status OfficeStatus.OPEN) {"open :)" } else { "closed :(" }officeStatusLiveData.value "Your office is isOpenText"}

CoroutinesviewModelScope.launch {val status withContext(Dispatchers.IO) {val office Details().insert(office)office.status}val isOpenText if (status OfficeStatus.OPEN) {"open :)" } else { "closed :(" }officeStatusLiveData.value "Your office is isOpenText"}

CoroutinesviewModelScope.launch {val status withContext(Dispatchers.IO) {val office Details().insert(office)office.status}val isOpenText if (status OfficeStatus.OPEN) {"open :)" } else { "closed :(" }officeStatusLiveData.value "Your office is isOpenText"}

CoroutinesviewModelScope.launch {val status withContext(Dispatchers.IO) {val office Details().insert(office)office.status}val isOpenText if (status OfficeStatus.OPEN) {"open :)" } else { "closed :(" }officeStatusLiveData.value "Your office is isOpenText"}

Kotlin coroutines are therecommended solution forasync code

Android coroutinesStructured concurrencyNon-blocking, sequential codeCancellation propagationNatural exception handling

Coroutines & Jetpack

RoomDatabase queries@Daointerface UsersDao {@Insertsuspend fun insertUsers(vararg users: User)@Updatesuspend fun updateUsers(vararg users: User)@Deletesuspend fun deleteUsers(vararg users: User)@Query("SELECT * FROM users")suspend fun getUsers(): List User }

RoomDatabase queries@Daointerface UsersDao {@Query("SELECT * FROM users")suspend fun getUsers(): List User @Query("SELECT * FROM users")fun getUsers(): Flow List User }

WorkManagerCoroutineWorkerclass 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()}}

PagingPagingSourceclass UsersPagingSource(val backend: UsersService): PagingSource Int, User () {override suspend fun load(params: LoadParams Int ): LoadResult Int, User {}}.val response backend.getUsers(page)return .

PagingPagerval users: Flow PagingData User Pager(config PagingConfig(pageSize 10)) {UsersPagingSource(usersService)}.flow

DataStore Data storage solution SharedPreferences replacement Proto DataStore - typed objects Preferences DataStore key-value pairs

DataStorePreferences DataStore and Proto DataStore// Using Proto DataStoreval myCounterFlow: Flow Int settingsDataStore.data.map { settings - settings.myCounter}

Jetpack Compose@Composablefun Search(// .) {launchInComposition(state.query.text) {state.searching truestate.searchResults SearchRepo.search(state.query.text)state.searching false}}

Kotlin coroutines are therecommended solution forasync code

Coroutines interopFor Java Kotlin usersImage safe areaTitlesafe

Coroutines learninggoo.gle/coroutines-posts Coroutines: First things first Cancellation in coroutines Exceptions in Coroutines The suspend modifier — Under the hoodgoo.gle/coroutines-videos Coroutines 101 Android Coroutines: How to manage async tasks in Kotlingoo.gle/kotlin-codelabs Use Kotlin Coroutines in your Android App Learn advanced coroutines with Kotlin Flow and LiveData Building a Kotlin extensions library

Android Kotlin Productive Language Growing support New releases & tooling Coroutines recommended forasync programming Expanding docs, codelabs &videos

Thanks!Have a nice Kotlin FMuntenescu

Pro Android developers use Kotlin Source: Google Internal Data, May 2020 60% Kotlin on Android in 2020. 70% Top 1k apps contain Kotlin code Source: Google Internal Data, May 2020 Kotlin on Andro