Skip to content

AndroidClasses/android-architecture

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

57 Commits
 
 
 
 
 
 

Repository files navigation

Android Architecture Blueprints [beta] - MVP + Dagger2

Summary

Project contributors: Saúl Molinero

Key concepts

Dagger2 is a fully static, compile-time dependency injection framework for both Java and Android. It is an adaptation of an earlier version created by Square and now maintained by Google.

Dependency injection frameworks take charge of object creation. For example, in todo-mvp we create the TasksPresenter in TasksActivity:

mTasksPresenter = new TasksPresenter(
        Injection.provideTasksRepository(getApplicationContext()), tasksFragment);

But in this sample, the presenter is injected. Dagger2 takes care of creation and figuring out the dependencies:

public class TasksActivity extends AppCompatActivity {
    @Inject TasksPresenter mTasksPresenter;
    ...
}

For this to work we added new classes to the feature, check out:

That's a lot of work to replace a constructor call! The main advantage of doing this is that we can substitute modules for testing. It can be done at compile time, using flavors, or at runtime, using some kind of debug panel for manual testing. They can also be configured from automated tests, to test different scenarios.

This sample is still using the mock/prod flavors from todo-mvp to generate different APKs. There is a TasksRepositoryModule in prod/ which fetches data from the slow data source (simulating a backend API) and another one in mock/, which provides fake data, ideal for automated testing. With this approach the app can be configured to use fake location coordinates, write and read from fake shared preferences, simulate network conditions, etc.

Dependencies

  • Dagger2

Features

Complexity - understandability

Use of architectural frameworks/libraries/tools:

Building an app with a dependency injection framework is not trivial as it uses new concepts and many operations are done under the hood. However, once in place, it's not hard to understand and use.

Testability

Very high. Use of Dagger2 improves flexibility in local integration tests and UI tests. Components can be replaced by doubles very easily and test different scenarios.

Code metrics

-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Java                            60           1175           1610           3699 (3451 in MVP)
XML                             34             97            337            602
-------------------------------------------------------------------------------
SUM:                            94           1272           1947           4301
-------------------------------------------------------------------------------

Maintainability

Ease of amending or adding a feature + Learning cost

Medium. Developers need to be aware of how Dagger2 works, although the setup of new features should look very similar to existing ones.

About

A collection of samples to discuss and showcase different architectural tools and patterns for Android apps.

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published