android-coroutines

Implement Kotlin structured concurrency with lifecycle-aware flows on Android using repeatOnLifecycle and runTest.

Updated Dec 4, 2025
One-click install
npx skills add https://github.com/thinhtt264/Snaplet-App --skill android-coroutines
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: android-coroutines
Source: https://github.com/thinhtt264/Snaplet-App/tree/main/.cursor/skills/android-coroutines
Command: npx skills add https://github.com/thinhtt264/Snaplet-App --skill android-coroutines

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Android apps often suffer from unsafe coroutine usage, lifecycle leaks, and unstructured concurrency. This skill provides authoritative rules and patterns to write robust, lifecycle-aware Kotlin Coroutines for Android, ensuring structured concurrency and modern best practices.

Core Features & Use Cases

  • Structured concurrency enforcement across ViewModel, Repository, and Worker boundaries to prevent leaks.
  • Lifecycle integration with viewModelScope, lifecycleScope, and repeatOnLifecycle for safe flow collection.
  • Reactive streams support with Flow, StateFlow, SharedFlow, and callbackFlow for responsive UIs.
  • Error handling, cancellation, and testing patterns (CoroutineExceptionHandler, TestDispatcher, runTest) to improve reliability.
  • Guidance to avoid hard-coding Dispatchers and to maintain main-safety across layers.

Quick Start

Refactor a sample Android feature to inject a CoroutineDispatcher and expose UI state via StateFlow in a ViewModel. Demonstrate lifecycle-safe collection using repeatOnLifecycle and viewModelScope for long-running tasks.

Frequently Asked Questions about android-coroutines

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

FAQPage Schema
How do I prevent lifecycle leaks when collecting Kotlin Coroutines Flow in Android?

To prevent lifecycle leaks when collecting Kotlin Coroutines Flow, use repeatOnLifecycle with lifecycleScope to ensure collection only occurs during active lifecycle states, automatically canceling jobs when the UI is destroyed.

What is structured concurrency in Android Kotlin Coroutines and how does it work?

Structured concurrency in Android Kotlin Coroutines enforces parent-child job hierarchies across ViewModel and Repository boundaries, ensuring child coroutines are automatically canceled when their parent scope is cleared, preventing memory leaks.

How do I inject Dispatchers and maintain main-safety in Android Kotlin Coroutines?

Maintain main-safety by injecting CoroutineDispatcher instances into ViewModels and Repositories rather than hard-coding Dispatchers, ensuring blocking operations run on background threads and UI updates resume on the main dispatcher.

How do I test Kotlin Coroutines in Android using runTest and TestDispatcher?

Test Kotlin Coroutines in Android by using runTest with TestDispatcher to control virtual time, avoiding flaky tests by skipping delays and verifying state flows and cancellation logic execute synchronously.

Can I use CoroutineExceptionHandler for error handling in Android ViewModelScope?

Yes, you can use CoroutineExceptionHandler for error handling in Android ViewModelScope to catch uncaught exceptions, but for recoverable errors you should use try-catch within coroutine builders.

When should I use StateFlow vs SharedFlow for reactive streams in Android Kotlin?

Use StateFlow for UI state requiring a single updated value exposed to collectors, and SharedFlow for events or transient updates without replaying initial values back to new subscribers.