android-coroutines-expert

Guide Android developers to implement lifecycle-aware Kotlin Coroutines with viewModelScope and TestDispatcher.

Updated Apr 6, 2026
One-click install
npx skills add https://github.com/jperezdelreal/GymBro --skill android-coroutines-expert
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: android-coroutines-expert
Source: https://github.com/jperezdelreal/GymBro/tree/main/.squad/skills/android/android-coroutines-expert
Command: npx skills add https://github.com/jperezdelreal/GymBro --skill android-coroutines-expert

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Android apps often struggle with lifecycle-unsafe coroutine usage, leading to leaks, crashes, or unresponsive UI. This guide delivers practical patterns to write lifecycle-aware, testable coroutine code using viewModelScope, proper Dispatchers, Flow in Compose, and robust error handling.

Core Features & Use Cases

  • ViewModel-scoped coroutines with viewModelScope for lifecycle safety across config changes.
  • Dispatcher guidance for Main, IO, and Default to optimize UI responsiveness and background work.
  • Flow collection in Compose with collectAsStateWithLifecycle for safe UI state exposure.
  • LaunchedEffect and side-effect handling for one-time or key-based actions.
  • Managing long-running jobs with cancellation and proper cleanup.
  • Testing strategies with TestDispatcher, runTest, and Turbine for Flow emissions.
  • Anti-patterns to avoid, including GlobalScope, blocking Main thread, and unchecked exceptions.

Quick Start

Refactor a sample Android ViewModel to use viewModelScope, the correct Dispatchers, and collectAsStateWithLifecycle for Compose to improve lifecycle safety and testability.

Frequently Asked Questions about android-coroutines-expert

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I use Kotlin Coroutines in Android ViewModels for lifecycle safety?

Use Kotlin Coroutines in Android ViewModels via viewModelScope to ensure lifecycle safety across configuration changes. This built-in scope automatically cancels running jobs when the ViewModel is cleared, preventing memory leaks and crashes.

What is the best way to collect Flow in Jetpack Compose without causing UI issues?

The best way to collect Flow in Jetpack Compose is using collectAsStateWithLifecycle. This function ensures Flow collection only occurs when the UI is active, preventing unnecessary background processing and preserving device battery life.

How do I test Kotlin Coroutines and Flow in Android?

Test Kotlin Coroutines and Flow in Android using TestDispatcher and runTest to control virtual time. For testing Flow emissions specifically, use the Turbine library to assert sequential values and verify lifecycle-safe collection behavior.

Why do my Android Coroutines cause crashes or unresponsive UIs on the Main thread?

Android Coroutines cause crashes or unresponsive UIs when blocking the Main thread or using GlobalScope. Fix this by applying correct Dispatchers, switching to IO for network or disk operations, and Default for CPU-intensive background work.

How do I handle side effects with LaunchedEffect in Compose when using Coroutines?

Handle side effects with LaunchedEffect in Compose by tying Coroutines execution to specific UI keys. This pattern safely manages one-time or key-based actions, ensuring jobs are properly cancelled and cleaned up when the composition leaves.

What are common Kotlin Coroutines anti-patterns in Android development?

Common Kotlin Coroutines anti-patterns in Android include using GlobalScope, blocking the Main thread, and leaving exceptions unchecked. Avoid these by relying on viewModelScope, applying correct Dispatchers, and implementing robust error handling.