kotlin-concurrency-expert

Review and remediate Kotlin coroutine usage for structured concurrency and lifecycle safety.

915|88|Updated Feb 6, 2024
One-click install
npx skills add https://github.com/new-silvermoon/awesome-android-agent-skills --skill kotlin-concurrency-expert-new-silvermoon
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: kotlin-concurrency-expert
Source: https://github.com/new-silvermoon/awesome-android-agent-skills/tree/main/.github/skills/kotlin-concurrency-expert
Command: npx skills add https://github.com/new-silvermoon/awesome-android-agent-skills --skill kotlin-concurrency-expert-new-silvermoon

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Kotlin Coroutines issues in Android code often lead to ANR, leaks, and lifecycle bugs. This Skill provides a disciplined review and remediation approach to ensure structured concurrency and lifecycle safety.

Core Features & Use Cases

  • Triage and identify improper coroutine usage (GlobalScope, missing cancellation, incorrect Dispatchers).
  • Propose minimal, safe patches: replace GlobalScope with lifecycle-aware scopes, inject dispatchers, encapsulate state and handle cancellation robustly.
  • Real-world use case: In a ViewModel-heavy app, prevent ANR by moving work off Main and ensuring proper scope handling.

Quick Start

Analyze the Android project for coroutine misuses and propose a minimal, safe patch set to replace GlobalScope with lifecycle-aware scopes, inject dispatchers for testability, and improve cancellation handling.

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 Kotlin coroutine leaks and ANR in my Android app?

Fix coroutine leaks and ANR by enforcing structured concurrency and lifecycle safety in your Android code. This involves replacing GlobalScope with lifecycle-aware scopes like viewModelScope, injecting dispatchers to move work off the Main thread, and ensuring proper cancellation handling.

Why does GlobalScope cause lifecycle bugs in Android coroutines?

GlobalScope causes lifecycle bugs because it creates coroutines that outlive their calling component, leading to memory leaks and crashes. Replacing GlobalScope with lifecycle-aware scopes like lifecycleScope or viewModelScope ensures coroutines are automatically cancelled when the associated lifecycle is destroyed.

How do I review my Kotlin code for improper coroutine usage?

Review Kotlin code for improper coroutine usage by triaging for GlobalScope instances, missing cancellation handling, and incorrect Dispatchers. Propose a minimal patch set encapsulating mutable state and injecting dispatchers for testability to enforce structured concurrency.

Does this approach work for ViewModel-heavy Android projects using coroutines?

Yes, this approach works for ViewModel-heavy Android projects using coroutines. It directly targets lifecycle safety in viewModelScope usage, preventing ANR by moving work off the Main thread and ensuring proper scope handling during configuration changes.

What is the best way to inject Dispatchers for coroutine testability in Android?

Inject Dispatchers for testability by passing them as dependencies rather than hardcoding them. This encapsulates mutable state and allows swapping real dispatchers with test dispatchers during unit testing, ensuring structured concurrency without side effects.

When should I handle coroutine cancellation in Android lifecycle scopes?

Handle coroutine cancellation in Android lifecycle scopes whenever the associated lifecycle is destroyed, such as when a ViewModel is cleared. Robust cancellation handling prevents leaks and ensures background tasks do not update destroyed UI components.