Programming Android with Kotlin

Book description

Developing applications for the Android mobile operating system can seem daunting, particularly if it requires learning a new programming language: Kotlin, now Androidâ??s officialdevelopment language. With this practical book, Android developers will learn how to make the transition from Java to Kotlin, including how Kotlin provides a true advantage for gaining control over asynchronous computations.

Authors Pierre-Olivier Laurence, Amanda Hinchman-Dominguez, G. Blake Meike, and Mike Dunn explore implementations of the most common tasks in native Android development, and show you how Kotlin can help you solve concurrency problems. With a focus on structured concurrency, a new asynchronous programming paradigm, this book will guide you through one of Kotlin's most powerful constructs, coroutines.

  • Learn about Kotlin essentials and the Kotlin Collections Framework
  • Explore Android fundamentals: the operating system and the application container and its components
  • Learn about thread safety and how to handle concurrency
  • Write sequential, asynchronous work at a low cost
  • Examine structured concurrency with coroutines, and learn how channels make coroutines communicate
  • Learn how to use flows for asynchronous data processing
  • Understand performance considerations using Android profiling tools
  • Use performance optimizations to trim resource consumption

Publisher resources

View/Submit Errata

Table of contents

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

Product information

  • Title: Programming Android with Kotlin
  • Author(s): Pierre-Olivier Laurence, Amanda Hinchman-Dominguez, Mike Dunn, G. Blake Meike
  • Release date: December 2021
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781492063001