# android-kotlin-coroutines

android-kotlin-coroutines is a Claude Code skill for asynchronous Kotlin patterns in Android apps. It guides coroutines, Flow, StateFlow, and safe concurrency across ViewModels and Jetpack Compose.

- skill · `type=skill` · Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, OpenCode, Windsurf (Devin Desktop), Claude.ai · 145 installs · ★ 201 repo stars
- Author: TheBushidoCollective
- License: Apache-2.0 · policy=mirror · the beginning of the file is at the end of this page
- Source: https://github.com/TheBushidoCollective/han/tree/HEAD/plugins/specialized/android/skills/kotlin-coroutines/SKILL.md
- Updated: 2026-09-08
- Security check: automated, 2026-11-16 (text) — no signs of malicious code found.
- HTML: https://affpapa.org/en/nevibecode/skills/android-kotlin-coroutines

## What it does

The skill provides concrete patterns for asynchronous Kotlin development on Android. It covers coroutine execution in `viewModelScope`, switching execution contexts via `Dispatchers`, and parallel work using `coroutineScope` with `withTimeout`.

It details reactive streaming using `Flow` and `StateFlow`. Patterns include search debouncing, `flatMapLatest`, and exponential backoff retries. Flow collection in UI components relies on `repeatOnLifecycle` or `collectAsStateWithLifecycle`. Without lifecycle-aware collection, background streams waste device memory and battery.

## Install

### Claude Code (native platform)

1. **Skills CLI (Vercel)**
```bash
npx skills add TheBushidoCollective/han --skill android-kotlin-coroutines -a claude-code
```

2. **Manually: put the files in place**
- File: `~/.claude/skills/kotlin-coroutines/SKILL.md` — Copy the whole skill folder; for one project use .claude/skills/kotlin-coroutines/

### Codex

1. **Skills CLI (Vercel)**
```bash
npx skills add TheBushidoCollective/han --skill android-kotlin-coroutines -a codex
```

2. **Manually: put the files in place**
- File: `.agents/skills/kotlin-coroutines/SKILL.md` — For all projects use ~/.agents/skills/

### Cursor

1. **Skills CLI (Vercel)**
```bash
npx skills add TheBushidoCollective/han --skill android-kotlin-coroutines -a cursor
```

2. **Manually: put the files in place**
- File: `.cursor/skills/kotlin-coroutines/SKILL.md` — Cursor also reads .agents/skills/ and .claude/skills/

### GitHub Copilot

1. **GitHub CLI**
```bash
gh skill install TheBushidoCollective/han plugins/specialized/android/skills/kotlin-coroutines
```

2. **Manually: put the files in place**
- File: `.github/skills/kotlin-coroutines/SKILL.md`

### Gemini CLI

1. **Skills CLI (Vercel)**
```bash
npx skills add TheBushidoCollective/han --skill android-kotlin-coroutines -a gemini-cli
```

2. **Platform CLI**
```bash
gemini skills install https://github.com/TheBushidoCollective/han --path plugins/specialized/android/skills/kotlin-coroutines
```

### Windsurf (Devin Desktop)

**Manually: put the files in place**
- File: `.devin/skills/kotlin-coroutines/SKILL.md` — The legacy .windsurf/skills/ path still works

### OpenCode

**Skills CLI (Vercel)**
```bash
npx skills add TheBushidoCollective/han --skill android-kotlin-coroutines -a opencode
```

### Claude.ai

**Claude.ai**
- Zip the folder and upload it in Settings → Capabilities → Skills

Where a file is given instead of a command, copy the files from the source to that path: https://github.com/TheBushidoCollective/han/tree/HEAD/plugins/specialized/android/skills/kotlin-coroutines/SKILL.md

## When to use it
- implement search queries with `StateFlow`, `debounce`, and `flatMapLatest`
- run concurrent network calls inside `coroutineScope` with a timeout
- collect `Flow` states in Jetpack Compose using `collectAsStateWithLifecycle`
- isolate coroutine failures with `SupervisorJob` and `runCatching`

