nucleus
Nucleus is an Android library, which utilizes the Model-View-Presenter pattern to properly connect background tasks with visual parts of an application.
Top Related Projects
RxJava – Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.
A fast dependency injector for Android and Java.
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.
Lifecycle handling APIs for Android apps using RxJava
RxJava binding APIs for Android's UI widgets.
Quick Overview
Nucleus is a lightweight dependency injection framework for Android and Java applications. It aims to simplify the process of managing dependencies in your projects while providing a clean and efficient API. Nucleus focuses on ease of use and performance, making it a suitable choice for both small and large-scale applications.
Pros
- Simple and intuitive API, reducing the learning curve for developers
- Lightweight and fast, with minimal impact on application performance
- Supports both constructor and field injection
- Compatible with both Android and Java projects
Cons
- Less feature-rich compared to some more established dependency injection frameworks
- Limited documentation and community support
- May not be suitable for very complex dependency graphs
- Lacks some advanced features found in larger DI frameworks
Code Examples
- Basic dependency injection:
public class Car {
@Inject
Engine engine;
@Inject
public Car() {}
}
public class Engine {
@Inject
public Engine() {}
}
Nucleus nucleus = new Nucleus();
Car car = nucleus.get(Car.class);
- Providing custom implementations:
public interface Engine {
void start();
}
public class V8Engine implements Engine {
@Inject
public V8Engine() {}
@Override
public void start() {
System.out.println("V8 engine started");
}
}
Nucleus nucleus = new Nucleus();
nucleus.bind(Engine.class, V8Engine.class);
Engine engine = nucleus.get(Engine.class);
engine.start(); // Outputs: V8 engine started
- Singleton scoping:
@Singleton
public class DatabaseHelper {
@Inject
public DatabaseHelper() {}
}
Nucleus nucleus = new Nucleus();
DatabaseHelper db1 = nucleus.get(DatabaseHelper.class);
DatabaseHelper db2 = nucleus.get(DatabaseHelper.class);
System.out.println(db1 == db2); // Outputs: true
Getting Started
To use Nucleus in your project, follow these steps:
- Add the Nucleus dependency to your project:
implementation 'com.github.konmik:nucleus:1.0.0'
- Create a Nucleus instance in your application:
public class MyApplication extends Application {
private Nucleus nucleus;
@Override
public void onCreate() {
super.onCreate();
nucleus = new Nucleus();
// Configure your bindings here
}
public Nucleus getNucleus() {
return nucleus;
}
}
-
Use the
@Inject
annotation to mark constructor or field injection points in your classes. -
Retrieve instances using the Nucleus instance:
MyClass instance = ((MyApplication) getApplication()).getNucleus().get(MyClass.class);
Competitor Comparisons
RxJava – Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.
Pros of RxJava
- More comprehensive and feature-rich, offering a wide range of operators for complex reactive programming
- Larger community and ecosystem, with extensive documentation and third-party libraries
- Better suited for complex asynchronous operations and event-driven programming
Cons of RxJava
- Steeper learning curve due to its extensive API and concepts
- Can be overkill for simpler applications or those with minimal reactive requirements
- Potential for performance overhead in certain scenarios due to its abstraction layers
Code Comparison
Nucleus:
@Override
protected void onCreate(Bundle savedState) {
super.onCreate(savedState);
getPresenter().takeView(this);
}
RxJava:
Observable.just("Hello, World!")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(System.out::println);
Summary
Nucleus is a lightweight MVP library for Android, focusing on simplifying the implementation of the MVP pattern. RxJava, on the other hand, is a comprehensive reactive programming library that extends beyond MVP and offers powerful tools for handling asynchronous data streams and events.
While Nucleus provides a straightforward approach to MVP, RxJava offers more flexibility and power for complex reactive scenarios. The choice between the two depends on the project's requirements, with Nucleus being more suitable for simpler MVP implementations and RxJava for more advanced reactive programming needs.
A fast dependency injector for Android and Java.
Pros of Dagger
- Widely adopted and battle-tested in large-scale applications
- Extensive documentation and community support
- Supports both Java and Android development
Cons of Dagger
- Steeper learning curve, especially for beginners
- More complex setup and configuration
- Can lead to verbose code in some cases
Code Comparison
Nucleus:
class MainPresenter : Presenter<MainView>() {
override fun onTakeView(view: MainView) {
super.onTakeView(view)
view.showMessage("Hello, Nucleus!")
}
}
Dagger:
@Module
public class AppModule {
@Provides
@Singleton
SharedPreferences provideSharedPreferences(Application application) {
return PreferenceManager.getDefaultSharedPreferences(application);
}
}
Key Differences
- Nucleus focuses on MVP architecture, while Dagger is a dependency injection framework
- Nucleus is Kotlin-centric, whereas Dagger supports both Java and Kotlin
- Dagger requires more boilerplate code but offers greater flexibility in managing dependencies
- Nucleus provides a simpler API for handling lifecycle events in presenters
Use Cases
- Choose Nucleus for smaller projects or when focusing on MVP architecture in Kotlin
- Opt for Dagger in larger projects, especially those requiring complex dependency management or when working with Java
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
- More actively maintained with recent updates and releases
- Larger community and backing from Square, potentially leading to better support
- Offers a wider range of features for dependency injection and app architecture
Cons of Mortar
- Steeper learning curve due to more complex architecture
- Heavier implementation that may increase app size and complexity
- Less flexible for smaller projects or simpler use cases
Code Comparison
Nucleus:
class MainPresenter @Inject constructor(private val router: Router) : Presenter<MainView>() {
override fun onTakeView(view: MainView) {
view.showMessage("Hello, Nucleus!")
}
}
Mortar:
@WithScope(MainScope::class)
class MainPresenter @Inject constructor(private val router: Router) : Presenter<MainView>() {
@Override fun onLoad(savedInstanceState: Bundle?) {
view?.showMessage("Hello, Mortar!")
}
}
Summary
Mortar offers a more comprehensive solution for app architecture with stronger community support, while Nucleus provides a simpler, more lightweight approach. The choice between them depends on project requirements, team expertise, and desired level of architectural complexity.
Lifecycle handling APIs for Android apps using RxJava
Pros of RxLifecycle
- Lightweight and focused solely on managing RxJava subscriptions
- Easy integration with existing Android components
- Flexible API allowing custom lifecycle handling
Cons of RxLifecycle
- Limited to RxJava subscription management
- Requires manual integration with each component
- Less opinionated about overall application architecture
Code Comparison
RxLifecycle:
public class MyActivity extends RxActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Observable.interval(1, TimeUnit.SECONDS)
.compose(this.<Long>bindToLifecycle())
.subscribe(/* ... */);
}
}
Nucleus:
@RequiresPresenter(MainPresenter.class)
public class MainActivity extends NucleusAppCompatActivity<MainPresenter> {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getPresenter().loadData();
}
}
Key Differences
- Nucleus provides a full MVP architecture, while RxLifecycle focuses on subscription management
- Nucleus includes presenter lifecycle management, whereas RxLifecycle leaves this to the developer
- RxLifecycle offers more granular control over subscription timing, while Nucleus abstracts this away
- Nucleus encourages a specific architectural pattern, while RxLifecycle is more flexible in its usage
RxJava binding APIs for Android's UI widgets.
Pros of RxBinding
- Focuses specifically on binding Android UI widgets to RxJava Observables
- Provides a comprehensive set of bindings for most Android UI components
- Maintained by a well-known Android developer with frequent updates
Cons of RxBinding
- Limited to UI bindings, doesn't provide a full application architecture
- Requires understanding of RxJava, which can have a steep learning curve
- May introduce unnecessary complexity for simple UI interactions
Code Comparison
Nucleus:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewModelProviders.of(this).get(MainViewModel.class);
}
RxBinding:
RxView.clicks(button)
.subscribe(event -> {
// Handle button click
});
Summary
Nucleus provides a more comprehensive MVP architecture for Android apps, while RxBinding focuses solely on UI bindings using RxJava. Nucleus offers a structured approach to app development, whereas RxBinding provides fine-grained control over UI interactions. The choice between the two depends on project requirements and team preferences. Nucleus may be better for larger projects requiring a full architecture, while RxBinding could be ideal for adding reactive UI components to existing projects.
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
Nucleus
Deprecation notice
Nucleus is not under develpment anymore. It turns out that Redux architecture scales way better than MVP/MVI/MVVM/MVxxx and I do not see further development of Nucleus valuable.
I recommend considering ReKotlin as a simple Redux implementation.
Info
Nucleus is a simple Android library, which utilizes the Model-View-Presenter pattern to properly connect background tasks with visual parts of an application.
Introduction
Some time has passed since Model-View-Presenter and RxJava completely superseded Loader and AsyncTask in our applications.
But there are still some defects in our applications even when using such advanced technologies:
- An application is unable to continue a background task execution after a configuration change.
- An application does not automatically restart a background task after a process restart.
While most applications work without such capabilities, their absence is an obvious bug that just sits there and waits for a user who pressed "Login" button while being in a subway and switched to another application because his network connection was too slow. Bugs that almost any application produce in such cases are numerous.
Android docs are covering such problems very briefly, take a look at: Processes and Threads - 4. Background process "If an activity implements its lifecycle methods correctly, and saves its current state, killing its process will not have a visible effect on the user experience, because when the user navigates back to the activity, the activity restores all of its visible state."
This is not true - there WILL be a visible effect because we're not restoring background tasks. The application will restore it's visual state, but it will forget what it is doing. So, if an application restores a progress bar, but does not restore the background task itself - a user will see the usual "progress bar forever" bug.
Nucleus' main features
-
In case of configuration change Nucleus automatically re-attaches all running background tasks to the new View. The application will not forget what it is doing.
-
In case of process restart Nucleus automatically restarts background tasks. Even when running on a low memory device or waiting for a long running background task completion, the application is still reliable.
-
The entire library has been built keeping The Kiss Principle in mind. Anyone can read and understand it easily.
-
The library does not depend on Dagger, you don't need to write a whole class just to inject a presenter. One annotation is all you need. Despite presenters are instantiated without Dagger, their dependencies can still be injected.
-
Presenter in Nucleus is an external class that does not depend on View, this automatically prevents any troubles that are connected with activity context leaks.
History
At the moment of the first release, the library main idea was to be the simplest possible MVP implementation.
A couple of months later, I finally realized that RxJava has became the tool number one for smart background threads handling, so RxPresenter appeared. Since that moment, the main focus shifted in the direction of RxJava support.
The correct lifecycle handling was something that seemed obvious to me from the beginning, so I did not make an accent on this feature in the library description. However, since those times, more MVP libraries appeared, now I need to differentiate Nucleus from other implementations. The library description became: "Nucleus is a simple Android library, which utilizes the Model-View-Presenter pattern to properly connect background tasks with visual parts of an application."
Include this library:
dependencies {
compile 'info.android15.nucleus:nucleus:6.0.0'
}
For additional view classes NucleusSupportFragment
, NucleusFragmentActivity
include:
dependencies {
compile 'info.android15.nucleus:nucleus-support-v4:6.0.0'
}
For additional view class NucleusAppCompatActivity
include:
dependencies {
compile 'info.android15.nucleus:nucleus-support-v7:6.0.0'
}
ProGuard config:
-keepclassmembers class * extends nucleus.presenter.Presenter {
<init>();
}
For RxJava 2:
dependencies {
compile 'info.android15.nucleus5:nucleus:7.0.0'
}
For additional view classes NucleusSupportFragment
, NucleusFragmentActivity
include:
dependencies {
compile 'info.android15.nucleus5:nucleus-support-v4:7.0.0'
}
For additional view class NucleusAppCompatActivity
include:
dependencies {
compile 'info.android15.nucleus5:nucleus-support-v7:7.0.0'
}
Hint: you can just copy/paste those classes code into ANY View class to keep your View classes hierarchy as you like to.
ProGuard config:
-keepclassmembers class * extends nucleus5.presenter.Presenter {
<init>();
}
Top Related Projects
RxJava – Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.
A fast dependency injector for Android and Java.
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.
Lifecycle handling APIs for Android apps using RxJava
RxJava binding APIs for Android's UI widgets.
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