Share E-Book
Scan to open this page

Scan with your phone to open this page

AuthorLaurence, Pierre-Olivier, Hinchman-Dominguez, Amanda, Meike, G. Blake, Dunn, Mike

Android development is so vast that mastering this mobile operating system can seem daunting--particularly now that Kotlin has become the official Android development language. This book helps Android developers make the transition from Java to Kotlin and shows them how Kotlin provides a true advantage for gaining control over asynchronous computations. By focusing specifically on coroutines, a new asynchronous programming paradigm, this book describes how you can achieve structured concurrency with Kotlin. Authors Pierre-Oliver Laurence, Amanda Hinchman-Dominguez, and Mike Dunn provide implementations of the most common tasks in native Android development. The basics of the Kotlin language and the Android architecture Data transformations in Kotlin Android fundamentals in memory and threading Concurrency with coroutines Channels and flows Android profiling tools

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
# Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines ## 【One-Line Pitch】 A practical guide for Android developers transitioning from Java to Kotlin, focusing on how coroutines and structured concurrency solve real-world asynchronous programming challenges. Ideal for intermediate developers who want to write safer, more efficient Android apps while mastering Kotlin's modern language features. ## 【Book Arc】 - **Opening (~0%–10%)**: Introduces Kotlin fundamentals with a Java-to-Kotlin comparison approach, covering null safety, extension functions, sealed classes, and lazy initialization—establishing why Kotlin's type system prevents entire categories of runtime crashes. - **Early (~10%–23%)**: Dives into the Kotlin Collections Framework, explaining functional programming concepts like map, flatMap, and filter through practical examples, including building CSV files from complex data models. - **Early (~23%–32%)**: Shifts to Android platform fundamentals—the Linux kernel foundation, hardware constraints (especially power), Activity lifecycle and back stack navigation, ContentProviders, and the MVVM architecture pattern. - **Middle (~32%–48%)**: Explores memory management and threading challenges, including activity leaks, synchronization, thread contention, and Android's background execution limits introduced in API 26+. - **Middle (~48%–end)**: Transitions into concurrency solutions, examining immutability trade-offs, locking strategies, and setting the stage for coroutines as the structured concurrency answer to Android's threading complexity. ## 【Key Takeaways】 - **Kotlin's null safety eliminates an entire class of crashes** (Early): The type system forces you to handle nullable values explicitly with `?.` and `?:` operators, trading a few question marks for dramatically more robust code compared to Java's NullPointerException-prone approach. - **Extension functions and lambdas make code more readable and expressive** (Early): Adding methods to classes you don't own becomes trivial, and trailing lambda syntax enables clean, DSL-like APIs that read naturally. - **Sealed classes provide type-safe state modeling** (Early): Unlike enums, sealed classes allow different subtypes with distinct data, making them perfect for representing outcomes like `Success` and `Failure` in a way that `when` expressions can handle exhaustively. - **Functional collection operations are essential for real Android work** (Early): Understanding `map`, `flatMap`, and `filter` transforms data pipelines elegantly—flatMap specifically "flattens away" nested containers, which is crucial for merging and reshaping data structures. - **Android's hardware constraints shape its architecture** (Early): Power is the dominant constraint—unlike desktop OSes, Android assumes limited battery, which drives everything from kernel design to background execution policies. - **Activity leaks are a serious, subtle memory problem** (Middle): Any reference to an Activity held by a background thread prevents garbage collection, and the solution requires understanding both the lifecycle and how implicit references work. - **Thread synchronization requires understanding memory visibility** (Middle): `@Synchronized` isn't just about mutual exclusion—it ensures values are "published" from registers/caches to memory visible across threads, preventing subtle concurrency bugs. - **Android 8.0+ fundamentally changed background execution** (Middle): Foreground-only guarantees, broadcast restrictions, and service limitations mean developers must rethink how to perform background work—paving the way for coroutines as the solution. ## 【Reading Tips】 - **Skim the Java-to-Kotlin comparisons in early chapters** if you're already comfortable with Kotlin basics—the real value is in the "why" explanations, not the syntax translations. - **Deep-read the collections chapter** (around 13%–23%): The flatMap and data transformation examples are foundational for everything that follows, and the IntelliJ type-hint trick (Ctrl+Shift+P) is worth remembering. - **Pay special attention to the Activity lifecycle and memory sections** (around 29%–42%): These explain the *problems* that coroutines later solve, so understanding the pain points makes the solutions click. - **Don't skip the Android background execution limits** (around 42%–48%): These API 26+ constraints are the practical motivation for structured concurrency—knowing them helps you appreciate why coroutines matter. - **The excerpts don't cover the actual coroutine implementation details**—if you need hands-on coroutine code, you'll need to read the later chapters directly. ## 【Coverage Limits】 This guide is based on excerpts covering roughly the first half of the book (through ~48%). The detailed coroutine implementations, channels, flows, and profiling tools mentioned in the book's description are not covered in the available material. ##
Excerpt 1
by context. TIP This element signifies a tip or suggestion. NOTE This element signifies a general note. showNameLength(name) // error! Won't compile because...
View in text
Excerpt 2
Collection Basics Functional Programming We operate on them! Nearly all of the operations that we will discuss here are based on the paradigm of functional p...
View in text
Excerpt 3
e robust access while harnessing the full power of SQLite.” For more information on how to save data in a local database using Room, check out the Android De...
View in text
Excerpt 4
sks: synching with a remote, recording location, and so on. In most cases, the constraints can be mitigated using the JobScheduler or Jetpack’s WorkManager....
View in text
Excerpt 5
g out the result. It’s important to remember the following: The async coroutine builder is intended for parallel decomposition of work—that is, you explicitl...
View in text
Excerpt 6
traditional” approach. Other libraries like RxJava/RxKotlin and Arrow could have been used to perform essentially the same thing. The logic is made of severa...
View in text
Excerpt 7
ch(Dispatchers.IO) in itself has an insignificant overhead, while fetching the ShapeData could require a REST API call on a server with limited bandwidth. So...
View in text
Excerpt 8
Dispatchers.Default. It doesn’t matter in which context the resulting flow will be collected; the previously mentioned operators will be executed using Dispa...
View in text
Tags
AI categories
androidkotlinProgramming
ISBN: 1492063002
Publisher: O'Reilly Media
Publish Year: 2023
Language: English
Pages: 325
File Format: PDF
File Size: 7.5 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…