Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines
- Length: 354 pages
- Edition: 1
- Language: English
- Publisher: O'Reilly Media
- Publication Date: 2021-12-28
- ISBN-10: 1492063002
- ISBN-13: 9781492063001
- Sales Rank: #4005929 (See Top 100 Books)
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
Preface Who Should Read This Book Why We Wrote This Book Navigating This Book Conventions Used in This Book Using Code Examples O’Reilly Online Learning How to Contact Us Acknowledgments 1. Kotlin Essentials The Kotlin Type System Primitive Types Null Safety The Unit Type Function Types Generics Variables and Functions Variables Lambdas Extension Functions Classes Class Initialization Properties lateinit Properties Lazy Properties Delegates Companion Objects Data Classes Enum Classes Sealed Classes Visibility Modifiers Summary 2. The Kotlin Collections Framework Collection Basics Java Interoperability Mutability Overloaded Operators Creating Containers Functional Programming Functional Versus Procedural: A Simple Example Functional Android Kotlin Transformation Functions The Boolean Functions Filter Functions Map flatMap Grouping Iterators Versus Sequences An Example The Problem The Implementation Summary 3. Android Fundamentals The Android Stack Hardware Kernel System Services Android Runtime Environment Applications The Android Application Environment Intents and Intent Filters Context Android Application Components: The Building Blocks The Activity and Its Friends Services Content Providers Broadcast Receivers Android Application Architectures MVC: The Foundation Widgets The Local Model Android Patterns Model–View–Intent Model–View–Presenter Model–View–ViewModel Summary 4. Concurrency in Android Thread Safety Atomicity Visibility The Android Threading Model Dropped Frames Memory Leaks Tools for Managing Threads Looper/Handler Executors and ExecutorServices Tools for Managing Jobs JobScheduler WorkManager Summary 5. Thread Safety An Example of a Thread Issue Invariants Mutexes Thread-Safe Collections Thread Confinement Thread Contention Blocking Call Versus Nonblocking Call Work Queues Back Pressure Summary 6. Handling Concurrency Using Callbacks Example-of-Purchase Feature Creating the App View-Model View Implement the Logic Discussion Limitations of the Threading Model Summary 7. Coroutines Concepts What Exactly Is a Coroutine? Your First Coroutine The async Coroutine Builder A Quick Detour About Structured Concurrency The Parent-Child Relationship in Structured Concurrency CoroutineScope and CoroutineContext Suspending Functions Suspending Functions Under the Hood Using Coroutines and Suspending Functions: A Practical Example Don’t Be Mistaken About the suspend Modifier Summary 8. Structured Concurrency with Coroutines Suspending Functions Set the Scene Traditional Approach Using java.util.concurrent.ExecutorService A Reminder About HandlerThread Using Suspending Functions and Coroutines Summary of Suspending Functions Versus Traditional Threading Cancellation Coroutine Lifecycle Cancelling a Coroutine Cancelling a Task Delegated to a Third-Party Library Coroutines That Are Cooperative with Cancellation delay Is Cancellable Handling Cancellation Causes of Cancellation Supervision supervisorScope Builder Parallel Decomposition Automatic Cancellation Exception Handling Unhandled Versus Exposed Exceptions Exposed Exceptions Unhandled Exceptions Summary Closing Thoughts 9. Channels Channels Overview Rendezvous Channel Unlimited Channel Conflated Channel Buffered Channel Channel Producers Communicating Sequential Processes Model and Architecture A First Implementation The select Expression Putting It All Together Fan-Out and Fan-In Performance Test Back Pressure Similarities with the Actor Model Execution Is Sequential Inside a Process Final Thoughts Deadlock in CSP TL;DR Limitations of Channels Channels Are Hot Summary 10. Flows An Introduction to Flows A More Realistic Example Operators Terminal Operators Examples of Cold Flow Usage Use Case #1: Interface with a Callback-Based API Use Case #2: Concurrently Transform a Stream of Values What Happens in Case of Error? Final Thoughts Use Case #3: Create a Custom Operator Usage Error Handling The try/catch Block Separation of Concern Is Important Exception Transparency Violation The catch Operator Materialize Your Exceptions Hot Flows with SharedFlow Create a SharedFlow Register a Subscriber Send Values to the SharedFlow Using SharedFlow to Stream Data Using SharedFlow as an Event Bus StateFlow: A Specialized SharedFlow An Example of StateFlow Usage Summary 11. Performance Considerations with Android Profiling Tools Android Profiler Network Profiler CPU Profiler Energy Profiler Memory Profiler Detecting Memory Leaks with LeakCanary Summary 12. Trimming Down Resource Consumption with Performance Optimizations Achieving Flatter View Hierarchy with ConstraintLayout Reducing Programmatic Draws with Drawables Minimizing Asset Payload in Network Calls Bitmap Pooling and Caching Reducing Unnecessary Work Using Static Functions Minification and Obfuscation with R8 and ProGuard Summary Index About the Authors
Donate to keep this site alive
How to download source code?
1. Go to: https://www.oreilly.com/
2. Search the book title: Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines
, sometime you may not get the results, please search the main title
3. Click the book title in the search results
3. Publisher resources
section, click Download Example Code
.
1. Disable the AdBlock plugin. Otherwise, you may not get any links.
2. Solve the CAPTCHA.
3. Click download link.
4. Lead to download server to download.