# RxAndroid — Reactive Extensions for Android > Android-specific bindings for RxJava that provide schedulers for the Android main thread and lifecycle-aware reactive programming primitives. ## Install Save as a script file and run: # RxAndroid — Reactive Extensions for Android ## Quick Use ```gradle implementation 'io.reactivex.rxjava3:rxandroid:3.0.2' implementation 'io.reactivex.rxjava3:rxjava:3.1.8' ``` ```java Observable.fromCallable(() -> fetchDataFromNetwork()) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( data -> textView.setText(data), error -> Log.e("Rx", error.getMessage()) ); ``` ## Introduction RxAndroid provides the essential Android-specific components for using RxJava on the Android platform. Its primary contribution is AndroidSchedulers.mainThread(), a scheduler that posts work to the Android UI thread, enabling developers to compose asynchronous operations that safely update the UI. Combined with RxJava's operator library, it enables declarative management of threading, error handling, and data transformation in Android apps. ## What RxAndroid Does - Provides AndroidSchedulers.mainThread() for observing results on the Android UI thread - Bridges RxJava's reactive streams with Android's main-thread Looper mechanism - Enables composable threading with subscribeOn for background work and observeOn for UI updates - Integrates with RxJava's full operator library for filtering, mapping, combining, and debouncing streams - Supports lifecycle-aware subscription management when paired with libraries like AutoDispose or RxLifecycle ## Architecture Overview RxAndroid is a thin layer on top of RxJava. Its core class, AndroidSchedulers, creates a scheduler backed by a Handler tied to the main Looper. When observeOn(AndroidSchedulers.mainThread()) is called, RxJava posts each emitted item to the main thread's message queue via that Handler. The library intentionally stays minimal, delegating all stream operators and threading primitives to RxJava itself. ## Setup & Configuration - Add both rxjava and rxandroid dependencies to your module-level build.gradle - Use subscribeOn(Schedulers.io()) for network or disk operations and observeOn(AndroidSchedulers.mainThread()) for UI updates - Manage subscription lifecycle by disposing Disposable objects in onDestroy or onStop - Consider AutoDispose or RxLifecycle for automatic disposal tied to Android lifecycle events - Use CompositeDisposable to manage multiple subscriptions as a group ## Key Features - Main-thread scheduler that safely bridges background streams to Android UI updates - Composable threading model that makes complex async flows readable and maintainable - Full access to RxJava's operator library for debounce, throttle, merge, zip, and retry patterns - Backpressure support via Flowable for handling high-frequency data sources - Error handling operators that prevent unhandled exceptions from crashing the app ## Comparison with Similar Tools - **Kotlin Coroutines + Flow** — modern Kotlin-first async approach with structured concurrency; RxAndroid suits Java-heavy codebases and complex stream composition - **LiveData** — lifecycle-aware observable for ViewModel-to-UI communication; RxAndroid offers richer operators for stream transformation - **AsyncTask** — deprecated Android API for background work; RxAndroid provides composable, leak-resistant alternatives - **Handler + Thread** — low-level Android threading; RxAndroid abstracts threading into declarative scheduler declarations - **ListenableFuture (Guava)** — callback-based future; RxAndroid provides a stream-oriented model with built-in error propagation ## FAQ **Q: Should new Android projects use RxAndroid or Kotlin Coroutines?** A: Kotlin Coroutines and Flow are the recommended approach for new Kotlin projects. RxAndroid remains valuable for existing Java codebases and when complex stream operators like combineLatest or debounce are heavily used. **Q: How do I prevent memory leaks from subscriptions?** A: Dispose subscriptions in lifecycle callbacks using CompositeDisposable, or use AutoDispose to bind disposal to the component lifecycle automatically. **Q: Is RxAndroid compatible with RxJava 3?** A: Yes. RxAndroid 3.x is designed for RxJava 3. Ensure both dependencies use the same major version. **Q: Can I use RxAndroid with Jetpack Compose?** A: You can convert RxJava streams to Compose State using collectAsState extensions, but Kotlin Flow integrates more naturally with Compose. ## Sources - https://github.com/ReactiveX/RxAndroid - https://github.com/ReactiveX/RxJava --- Source: https://tokrepo.com/en/workflows/asset-c463d36d Author: Script Depot