一键导入
rxjava-migration
Use only when the user explicitly requests migration from RxJava to Kotlin coroutines and/or flows.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Use only when the user explicitly requests migration from RxJava to Kotlin coroutines and/or flows.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Use this skill as the baseline for ALL Android and Kotlin Multiplatform (KMP) work — whenever the user mentions Android, Kotlin (in an Android context), KMP, CMP, commonMain, androidMain, iosMain, AndroidManifest, Gradle, build.gradle, Hilt, Dagger, Room, Retrofit, Ktor, ViewModel, LiveData, StateFlow, SharedFlow, Compose, Activity, Fragment, Intent, ADB, Logcat, MVVM, MVI, repository pattern, or any Android SDK / Jetpack / AndroidX API. Always load this skill alongside the more specific skills (android-skills:compose, android-skills:kotlin-flows, android-skills:kmp-ktor, android-skills:android-retrofit, etc.): it routes to them and adds the few baseline rules that are easy to get wrong. Casual mentions like "fix this bug in my Android app," "refactor this ViewModel," "my KMP project," or any work inside an Android project directory should trigger this skill.
Compose and Compose Multiplatform expert for UI development across Android, Desktop, iOS, and Web. Covers state management, composition, animations, navigation, performance, design-to-code workflows, and production crash patterns, backed by source analysis from androidx/androidx and JetBrains/compose-multiplatform-core. Use whenever the user mentions Compose, @Composable, remember, LaunchedEffect, Scaffold, NavHost, NavDisplay, MaterialTheme, LazyColumn, Modifier, recomposition, Compose Multiplatform/CMP, commonMain, expect/actual, ComposeUIViewController, UIKitView, ComposeViewport, Res.drawable/Res.string, or any Compose API. Also trigger on phrases like "design to compose", "build this UI", "implement this design", or any modern Kotlin UI question — including casual mentions like "my compose screen is slow". Plus focus topics: FocusRequester, focusProperties, onPreviewKeyEvent, D-pad, TV remote, ChromeOS, androidx.tv.material3.
Use when reading, adding, updating, or removing PDF annotations (highlight, free-text, stamp) or page objects (path/text/image) with Android's platform PDF APIs, or when saving those edits back to disk — the editing surface added in API 36.1 (PdfRenderer.Page) and SDK extension 18 (PdfRendererPreV.Page on API 31+), android.graphics.pdf.component. Covers the version gate, the open-edit-write workflow, and the id/render-flag/save contracts. Not for display-only PdfViewerFragment work, PdfDocument creation-from-scratch, form filling, or third-party PDF SDKs (iText, PDFBox, PSPDFKit).
Use when debugging Android or KMP issues — Android-specific techniques covering Logcat, ADB, ANR traces, R8 stack trace decoding, memory leaks, Gradle build failures, and Compose recomposition bugs, on a root-cause-first foundation.
Use when writing, fixing, or refactoring Android/KMP code in Kotlin — a test-first (RED-GREEN-REFACTOR) foundation plus the Android test traps: Compose-test dispatching (StandardTestDispatcher default, the two-schedulers trap), semantics-first selectors, choosing the smallest test shape, test-clock vs wall-clock, and animation/screenshot determinism.
Use when setting up or working with Ktor client in KMP or Android projects — HttpClient configuration, per-platform engine selection, kotlinx.serialization, bearer auth with refresh, MockEngine testing, and error mapping at the repository boundary.
| name | rxjava-migration |
| description | Use only when the user explicitly requests migration from RxJava to Kotlin coroutines and/or flows. |
Migrate RxJava to Kotlin coroutines/flows incrementally. Only invoke this skill when the user explicitly asks to migrate RxJava code. Simple cases map directly; complex cases need a strategy and user input before any code is written.
Classify before writing any migrated code. A chain is Complex if ANY of: nested flatMap/switchMap with an inner Single/Observable; a custom Scheduler; complex error recovery (retryWhen, backoff, retry counts); multi-source zip/combineLatest (3+ sources); Flowable with an explicit backpressure strategy; a Subject shared across classes; or an unclear API return type. Any single Complex criterion makes the whole chain Complex — a two-operator chain with retryWhen is Complex.
Always confirm what each called API returns before migrating — do not assume. A leaf you think is migrated may still return Single/Observable.
| RxJava | Coroutines/Flow |
|---|---|
Observable<T> / Flowable<T> | Flow<T> (Flowable: match the original backpressure with buffer() / conflate()) |
Single<T> / Maybe<T> / Completable | suspend fun: T / T? / Unit |
PublishSubject / ReplaySubject(n) | MutableSharedFlow(replay = 0 / n) |
BehaviorSubject<T> | ask — MutableStateFlow vs MutableSharedFlow(replay = 1) |
BehaviorSubject — ask first: StateFlow always has a current value (needs an initial, exposes .value, replays it to new collectors); SharedFlow(replay = 1) also replays the last emission but has no .value and no initial-value requirement. "Do you always have an initial value and need .value access, or is the stream sometimes empty at start?"
observeOn shiftSchedulers.io() → Dispatchers.IO, computation() → Default, mainThread() → Main, single() → newSingleThreadContext(...), newThread() → IO (per-task thread spawning isn't idiomatic in coroutines).
observeOn(AndroidSchedulers.mainThread()) does NOT become a dispatcher switch in the repository/use case — it means the caller collects on Main, which in the coroutines model is the ViewModel's job (viewModelScope runs on Main). Explain this shift to the developer.suspend fun, it manages its own dispatcher via withContext — subscribeOn has no equivalent and simply disappears. Do not wrap an already-main-safe suspend function in another withContext.subscribeOn + observeOn → flowOn applies upstream only; restructure accordingly.For the full operator mapping, see migration-map.md.
RxJava's retryWhen is stateful; Flow's retry predicate receives the Throwable, not an index.
// WRONG — 'attempt' is a Throwable, not a Long; this does not compile
flow.retry(3) { attempt -> delay(attempt * 1000L); true }
// CORRECT — retry(n)'s predicate gets the cause
flow.retry(3) { cause -> cause is IOException }
// CORRECT — stateful backoff: attempt IS the 0-based index here
flow.retryWhen { cause, attempt ->
if (cause is IOException && attempt < 3) { delay((attempt + 1) * 1000L); true } else false
}
Use retryWhen { cause, attempt -> } for any policy that depends on the attempt count.
Add kotlinx-coroutines-rx3 (or rx2); keep interop at layer boundaries only, never mixing RxJava and coroutines inside one function body.
observable.asFlow(); single.await(); maybe.awaitSingleOrNull(); completable.await() // Rx → coroutines
flow.asObservable(); flow.asSingle() // coroutines → Rx (asSingle throws if the flow emits 0 or 2+ elements)
Migrate leaf-up (data source → repository → use case → ViewModel), remove each bridge once its layer is fully migrated, and commit per layer.
retryWhen (count? linear vs exponential? which error types? what after exhaustion?); a custom Scheduler (which CoroutineDispatcher?); Flowable backpressure (buffer / conflate / DROP_OLDEST?); flatMap/switchMap over writes — switchMap → flatMapLatest is safe for reads (search/live data) but cancels in-flight work, so it's dangerous for writes; a Subject shared across classes (state → StateFlow, event → SharedFlow?). For onErrorResumeNext → catch, rethrow cancellation: catch { e -> if (e is CancellationException) throw e else emit(fallback) }.
(compositeDisposable.clear() in onCleared() → delete it entirely: viewModelScope is cancelled automatically when the ViewModel is cleared.)