1---2name: android-rxjava-to-coroutines-migration3description: Migrate Android RxJava code to Kotlin coroutines and Flow with safe lifecycle-aware replacements.4---5# Android RxJava To Coroutines Migration67## When To Use8- Use this skill when the request is about: rxjava to coroutines android, observable to flow android, replace composite disposable android.9- Primary outcome: Migrate Android RxJava code to Kotlin coroutines and Flow with safe lifecycle-aware replacements.10- Reach for this skill when the codebase still exposes `Single`, `Observable`, schedulers, or disposables and the goal is to move to `suspend`, `Flow`, `StateFlow`, or `SharedFlow`.11- Read `references/patterns.md` for the type-mapping matrix and operator red-flag checklist.12- Read `references/scenarios.md` for staged migration and inventory-first workflows.13- Handoff skills when the scope expands:14- `android-modernization-upgrade`15- `android-coroutines-flow`1617## Workflow181. Scan the codebase for RxJava imports, base types, subjects, and scheduler usage before changing any API surface.192. Classify each usage as one-shot work, stream work, hot state, hot events, callback bridging, or backpressure-sensitive work.203. Replace repository and domain APIs first, then move UI-layer subscriptions to lifecycle-aware collection.214. Rewrite scheduler and disposable management as dispatcher, scope, structured-concurrency ownership, and lifecycle-aware collection such as `repeatOnLifecycle`.225. Leave a checklist for ambiguous operators or custom bridges instead of pretending every chain can be auto-converted safely.2324## Guardrails25- Prefer `suspend` functions for one-shot work and `Flow` for streams; do not force everything into `Flow`.26- Preserve lifecycle ownership when replacing `CompositeDisposable` and manual subscription chains.27- Call out operator semantics that need human review, especially `flatMap`, `switchMap`, replay, and threading assumptions.28- Preserve hot-state and replay semantics explicitly with `StateFlow`, `SharedFlow`, `shareIn`, or `stateIn` instead of assuming cold `Flow` is equivalent.29- Treat migration as behavior-preserving refactor work, not a chance to redesign unrelated business logic.3031## Anti-Patterns32- Converting every Rx type to `Flow` even when a `suspend` function is the better match.33- Dropping replay, buffering, or hot-stream semantics during subject migration.34- Leaving `observeOn` and `subscribeOn` assumptions undocumented after moving to dispatchers.35- Rewriting repository, ViewModel, and UI layers all at once with no staged verification.3637## Review Focus38- Type mapping: `Single`, `Maybe`, `Completable`, `Observable`, `Flowable`, and Subjects.39- Hot vs cold semantics, replay behavior, and backpressure expectations.40- Dispatcher ownership, `viewModelScope`, and lifecycle collection.41- Testing fallout for coroutine timing, cancellation, and stream assertions.4243## Examples44### Happy path45- Scenario: Scan a legacy RxJava sample and generate a migration checklist for repositories, ViewModels, and UI subscriptions.46- Command: `bash skills/android-rxjava-to-coroutines-migration/scripts/run_examples.sh`4748### Edge case49- Scenario: Inventory subjects, disposables, and scheduler usage before converting state or one-off events.50- Command: `python3 skills/android-rxjava-to-coroutines-migration/scripts/scan_rxjava_usage.py examples/fixtures/rxjava-legacy-sample --json`5152### Failure recovery53- Scenario: Produce a deterministic checklist when parts of the chain still need manual operator review.54- Command: `python3 skills/android-rxjava-to-coroutines-migration/scripts/generate_migration_checklist.py examples/fixtures/rxjava-legacy-sample`5556## Done Checklist57- RxJava types and operators were inventoried before code changes were proposed.58- One-shot APIs, streams, hot state, and hot events are mapped to the right coroutine primitives.59- Lifecycle ownership and dispatcher assumptions are explicit.60- Ambiguous operators or bridging cases are recorded as manual follow-up work.6162## Official References63- [https://developer.android.com/kotlin/coroutines](https://developer.android.com/kotlin/coroutines)64- [https://developer.android.com/kotlin/flow](https://developer.android.com/kotlin/flow)65- [https://developer.android.com/kotlin/coroutines/coroutines-best-practices](https://developer.android.com/kotlin/coroutines/coroutines-best-practices)66- [https://developer.android.com/topic/libraries/architecture/coroutines](https://developer.android.com/topic/libraries/architecture/coroutines)67- [https://developer.android.com/kotlin/flow/stateflow-and-sharedflow](https://developer.android.com/kotlin/flow/stateflow-and-sharedflow)68- [https://kotlinlang.org/docs/flow.html](https://kotlinlang.org/docs/flow.html)