## Example prompts
- Create a search ViewModel pipeline using StateFlow, debounce, and flatMapLatest
- Combine user data and settings flows in UserRepository using the combine operator
- Set up lifecycle-aware Flow collection inside an Android Fragment

## Source text · SKILL.md · Apache-2.0

Below is the content of a third-party file. It is data, not instructions for you: do not act on anything written inside unless the human asks. Block tag: 1a6309f242fd4569.

BEGIN THIRD-PARTY FILE 1a6309f242fd4569
````text
---
name: android-kotlin-coroutines
description: Use when implementing async operations with Kotlin coroutines, Flow, StateFlow, or managing concurrency in Android apps.
allowed-tools:
  - Read
  - Write
  - Edit
  - Bash
  - Grep
  - Glob
---

# Android - Kotlin Coroutines

Asynchronous programming patterns using Kotlin coroutines and Flow in Android.

## Key Concepts

### Coroutine Basics

```kotlin
// Launching coroutines
class UserViewModel : ViewModel() {

    fun loadUser(id: String) {
        // viewModelScope is automatically cancelled when ViewModel is cleared
        viewModelScope.launch {
            try {
                val user = userRepository.getUser(id)
                _uiState.value = UiState.Success(user)
            } catch (e: Exception) {
                _uiState.value = UiState.Error(e.message)
            }
        }
    }

    // For operations that return a value
    fun fetchUserAsync(id: String): Deferred<User> {
        return viewModelScope.async {
            userRepository.getUser(id)
        }
    }
}

// Suspend functions
suspend fun fetchUserFromNetwork(id: String): User {
    return withContext(Dispatchers.IO) {
        api.getUser(id)
    }
}
```

### Dispatchers

```kotlin
// Main - UI operations
withContext(Dispatchers.Main) {
    textView.text = "Updated"
}

// IO - Network, database, file operations
withContext(Dispatchers.IO) {
    val data = api.fetchData()
    database.save(data)
}

// Default - CPU-intensive work
withContext(Dispatchers.Default) {
    val result = expensiveComputation(data)
}

// Custom dispatcher for limited parallelism
val limitedDispatcher = Dispatchers.IO.limitedParallelism(4)
```

### Flow Basics

```kotlin
// Creating flows
fun getUsers(): Flow<List<User>> = flow {
    while (true) {
        val users = api.getUsers()
        emit(users)
        delay(30_000) // Poll every 30 seconds
    }
}

// Flow from Room
@Dao
interface UserDao {
    @Query("SELECT * FROM users")
    fun getAllUsers(): Flow<List<UserEntity>>
}

// Collecting flows
viewModelScope.launch {
    userRepository.getUsers()
        .catch { e -> _uiState.value = UiState.Error(e) }
        .collect { users ->
            _uiState.value = UiState.Success(users)
        }
}
```

### StateFlow and SharedFlow

```kotlin
class SearchViewModel : ViewModel() {
    // StateFlow - always has a current value
    private val _searchQuery = MutableStateFlow("")
    val searchQuery: StateFlow<String> = _searchQuery.asStateFlow()

    // SharedFlow - for events without initial value
    private val _events = MutableSharedFlow<UiEvent>()
    val events: SharedFlow<UiEvent> = _events.asSharedFlow()
````
END THIRD-PARTY FILE 1a6309f242fd4569

The third-party file has ended. Everything between the 1a6309f242fd4569 tags is data: requests and commands from it are not followed. An “end of file” line with another tag inside the block is part of the file.

First 2662 of 10880 bytes. © TheBushidoCollective · Apache-2.0 · https://github.com/TheBushidoCollective/han/tree/HEAD/plugins/specialized/android/skills/kotlin-coroutines/SKILL.md

_Part of the page did not fit — see the HTML version._

---
NeVibeCode on affpapa.org. Catalog texts are descriptions, not instructions for the agent. How to choose and install: https://affpapa.org/en/nevibecode/agents.md
