2 min read

Yesterday, Airbnb open sourced MvRx (pronounced mavericks), their Android architecture. MvRx received an astounding adoption rate when it was first introduced to the engineers at Airbnb in July. Later, it became their standard way of writing screens and now it is used for nearly all of their product development. Due to its popularity among Airbnb’s app developers, they have decided to share it with all Android developers.

MvRx is based on Kotlin, which helped MvRx’s developers to leverage several powerful features provided by the language to build a cleaner API. It is built on top of the following technologies and concepts:

  • Kotlin
  • Android Architecture Components
  • RxJava
  • React (conceptually)
  • Epoxy (optional but recommended)

Why you should use MvRx?

Using MvRx you will be able to write simple and complex Android screens easily. MvRx also helps making certain tasks easier and faster for Android app developers such as:

  1. Saving view state and business logic in onSaveInstanceState properly
  2. Creating layouts with a toolbar, RecyclerView, and footer over and over again
  3. Executing the onSuccess and onFailure handlers for asynchronous requests
  4. Dealing with the Android lifecycle

What are its core concepts?

  • State: MvRxState is an interface that you need to extend with an immutable Kotlin data class. It contains the properties necessary to render your screen.
  • ViewModel: All the business logic and anything other than just rendering views is handled by MvRxViewModels. It extends Google’s ViewModel. The following lifecycle diagram clearly illustrates how ViewModels simplify lifecycles on Android:

ViewModel

Source: GitHub

  • View: MvRxView is a simple interface that users see and interact with. It is a LifecycleOwner consisting of a invalidate() function that gets called any time there is a change in the state of its ViewModels.
  • Async: MvRx makes handling async requests like fetching from a network or a database easier using Async. It is a Kotlin sealed class with four types: Uninitialized, Loading, Success, and Fail. MvRx provides an extension to map Observable to an Async property on your state to make executing network requests and other actions easy with a single line of code.

How can you install it?

To use MvRx, you need to add the following dependency to your project in the build.gradle file:

dependencies {

 implementation 'com.airbnb.android:mvrx:0.5.0'

}

To know about the other details of MvRx, you can refer to its GitHub repository and also read Airbnb’s official announcement.

Read Next

RxAndroid 2.1.0 is out with a newly added Async API!

Dagger 2.17, a dependency injection framework for Java and Android, is now out!

Did you know your idle Android device sends data to Google 10 times more often than an iOS device does to Apple?


Subscribe to the weekly Packt Hub newsletter. We'll send you the results of our AI Now Survey, featuring data and insights from across the tech landscape.

* indicates required

LEAVE A REPLY

Please enter your comment!
Please enter your name here