The background,

What happens when our projects get bigger, our code gets fatter, there’s more coupling, compilations are slower, and development is less efficient? At this point, we need to refactor the old project, that is, to break up the module, officially known as componentization.

Statement | author: weixin_34138139 links: blog.csdn.net/weixin_3413…

Second, the introduction of

So what is componentization? Its basic idea is: common functions, controls, basic classes, third-party libraries, permissions and other common parts removed from the package, we call it basic components (baselibs); Business is divided into N modules for independent management, and each module is called business component. All business components need to rely on the encapsulated base components, business components do not rely on each other, so that each business module can run independently. The module of the whole project is encapsulated in the APP layer.

The jump between service modules can be realized by Arouter. Communication between business modules can be achieved through messages (EventBus).

Iii. Infrastructure construction

1. Component frame diagram

2. Project structure drawing based on component frame drawing

3. Each module is described next

There are altogether five modules in the project, including three business modules, one basic module and one APP shell module.

After building the project, we need to configure the switch of “integrated development mode” and “Component development mode” for the three modules. We can define the variable isModel in the gradle.properties file. IsModel =false means “integrated development mode”, isModel=true means “component development mode” (note: every change in isModel must be Sysn).

1) APP shell module

Gradle is configured to rely on different business components based on the isModel field in the configuration file.

2) Baselibs module

Mainly responsible for the encapsulation of common parts, such as MVP architecture, BaseView encapsulation, network request library, image loading library, tool classes and custom controls, etc.

To prevent duplicate dependencies, all third party libraries are placed in this module, and the business module does not make any third party dependencies, only relying on the Baselibs module.

The structure of the Baselibs module is as follows:

Gradle library introduced in the Baselibs module

3) Service modules (module_NEWS, MODULE_VIDEO, module_ME)

Each business module exists as a library in integrated development mode; Exists as an Application in Component development mode and can be run separately.

Since the configuration files of each business module are similar, the module_news module is used as an example.

Here is the gradle configuration file for the module_news module:

4) Config file config.gradle, configure the third library in the project, app version and so on

And finally, don’t forget that in engineeringbuild.gradleImport the configuration file

4. Interaction between business modules

The jump between service modules can be realized by Arouter. Communication between business modules can be achieved through messages (EventBus).

1. Arouter realizes the jump between service modules

We have previously relied on Arouter (see github.com/alibaba/ARo…). , it only takes the following two steps to realize the jump:

The first step

  • Gradle configuration

The second step

  • You need to specify the target page and the parameters to carry, and then call navigation().

The third step

  • First call arouter.getInstance ().inject(this) in onCreate;
  • Then annotate the page with the @route annotation and define a path to the page in the path variable.
  • And then finally, for the variables that are being sent in, we’ll just define a field with the same name and we’ll label it with the @AutoWired variable, and Arouter will automatically assign to that field

2. EventBus implements communication between business modules

Manage messages using a third party such as EventBus. BaseActivity and BaseFragment classes in baselibs components encapsulate the message simply. Subclasses only need to override useEventBus() to return true to register the event.

5. Problems encountered in the construction process

1, the AndroidManifest

We know that when the APP is packaged, all the AndroidManifest is merged, so the Activity of each business component only needs to be registered in its own module.

If the business component is to run separately, it needs a separate AndroidManifest, which can be loaded in Gradle sourceSets.

Gradle configuration

Note: AndroidManifest in integration mode does not require Application configuration. AndroidManifest in component mode requires Application configuration separately and must inherit BaseApp.

2. Resource file conflicts

Resource files in different business components may have the same name. Therefore, resource file conflicts may occur. You can set resource prefixes to prevent resource file conflicts.

Gradle configuration, using the module_news module as an example

After this configuration, if we do not prefix the named resource file, the compiler will tell us that we did not prefix it.

At this point,AndroidThe basic componentization framework has been built, please correct any mistakes.

Five, the last

Full project address: github.com/iceCola7/An…

To read more

A wave of Flutter cool effects hits

Gold three silver four, 2019 latest interview actual combat summary

Never bother with algorithms, bubble sort optimization, okay?

Animation: Learn TCP three-way handshake and four-way wave in one move

For Gradle, Groovy closures are enough

Welcome to follow my wechat technical number, Terminal R&D Department, ID :codeGoogler, to communicate and learn together!