kotlin-concurrency-expert

Audit and fix Kotlin coroutine scope usage in Android projects.

8|1|Updated Jun 12, 2018
One-click install
npx skills add https://github.com/SheTieJun/BaseKit --skill kotlin-concurrency-expert-shetiejun
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: kotlin-concurrency-expert
Source: https://github.com/SheTieJun/BaseKit/tree/main/.trae/skills/kotlin-concurrency-expert
Command: npx skills add https://github.com/SheTieJun/BaseKit --skill kotlin-concurrency-expert-shetiejun

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This skill helps Android teams improve the reliability of Kotlin coroutines by auditing and remediating improper scope usage, poor lifecycle handling, and potential race conditions that can cause UI freezes or leaks.

Core Features & Use Cases

  • Review coroutine scope usage (viewModelScope, lifecycleScope, and custom scopes) to identify unstructured concurrency patterns.
  • Propose and apply fixes to enforce structured concurrency, proper dispatcher usage, and safe cancellation handling.
  • Provide remediation guidance for common UI and background tasks, including network I/O, disk I/O, and long-running operations, with code-review examples.
  • Improve testability by advocating injectable dispatchers and test-friendly scoping strategies.

Quick Start

Run a quick code-review pass to locate GlobalScope launches, then refactor to viewModelScope or lifecycleScope, moving heavy work to withContext(Dispatchers.IO) and replacing launchWhenStarted with repeatOnLifecycle.

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 main-thread blocking in Android?

Structured concurrency in Android ensures coroutine operations are tied to specific lifecycles, preventing leaks and UI freezes. It requires replacing GlobalScope with viewModelScope or lifecycleScope, and using repeatOnLifecycle instead of launchWhenStarted for safe cancellation.

What is the best way to review Kotlin coroutine scope usage for unstructured concurrency?

The best way to review coroutine scope usage is to scan for GlobalScope launches and improper dispatcher injection. Refactoring these into viewModelScope or lifecycleScope with withContext(Dispatchers.IO) enforces structured concurrency and resolves lifecycle safety issues.

How do I replace launchWhenStarted with repeatOnLifecycle for Android coroutines?

To replace launchWhenStarted with repeatOnLifecycle, wrap your coroutine collection logic inside a repeatOnLifecycle(Lifecycle.State.STARTED) block. This ensures the coroutine automatically suspends and cancels when the lifecycle drops below the target state, preventing UI leaks.

Does refactoring coroutines for lifecycle safety improve Android testability?

Refactoring coroutines for lifecycle safety directly improves Android testability. By advocating injectable dispatchers and test-friendly scoping strategies, you can easily swap Dispatchers.Main or Dispatchers.IO during unit tests to verify cancellation and threading behavior.

Why should I avoid GlobalScope when launching Kotlin coroutines in Android?

You should avoid GlobalScope because it creates unstructured concurrency, keeping coroutines alive independently of the Android lifecycle. This leads to memory leaks and potential crashes; using viewModelScope or lifecycleScope ensures coroutines are safely cancelled when the UI is destroyed.