kotlin-coroutines

Guide Kotlin coroutine code review and debugging for Android and KMP.

119|14|Updated Mar 1, 2026
One-click install
npx skills add https://github.com/rcosteira79/android-skills --skill kotlin-coroutines-rcosteira79
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: kotlin-coroutines
Source: https://github.com/rcosteira79/android-skills/tree/main/skills/kotlin-coroutines
Command: npx skills add https://github.com/rcosteira79/android-skills --skill kotlin-coroutines-rcosteira79

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps developers write, review, and debug Kotlin coroutine code, ensuring efficient and robust asynchronous operations in Android and KMP projects.

Core Features & Use Cases

  • Dispatcher Management: Guides correct selection and injection of Dispatchers.Main, IO, and Default.
  • Scope Lifecycle: Explains and enforces proper use of viewModelScope, lifecycleScope, coroutineScope, and supervisorScope, while strictly prohibiting GlobalScope.
  • Structured Concurrency: Demonstrates patterns for parallel execution (async/await) and independent operations (launch within supervisorScope).
  • Cancellation & Error Handling: Provides strategies for cooperative cancellation (ensureActive) and robust exception management.
  • Testing: Offers guidance on using runTest and TestDispatcherProvider for reliable coroutine testing.

Quick Start

Use the kotlin-coroutines skill to explain how to inject dispatchers into a repository class.

Frequently Asked Questions about kotlin-coroutines

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

FAQPage Schema
How do I manage dispatchers correctly for async programming in Android and KMP?

Managing dispatchers in Android and KMP requires selecting the right scheduler for the task, such as Dispatchers.IO for network or file operations and Dispatchers.Main for UI updates. You should inject dispatchers into your repository classes to ensure main-safety and facilitate reliable coroutine testing.

What is the best way to handle coroutine scope lifecycle and avoid memory leaks?

The best way to handle coroutine scope lifecycle is by using structured concurrency with viewModelScope or lifecycleScope tied to Android components. You must strictly avoid using GlobalScope to prevent memory leaks and ensure operations are automatically cancelled when their parent scope is destroyed.

How do I test Kotlin coroutines reliably without flaky behaviors?

To test Kotlin coroutines reliably without flaky behaviors, use the runTest API along with a TestDispatcherProvider to control virtual time and dispatcher execution. This approach replaces real dispatchers, allowing you to validate asynchronous flows and timing-dependent logic deterministically.

Why does my Kotlin coroutine cancellation not stop the background task?

Kotlin coroutine cancellation fails to stop background tasks when the code is not cooperative. You must explicitly check for cancellation using ensureActive or yield control at regular intervals. Structured concurrency ensures that cancelling a parent scope properly propagates cancellation down to all child jobs.

Can I convert callback-based APIs to Kotlin Flows for structured concurrency?

Yes, you can convert callback-based APIs to Kotlin Flows to integrate legacy asynchronous code into structured concurrency. Using coroutine builder APIs like callbackFlow allows you to wrap existing callbacks, enabling proper lifecycle management, cancellation, and downstream exception handling within your KMP projects.

When should I use supervisorScope instead of coroutineScope for parallel execution?

You should use supervisorScope instead of coroutineScope when you need independent operations where a failure in one child should not cancel sibling operations. For standard parallel execution using async and await, use coroutineScope so exceptions properly propagate and cancel the entire group.