kotlin-concurrency-expert

Identify and fix improper Kotlin coroutine usage in Android projects.

Updated Nov 3, 2025
One-click install
npx skills add https://github.com/devanfer02/telnetquiz --skill kotlin-concurrency-expert-devanfer02
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: kotlin-concurrency-expert
Source: https://github.com/devanfer02/telnetquiz/tree/main/.claude/skills/kotlin-concurrency-expert
Command: npx skills add https://github.com/devanfer02/telnetquiz --skill kotlin-concurrency-expert-devanfer02

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill identifies and remediates improper Kotlin coroutine usage in Android projects, enforcing structured concurrency and lifecycle safety to improve reliability.

Core Features & Use Cases

  • Structured concurrency enforcement: replace GlobalScope with lifecycle-aware scopes and ensure proper use of viewModelScope and lifecycleScope.
  • Lifecycle safety and scope management: apply repeatOnLifecycle and correct dispatcher usage to prevent leaks and ANRs.
  • Cancellation and error handling: ensure CancellationException is rethrown and exceptions are handled without swallowing cancellation.
  • State encapsulation and testability: encapsulate MutableStateFlow, inject dispatchers for testability, and implement proper patterns like callbackFlow when needed.
  • Code modernization: adopt withContext for IO-bound work and modern coroutine best practices.

Quick Start

Review a Kotlin Android project and apply fixes to coroutine usage to align with structured concurrency best practices.

Frequently Asked Questions about kotlin-concurrency-expert

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

FAQPage Schema
How do I fix GlobalScope usage in Android Kotlin coroutines?

To fix GlobalScope usage in Android Kotlin coroutines, replace it with lifecycle-aware scopes like viewModelScope or lifecycleScope. This enforces structured concurrency and prevents memory leaks by automatically canceling jobs when their scopes are destroyed.

Why does my Android coroutine swallow CancellationException?

Android coroutine code swallows CancellationException when exceptions are caught and handled improperly. You must rethrow CancellationException within your error handling blocks to ensure structured concurrency and allow parent jobs to cancel child jobs correctly.

What's the best way to collect StateFlow safely in Android Fragments?

The best way to collect StateFlow safely in Android Fragments is using repeatOnLifecycle with lifecycleScope. This approach ties flow collection to specific lifecycle states, preventing unnecessary background processing and avoiding app not responding errors.

How do I inject CoroutineDispatcher for testability in Android?

To inject CoroutineDispatcher for testability in Android, pass dispatchers through ViewModel constructors instead of hardcoding them. This allows you to replace real dispatchers with test dispatchers during unit testing, ensuring deterministic and controlled coroutine execution.

When should I use withContext for IO-bound work in Kotlin?

You should use withContext for IO-bound work in Kotlin when performing disk or network operations. Switching to Dispatchers.IO inside a viewModelScope prevents blocking the main thread, avoiding UI freezes while maintaining structured concurrency boundaries.

Can I use callbackFlow to encapsulate asynchronous APIs in Android?

Yes, you can use callbackFlow to encapsulate asynchronous APIs in Android. It converts callback-based APIs into cold flows, allowing proper structured concurrency management, cancellation handling, and state exposure within lifecycle-aware components like ViewModel.