Provides information on Android Architecture.
Libraries and tools included:
- Support libraries
- RecyclerViews
- RxJava and RxAndroid
- Retrofit 2
- Dagger 2
- DBFlow
- Butterknife
- Timber
- EventBus
- JobManager
- PlayService
This architecture is basically forked from android-boilerplate and improved by studying with other android architectures such as Androidstarter, Android-CleanArchitecture, Architecting Android…The evolution.
Before starting a new project please read Guidelines: Android
Please see Bibliography end of this page to get more detail about this architecture.
To quickly start a new project from this boilerplate follow the next steps:
- Download this
- Change the package name.
- Rename packages in main, androidTest and test using Android Studio.
- In
app/build.gradle
file,packageName
andtestInstrumentationRunner
. - In
src/main/AndroidManifest.xml
andsrc/debug/AndroidManifest.xml
.
- Create a new git repository, GitHub tutorial.
- Replace the example code with your app code following the same architecture.
- In
app/build.gradle
add the signing config to enable release versions. - Add Fabric API key and secret to fabric.properties and uncomment Fabric plugin set up in
app/build.gradle
- Create
keystore
folder underapp
. Keep keystore in here. If you don't know how to create keystore please read Guidelines: Android - Update
proguard-rules.pro
to keep models (see TODO in file) and add extra rules to file if needed. - Update README with information relevant to the new project.
- Update LICENSE to match the requirements of the new project.
Imagine you have to implement a sign in screen.
- Create a new package under
ui
calledsignin
- Create an new Activity called
ActivitySignIn
. You could also use a Fragment. - Define the view interface that your Activity is going to implement. Create a new interface called
SignInMvpView
that extendsMvpView
. Add the methods that you think will be necessary, e.g.showSignInSuccessful()
- Create a
SignInPresenter
class that extendsBasePresenter<SignInMvpView>
- Implement the methods in
SignInPresenter
that your Activity requires to perform the necessary actions, e.g.signIn(String email)
. Once the sign in action finishes you should callgetMvpView().showSignInSuccessful()
. - Create a
SignInPresenterTest
and write unit tests forsignIn(email)
. Remember to mock theSignInMvpView
and also theDataManager
. - Make your
ActivitySignIn
implementSignInMvpView
and implement the required methods likeshowSignInSuccessful()
- In your activity, inject a new instance of
SignInPresenter
and callpresenter.attachView(this)
fromonCreate
andpresenter.detachView()
fromonDestroy()
. Also, set up a click listener in your button that callspresenter.signIn(email)
.
Please see the SampleActivity
implementation.
The architecture of our Android apps is based on the MVP (Model View Presenter) pattern.
-
View (UI layer): this is where Activities, Fragments and other standard Android components live. It's responsible for displaying the data received from the presenters to the user. It also handles user interactions and inputs (click listeners, etc) and triggers the right action in the Presenter if needed.
-
Presenter: presenters subscribe to RxJava Observables provided by the
DataManager
. They are in charge of handling the subscription lifecycle, analysing/modifying the data returned by theDataManager
and calling the appropriate methods in the View in order to display the data. -
Model (Data Layer): this is responsible for retrieving, saving, caching and massaging data. It can communicate with local databases and other data stores as well as with restful APIs or third party SDKs. It is divided in two parts: a group of helpers and a
DataManager
. The number of helpers vary between project and each of them has a very specific function, e.g. talking to an API or saving data inSharedPreferences
. TheDataManager
combines and transforms the outputs from different helpers using Rx operators so it can: 1) provide meaningful data to the Presenter, 2) group actions that will always happen together. This layer also contains the actual model classes that define how the data structure is.
Looking at the diagram from right to left:
-
Helpers (Model): A set of classes, each of them with a very specific responsibility. Their function can range from talking to APIs or a database to implementing some specific business logic. Every project will have different helpers but the most common ones are:
- Network Services : Retrofit interfaces that talk to Restful APIs, each different API will have its own Retrofit service. They return Rx Observables.
- DatabaseHelper: It handles inserting, updating and retrieving data from a local SQLite database. Its methods return Rx Observables that emit plain java objects (models)
- PreferencesHelper: It saves and gets data from
SharedPreferences
, it can return Observables or plain java objects directly. - ConfigurationHelper : It saves and gets data from
SharedPreferences
too. It can return some instant config data related project. such as base api etc - JobManager : Priority Job Queue is an implementation of a Job Queue specifically written for Android to easily schedule jobs (tasks) that run in the background, improving UX and application stability.
- Interceptors(Optional) : Interceptors are a powerful mechanism that can monitor, rewrite, and retry calls. For example if you need to add header for each call you may use.
-
Data Manager (Model): It's a key part of the architecture. It keeps a reference to every helper class and uses them to satisfy the requests coming from the presenters. Its methods make extensive use of Rx operators to combine, transform or filter the output coming from the helpers in order to generate the desired output ready for the Presenters. It returns observables that emit data models.
-
Presenters: Subscribe to observables provided by the
DataManager
and process the data in order to call the right method in the View. -
Activities, Fragments, ViewGroups (View): Standard Android components that implement a set of methods that the Presenters can call. They also handle user interactions such as clicks and act accordingly by calling the appropriate method in the Presenter. These components also implement framework-related tasks such us managing the Android lifecycle, inflating views, etc.
-
Event Bus: It allows the View components to be notified of certain types of events that happen in the Model. Generally the
DataManager
posts events which can then be subscribed to by Activities and Fragments. The event bus is only used for very specific actions that are not related to only one screen and have a broadcasting nature, e.g. the user has signed out.
The project can be distributed using the Google Play Store.
We use the Gradle Play Publisher plugin. Once set up correctly, you will be able to push new builds to the Alpha, Beta or production channels like this
./gradlew publishApkRelease
Read plugin documentation for more info.
You can also use Fabric's Crashlytics for distributing beta releases. Remember to add your fabric
account details to app/src/fabric.properties
.
To upload a release build to Crashlytics run:
./gradlew assembleRelease crashlyticsUploadDistributionRelease