background

In recent years, componentization has always been active exploration and practice in the direction of the industry, more and more companies use componentization to build the project, our company also have some practice in modular practice direction, but it is not a standard, is that why we are finishing the purpose of this document, make sure componentized solution, complex business for the future.

The advantages of componentization

First of all, some of the advantages of componentization are core values that we use, so what are the advantages? The summary is as follows:

1. Speed up project compilation and improve development efficiency because modules can be independently compiled, tested, packaged and deployed

2. Improve component reuse

3. Avoid cross dependence between modules, achieve low coupling and high cohesion

4. Unified management of referenced third-party library codes to avoid inconsistent versions and reduce the introduction of redundant libraries

Componentize goals

First set up a goal, to ensure that we are not deviating from the direction of follow-up, not for the sake of componentization modular, the design of the complex is bound to cause a lot of learning cost, it will reduce the development efficiency, actually our modular design goal is in order to be more simple, and goal motivates our values is unified, all in one direction efforts, together to create value. As follows:

1. Generate componentized template projects, which can quickly iterate new projects; 2. Componentized scaffolding, which can be customized

Componentized specification

1. Decouple your code as much as possible

  • Modules are divided into two types. One is functional component module, which encapsulates some common methods and services and provides them as a dependent library. The other is business component modules, which deal with functions such as business logic and are ultimately responsible for assembling the APP.

2. Each component can be run separately

  • Each component is highly cohesive and integrated as a whole
  • Switch between different environments through Gradle script configuration

3. Communication between components

  • Communicate through routing (Routing framework to be selected)

4. Regulate the component lifecycle

  • The life cycle refers to how long a component exists in an application, whether it can be used on demand and dynamically, and therefore involves loading and unloading components, etc

5. Strictly limit the growth of common base components

  • As development progresses, be careful not to add too much to the underlying common components. It should be reduced in volume! If the underlying component is too large, running the component is also slow

Componentize common problems and solutions

1. Communication Communication needs to consider two aspects, one is interprocess, one is cross-process, then there are two solutions as follows: interprocess: interface dependency call cross-process: github.com/iqiyi/Andro… Andromeda provides interface management for communication between components, including in-process local interface calls and cross-process interface calls.

2. The initialization

  • Developer.android.com/topic/libra… Official Google
  • Github.com/bingoogolap… AppInit is used to solve actual problems in the service evolution process of The B-terminal App of Meituan Cashier

3. Code isolation, how to deal with dependencies The componentization solution of Meituan is recommended here: split each business component into an Export Module and an Implement Module. In this case, if Module A needs to call the interface provided by Module B, and Module B needs to call the interface of Module A, Module A needs to rely on Module B Export, and Module B needs to rely on Module A Export.

4. The page is displayed

  • ARouter
  • Navigation

Navigation is mentioned separately here. In fact, there are many advantages when using Navigation, as follows:

  • You can implement a single Activity+ multiple fragments architecture. The advantage of fragments is that they are lighter than activities and thus consume less memory. In fact, there are process communication and other overhead between activities.
  • Combining with the framework of Flutter, we found that a page of Flutter consists of multiple widgets, making the page more flexible. The same Fragment also needs to be placed in the Activity and be flexible and configurable.
  • Official support, rest assured use

Navigation componentization project: github.com/VMadalin/an…

Componentized architecture diagram design

The above design drawings follow the componentization specification. For details, please read below.

Module is introduced

1. The layer of business components is mainly concerned with independent business components. For example, the login module can be separated from a complete component and packaged into an independent APP package.

• Common: contains Common business interface definitions, data models, and database related components. • Log: Log framework components, such as Logan • Push: Push message components, independent of business • Share: share SDK • Video: Video-related function components • Instant Messaging: chat real-time message related components

3.Core Base Layer:

  1. Network request (using Retrofit+ coroutine)
  2. Image loading (Strategy mode, Glide and Picasso or Coil can be toggle)
  3. Base class Adapter encapsulation (support item animation, multiple layout items, drop-down and load more, item click events)
  4. General purpose utility classes, Kotlin version of dynamic extensions (Context, Fragment, Activity, Service, Intent, etc.)
  5. Coroutines encapsulation
  6. Other, etc.

BuildSrc uses better Gradle dependency management and Composite Builds improve build speed and build custom plugins. In addition, framework version information is centrally managed to prevent framework dependent version conflicts among components

Introduction to componentized scaffolding

First say why want to design the scaffold, this is the result of SpringBoot scaffolding inspiration, because after componentization, program modules are relatively independent, if we develop the new project will encounter all sorts of functions, and conforms to the actual scene we demand, such as: no database consultant, and other app will need a database to store. Most of the cat processing is offline, while ESA is background processing. Each business has different characteristics, so we want to create a customized component project rather than remain unchanged. The development of scaffolding is also a goal of our design, designed as follows: This scaffolding we do is online, fool operation, just need to select the template has been configured in the page, can be customized as follows:

  1. The package name
  2. language
  3. Minimum Compatible version
  4. Architecture to choose
  5. The component dependence

Of course, the design of this page is not perfect enough to reflect that we can rely on the tripartite framework. We will add this into the future design to achieve customization based on the actual business scenes of our company.

Discuss the conclusion

The frame selection to be confirmed is as follows:

  1. Do not use single Activity+ multiple Fragment architecture (Considering that C end is already using multiple activities, and single Activity does not have better practice results, so keep the original architecture)
  2. Consider upgrading the database to Moshi, or Kotlin official Serialization, removing Gson logic, and later dedicated to thematic discussions
  3. The network layer abandons Rxjava mode and uses OKHTTP + coroutine mode in the future, which will be specially discussed in the following topics
  4. Page jumps still apply to Arouter
  5. Component initialization currently has a self-developed solution and Google official or Meituan related solution, to be followed by a special topic discussion
  6. Gradle Build mode updated to Kotlin DSL Gradle

Refer to the project

Github.com/hegaojian/J… Github.com/goldze/MVVM… Github.com/getActivity… Github.com/android/sun… Github.com/KunMinX/Jet… Github.com/skydoves/Po… Github.com/VMadalin/an… Github.com/qingmei2/MV…