Guide and execute the migration of asynchronous code from RxJava to Kotlin Coroutines and Flow. Use this skill when a user asks to convert RxJava (Observables, Singles, Completables, Subjects) to Coroutines (suspend functions, Flows, StateFlows).
Start a fresh conversation. Replace the highlighted placeholders.
Use rxjava-to-coroutines-migration to migrate the RxJava code in [target Kotlin files or module] to idiomatic Kotlin Coroutines and Flow, preserving behavior, error handling, threading, and lifecycle ownership; use suspend functions for one-shot work and Flow or StateFlow only for genuine streams and state.
Required inputs
[target Kotlin files or module]
User-approved starter prompt based on a pinned source version; check current skill requirements before running. Task evidence ↗
SKILL.md
name: rxjava-to-coroutines-migrationdescription: Guide and execute the migration of asynchronous code from RxJava to Kotlin Coroutines and Flow. Use this skill when a user asks to convert RxJava (Observables, Singles, Completables, Subjects) to Coroutines (suspend functions, Flows, StateFlows).
RxJava to Kotlin Coroutines Migration Skill
A specialized skill designed to safely and idiomatically refactor Android or Kotlin codebases from RxJava to Kotlin Coroutines and Flow.
Migration Mapping Guide
When migrating RxJava components to Kotlin Coroutines, use the following standard mappings:
1. Base Types
Single<T> -> suspend fun ...(): T
A single asynchronous value.
Maybe<T> -> suspend fun ...(): T?
A single asynchronous value that might not exist.
Completable -> suspend fun ...()
An asynchronous operation that completes without a value.
Observable<T> -> Flow<T>
A cold stream of values.
Flowable<T> -> Flow<T>
Coroutines Flow natively handles backpressure.
2. Subjects to Hot Flows
PublishSubject<T> -> MutableSharedFlow<T>
Broadcasts events to multiple subscribers. Use MutableSharedFlow(extraBufferCapacity = ...) if buffering is needed.
BehaviorSubject<T> -> MutableStateFlow<T>
Holds state and emits the current/latest value to new subscribers. Requires an initial value.
delay -> delay (suspend function) or onEach { delay(...) }
5. Execution and Lifecycle
subscribe() -> collect {} (for Flows) or direct invocation (for suspend functions) inside a CoroutineScope.
Disposable.dispose() -> Job.cancel()
CompositeDisposable.clear() -> Cancel the parent CoroutineScope or Job.
Execution Steps
Analyze the RxJava Chain: Identify the source type (Single, Observable, etc.), operators used, and where the subscription happens.
Convert the Source: Change the return type in the repository or data source layer first. Convert to suspend functions for one-shot operations, and Flow for streams.
Rewrite Operators: Translate the RxJava operators to their Flow or Coroutine equivalents. Note that many RxJava operators can simply be replaced by standard Kotlin collection/sequence operations inside a map or onEach block.
Update the Subscription: Replace .subscribe(...) with launch { ... } and .collect { ... } in the ViewModel or Presenter. Ensure the launch is tied to the correct lifecycle scope (e.g., viewModelScope).
Handle Errors: Replace onError blocks with try/catch around suspend functions, or .catch { } operators on Flows.
Handle Threading: Remove .subscribeOn() and .observeOn(). Use withContext where necessary, or .flowOn() to change the context of the upstream flow.
suspend fun getUser(id: String): User { ... } // Internally uses withContext(Dispatchers.IO) if needed
viewModelScope.launch {
try {
val user = getUser("123")
view.showUser(user)
} catch (e: Exception) {
view.showError(e)
}
}
Best Practices
Favor Suspend Functions: Default to suspend functions instead of Flow unless you actually have a stream of multiple values over time. Single and Completable almost always become suspend functions.
State Handling: Use StateFlow in ViewModels to expose state to the UI instead of BehaviorSubject or LiveData.
Lifecycle Awareness: Use repeatOnLifecycle or flowWithLifecycle in the UI layer when collecting Flows to avoid background work when the view is not visible.