Top Related Projects
A simple library that makes it easy to pair thin views with dedicated controllers, isolated from most of the vagaries of the Activity life cycle.
A small, yet full-featured framework that allows building View-based Android applications
Lifecycle handling APIs for Android apps using RxJava
Nucleus is an Android library, which utilizes the Model-View-Presenter pattern to properly connect background tasks with visual parts of an application.
Mavericks: Android on Autopilot
Quick Overview
Mosby is an Android library that implements the Model-View-Presenter (MVP) and Model-View-Intent (MVI) architectural patterns. It aims to simplify the development of Android applications by providing a clean separation of concerns and improving testability.
Pros
- Promotes clean architecture and separation of concerns
- Enhances testability of Android applications
- Provides a solid foundation for building scalable and maintainable apps
- Offers support for both MVP and MVI patterns
Cons
- Steeper learning curve for developers new to MVP/MVI concepts
- May introduce additional boilerplate code
- Requires careful consideration when integrating with other libraries or frameworks
Code Examples
- Creating a simple MVP View interface:
public interface CounterView extends MvpView {
void showCount(int count);
void showError(String message);
}
- Implementing a Presenter:
public class CounterPresenter extends MvpBasePresenter<CounterView> {
private int count = 0;
public void incrementCounter() {
count++;
ifViewAttached(view -> view.showCount(count));
}
}
- Using the Presenter in an Activity:
public class CounterActivity extends MvpActivity<CounterView, CounterPresenter> implements CounterView {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_counter);
findViewById(R.id.incrementButton).setOnClickListener(v -> presenter.incrementCounter());
}
@Override
public CounterPresenter createPresenter() {
return new CounterPresenter();
}
@Override
public void showCount(int count) {
((TextView) findViewById(R.id.counterTextView)).setText(String.valueOf(count));
}
@Override
public void showError(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}
Getting Started
To use Mosby in your Android project, add the following dependencies to your build.gradle
file:
dependencies {
implementation 'com.hannesdorfmann.mosby3:mvp:3.1.1'
implementation 'com.hannesdorfmann.mosby3:viewstate:3.1.1'
}
Then, create your View interfaces, Presenters, and Activities/Fragments extending the appropriate Mosby base classes. Implement the required methods and start building your MVP-based Android application.
Competitor Comparisons
A simple library that makes it easy to pair thin views with dedicated controllers, isolated from most of the vagaries of the Activity life cycle.
Pros of Mortar
- Developed and maintained by Square, a well-known company in the mobile development space
- Provides a comprehensive set of tools for building Android apps, including view management and dependency injection
- Integrates well with other Square libraries like Dagger and Otto
Cons of Mortar
- Less active development and updates compared to Mosby
- Steeper learning curve due to its more complex architecture
- May be considered outdated as it doesn't fully embrace modern Android development practices
Code Comparison
Mosby (MVP implementation):
public class MyPresenter extends MvpBasePresenter<MyView> {
public void doSomething() {
// Presenter logic
getView().showResult();
}
}
Mortar (Presenter implementation):
@Singleton
public class MyPresenter extends Presenter<MyView> {
@Override
protected void onLoad(Bundle savedInstanceState) {
// Presenter logic
getView().showResult();
}
}
Both libraries aim to implement the MVP pattern, but Mosby offers a more straightforward approach with less boilerplate code. Mortar, on the other hand, provides a more opinionated structure that integrates with other Square libraries.
While Mortar offers a robust solution, Mosby's active development and simpler implementation make it a more attractive choice for many developers working on modern Android projects.
A small, yet full-featured framework that allows building View-based Android applications
Pros of Conductor
- Lightweight and focused on view-based navigation
- Easier to integrate into existing projects
- Better support for custom transitions and animations
Cons of Conductor
- Less comprehensive than Mosby for full MVP architecture
- Fewer built-in tools for handling state and data binding
- May require more manual setup for complex view hierarchies
Code Comparison
Conductor:
class MainController : Controller() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup): View {
return inflater.inflate(R.layout.controller_main, container, false)
}
}
Mosby:
class MainPresenter : MvpBasePresenter<MainView>() {
fun loadData() {
// Load data and update view
}
}
class MainFragment : MvpFragment<MainView, MainPresenter>(), MainView {
override fun createPresenter(): MainPresenter = MainPresenter()
}
Conductor focuses on view-based navigation and lifecycle management, while Mosby provides a more comprehensive MVP architecture. Conductor's approach is simpler for basic navigation scenarios, but Mosby offers more structure for complex applications with intricate data flows and state management.
Lifecycle handling APIs for Android apps using RxJava
Pros of RxLifecycle
- Lightweight and focused specifically on managing RxJava subscriptions
- Easy to integrate into existing projects without major architectural changes
- Provides a simple API for binding observables to Android lifecycle events
Cons of RxLifecycle
- Limited scope compared to Mosby's full MVP implementation
- Doesn't provide a complete architecture solution for Android apps
- May require additional libraries or patterns for a comprehensive app structure
Code Comparison
RxLifecycle:
myObservable
.compose(RxLifecycle.bindUntilEvent(lifecycle, ActivityEvent.DESTROY))
.subscribe();
Mosby:
public class MyPresenter extends MvpBasePresenter<MyView> {
@Override
public void attachView(MyView view) {
super.attachView(view);
// Presenter logic here
}
}
Summary
RxLifecycle focuses on managing RxJava subscriptions in Android, offering a lightweight solution for lifecycle-aware observables. Mosby, on the other hand, provides a complete MVP architecture implementation with additional features beyond subscription management. RxLifecycle is easier to integrate into existing projects, while Mosby offers a more comprehensive approach to app architecture. The choice between the two depends on the project's specific needs and the desired level of architectural guidance.
Nucleus is an Android library, which utilizes the Model-View-Presenter pattern to properly connect background tasks with visual parts of an application.
Pros of Nucleus
- Lightweight and minimalistic approach to Android architecture
- Focuses on dependency injection and lifecycle management
- Easier learning curve for developers new to MVP/MVVM patterns
Cons of Nucleus
- Less comprehensive feature set compared to Mosby
- Smaller community and fewer resources available
- Less frequent updates and maintenance
Code Comparison
Nucleus:
public class MyPresenter extends Presenter<MyView> {
@Override
protected void onTakeView(MyView view) {
super.onTakeView(view);
// Presenter logic here
}
}
Mosby:
public class MyPresenter extends MvpBasePresenter<MyView> {
@Override
public void attachView(MyView view) {
super.attachView(view);
// Presenter logic here
}
}
Both libraries aim to simplify Android development using MVP/MVVM patterns, but Mosby offers a more comprehensive solution with additional features and a larger ecosystem. Nucleus, on the other hand, provides a more lightweight approach that may be easier for beginners to grasp.
Mosby includes built-in support for view state management and handling orientation changes, which Nucleus lacks. However, Nucleus's simplicity can be advantageous for smaller projects or developers who prefer a minimalistic approach.
Ultimately, the choice between Nucleus and Mosby depends on the project requirements, team expertise, and desired level of abstraction in the architecture.
Mavericks: Android on Autopilot
Pros of Mavericks
- Built specifically for Kotlin, leveraging its features and syntax
- Integrates well with Jetpack Compose for modern UI development
- Provides a more opinionated architecture, potentially reducing boilerplate
Cons of Mavericks
- Less flexible than Mosby, which supports both Java and Kotlin
- Steeper learning curve due to its more complex architecture
- May be overkill for smaller projects or teams new to MVI
Code Comparison
Mosby (MVP pattern):
class MyPresenter : MvpBasePresenter<MyView>() {
fun loadData() {
// Load data and update view
}
}
Mavericks (MVI pattern):
class MyViewModel(state: MyState) : MavericksViewModel<MyState>(state) {
fun loadData() = setState { copy(isLoading = true) }
}
Both libraries aim to provide architectural patterns for Android development, but they differ in their approach. Mosby focuses on MVP and supports both Java and Kotlin, making it more versatile for existing projects. Mavericks, on the other hand, is tailored for Kotlin and follows the MVI pattern, which can lead to more predictable state management but may require more setup.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Mosby
A Model-View-Presenter and Model-View-Intent library for Android apps.
Dependency
dependencies {
compile 'com.hannesdorfmann.mosby3:mvi:3.1.1' // Model-View-Intent
// or
compile 'com.hannesdorfmann.mosby3:mvp:3.1.1' // Plain MVP
// or
compile 'com.hannesdorfmann.mosby3:viewstate:3.1.1' // MVP + ViewState support
}
Additional modules:
dependencies {
// MVP + ViewState + LCE Views
compile 'com.hannesdorfmann.mosby3:mvp-lce:3.1.1'
// Null Object Presenter for MVP
 compile 'com.hannesdorfmann.mosby3:mvp-nullobject-presenter:3.1.1'
 // Queuing Presenter for MVP
 compile 'com.hannesdorfmann.mosby3:mvp-queuing-presenter:3.1.1'
}
SNAPSHOT:
dependencies {
compile 'com.hannesdorfmann.mosby3:mvi:3.1.2-SNAPSHOT'
compile 'com.hannesdorfmann.mosby3:mvp:3.1.2-SNAPSHOT'
compile 'com.hannesdorfmann.mosby3:viewstate:3.1.2-SNAPSHOT'
compile 'com.hannesdorfmann.mosby3:mvp-lce:3.1.2-SNAPSHOT'
compile 'com.hannesdorfmann.mosby3:mvp-nullobject-presenter:3.1.2-SNAPSHOT'
 compile 'com.hannesdorfmann.mosby3:mvp-queuing-presenter:3.1.2-SNAPSHOT'
}
You also have to add the url to the snapshot repository:
allprojects {
repositories {
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
}
}
Documentation
See the project website.
For Model-View-Intent check out this blog post series.
Changelog
The changelog can be found in the release section
Migrating
In Mosby 3.0 we have changed the package name from com.hannesdorfmann.mosby
to com.hannesdorfmann.mosby3
(note the 3 at the end).
Migrating a Mosby 2.x based app to Mosby 3.0 should be straightforward:
Just replace all import statements of your app in android studio with Edit -> Find -> Replace in Path ...
and set find import com.hannesdorfmann.mosby
replace with import com.hannesdorfmann.mosby3
.
There were also some minor API changes (see changelog),
but most apps should be fine by replacing the import statements.
Conductor
Mosby has a plugin for Conductor. You can find it here: https://github.com/sockeqwe/mosby-conductor
License
Copyright 2015 Hannes Dorfmann
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Top Related Projects
A simple library that makes it easy to pair thin views with dedicated controllers, isolated from most of the vagaries of the Activity life cycle.
A small, yet full-featured framework that allows building View-based Android applications
Lifecycle handling APIs for Android apps using RxJava
Nucleus is an Android library, which utilizes the Model-View-Presenter pattern to properly connect background tasks with visual parts of an application.
Mavericks: Android on Autopilot
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot