The original link: https://medium.com/@laanayabdrzak/architecting-modern-mobile-applications-bf896120f0c2#.n1m5i520c

After working on a few projects, I took a look at how to properly design an Android application architecture based on my personal experience.

The first thing I want to share is an article by Uncle Bob

A good architecture has the following characteristics:

  • Independent of the UI

  • Independent of any framework

  • Independent of any third party services

  • Database independent

  • Easy to test

Why invest in architecture

Before I start this article, I want to talk about the importance of architecture and why it takes time and resources to build a good one. Some development teams go straight to work without designing the architecture, which seems to save a lot of trouble, but usually results in poor results, so it’s important to design the architecture before you develop it.

Use the MVP

There are a number of Android architectures that have become popular in the development community in recent years, such as MVP or MVVM, and they are increasingly being discussed.

Why MVP?

A big problem in the Android development middle layer is that activities and interfaces are so tightly coupled to data fetching classes that the View layer is almost too heavy to maintain or extend. Ideally, the MVP should separate the logic from the different views. MVP separates the View from the data and divides the entire application into at least three layers, which can be tested separately.

  • The Model layer contains the data to be displayed in the View (UI).

  • The View layer contains the interface responsible for presenting data and passing user events to Presenter.

  • Presenter is the middleman between Model and View and holds references to both.

Using ReactiveX: RxJava/RxAndroid

When an Android application has a lot of network operations, user interaction, and animation code, there can be a lot of Callback methods in the project, which is called Callback Hell. ReactiveX provides another solution for handling asynchronous tasks and events.

RxJava is a JVM implementation of Reactive Extensions. Developed by NetFlix, it’s on the rise.

RxJava is now the focus of the Android development community. It can be a bit difficult to get started, but once you get really used to RxJava, you’ll love it because it’s a cool way to avoid callback hell.

Use dependency injection framework: Dagger2

In the Clean architecture, the code is layered into an onion and follows a basic principle: the inner layer doesn’t know anything about the outer layer. That is to say dependence is from the outside in. The Dagger2 has the following features:

  • Because dependencies can be injected and configured externally, components can be reused.

  • The injection and configuration of dependencies are independent of the component, and the injected objects are initialized in a separate, uncoupled place, so that when we change the injected objects, we only need to modify the implementation method of the object without having to overhaul the code base.

  • Dependencies can be injected into a component: we can inject mock implementations of these dependencies, which makes testing easier.

Using Dagger2 brings at least the following advantages:

  • Simplify access to shared instances, just like ButterKnife.

  • Configuration is simple, even for complex dependencies.

  • Simplify unit tests.

  • Setting ranges for instances allows you to easily manage instances of different lifecycles-even the Application lifecycle.

Use Lambda expressions: Retrolambda

Retrolambda is a Java library for using Lambda expressions on Android and non-JDK8 platforms. It makes your code more compact and readable, especially when used with functional code such as RxJava.

Package JAVA code properly: by function, not layer

One of the questions you ask when you’re writing an application is what packages do you put the code in? For traditional applications, there are two general packaging methods:

Packaging by function

In this packaging, the package name corresponds to the task, or function. Each package usually contains only the classes responsible for the corresponding functions, such as the following structure:

Abderrazak.com.recycleviewcardview ├ ─ data │ ├ ─ local │ ├ ─ model │ └ ─ remote ├ ─ injection │ ├ ─ component │ └ ─ module ├ ─ UI │ ├─ Main │ ├─ detail │ etc.. ├─ customs ├─ adapters ├─ customsCopy the code

Advantages of this packaging method:

  • And you can see what APP does.
  • High degree of code modularity.
  • Easier to find documents.
  • Abstract chengdu is higher.
  • Separate different functions from different layers.
  • The structure is easy to read and maintain.
  • High adhesion between files.
  • Easy to quantify
  • It is not easy to modify classes or files incorrectly.
  • Easy to add or remove application features.
  • Modules are easy to reuse.

Pack by layer

In this way, the topmost packages correspond to different layers of the application, rather than functionality, such as the following structure:

Abderrazak.com.recycleviewcardview ├ ├ ─ model ├ ─ activities ├ ─ services ├ ─ fragments could ├ ─ util └ ─ etc..Copy the code

In this project structure, the implementation of each function is distributed in different directories. There is little relationship between the files in each directory, which leads to low adhesion and poor modularity of project packages and high coupling between different packages. As a result, when functionality is modified, various files are modified in different packages, and in particular it is almost impossible to remove a feature.

Project construction: Use Gradle

Gradle is the official build tool for Android development. Here are some of the benefits of using this automated build tool:

  • Build different style versions of your App.

  • Download and manage the dependency libraries.

  • Customize a keystore.

  • Adapt to Android project structure.

Test: Espresso/JUnit/Mockito/Robolectric

Testing should be part of the development process. Testing allows you to verify that the program is correct, the function works, the entire application works, and so on.

  • Presentation layer: Use Espresso2 and Instrumentation to test UI.

  • Domain layer: Because it is the pure Java layer, use JUnit and Mockito for testing.

  • Data layer: Tested with Robolectric3, JUnit, and Mockito. At one time, the testing code for this layer required a single module, and since the IDE didn’t have built-in unit tests, it wasn’t easy to build a Framework similar to Robolectric.

conclusion

Fortunately, application architecture is the focus of the moment, and there are so many articles and blogs discussing different Android application architectures, from the old MVC/MVP/MVVM to novel approaches like Mortar and Flow used by Square.

Hopefully this article will help you better structure future Android apps.

Welcome to pay attention to my public number, will be used in the fragmented time brush dry goods!