An idea about componentization
As time passes, as the business changes, as developers come and go, and the code becomes more and more bloated and complicated, it must be broken up or it will be a disaster. Just like the old projects of our company, the coupling degree is extremely high. The business that has been stopped is still retained in the project, and we dare not delete it at all. New features come online because of regression testing, which sometimes takes longer than development.
Componentization is a word that we’ve seen countless times in various places, through various channels, and it’s usually illustrated with this picture of little robots, green in color, very bright and tonality.
Both componentization and pluginization belong to modular programming, but there are two different presentation modes. There is only one difference between the two: pluginization supports dynamically adding and modifying online modules, while componentization can only add and remove existing modules.
E-commerce apps with frequent dynamic functions on the project line are suitable for plug-in application. The change demand is not strong tool class APP, suitable for the use of componentization. Our company does not require much flexibility, so we adopt a componentized approach.
Several key points of componentization
The key points of componentization are not too few. The following are the main parts I think, and the general ideas will be sorted out by asking and answering questions.
01. How to break down a huge project into organic units?
I think it should be divided into three parts, the main project, the underlying common library, and the business components. The basic common library is extracted first for other components to call, and the rest is grouped into components according to business logic, which is beneficial to the iterative development of business in the later period. The main project is responsible for loading components.
02. Can components run separately? How?
Each component should be able to run independently so that we can develop and test separately in a module. You can use the Apply plugin: ‘com.android.application’ and the apply plugin: ‘com.android.library’ to convert the two identities.
Here do not be misunderstood by the picture, the arms and legs in the componentization left the body, in fact, can still live independently.
03. How to make components independent from each other?
When components are independent from each other, coupling is reduced, which is mainly reflected in resource isolation and code isolation. Code isolation Compile time isolation can be achieved by using the runtimeOnly dependency syntax after Gradle3.0. Resource isolation, currently there is no official ready-made isolation scheme, temporarily you can use the resourcePrefix attribute, manual maintenance.
04. Components are independent of each other. How is data transmitted?
Considering the routing scheme, there is a very mature routing library ARouter.
In addition to the above problems, there are also problems such as the integration and debugging of components and the life cycle of components, which I think can be ignored in the early stage and left to the later optimization.
Before vs. after componentization
The process of componentization is very painful, but the development experience after completion is really super, super, super happy! Because the business module logic is separated, the code is less coupled, resulting in the following benefits:
- Short compile time
- Development cycle reduction
- Reduce test regression
- Fast locating problem
- Business module migration is convenient
The following are the decoupled business modules in our company’s project componentization process:
Android green robots sit in two rows, very cute.
How do I quickly start componentization
The first step, teenager, is to do your own search to get the knowledge about componentization, to have it in your mind.
The second step is to sort out the overall business logic and code architecture for your target project and make a feasible componentization solution. This is a very important step, and it’s important to get to the bottom of it and expose more problems before you execute. Otherwise, imagine that you feel like you have to move 80% of a module, and find that the business logic cannot be separated from it, or the technical implementation is difficult. This is a waste of time and energy, and affects your mood.
The third step is to submit the proposal to the technical leader. After the approval, the application is scheduled for development.
Step four: bathe and burn incense, worship the code god, and begin.
As a friendly tip, it is best to pull a separate branch, as this is likely to persist, so that it does not interfere with the normal iteration of the project.
My organizational steps
-
Extract the basic tool class BaseLib, network encapsulation library NetWorkLib.
-
Extract BasicRes, manage common resources, such as BaseActivity/BaseApplication base classes, dialog boxes, res resources.
-
Separate the service logic into modules.
-
Select Arouter as the routing scheme to connect each component Module.
Related to recommend
The concept of componentization has been around for quite some time, because there are many excellent articles available for reference. Here are my recommendations:
Zhang Mingqing -JIMU componentization scheme
I got a series of articles written by Zhang Mingqing, former technical director of APP, from which my componentization idea also comes.
Summarize a wave of android componentized open source solutions
This article compares some representative open source componentization development schemes on the Internet from multiple dimensions, which is suitable for you to quickly have an overall understanding of Android componentization.
And finally, my own series on componentization, which is easy to understand and I hope you’ll find helpful:
What led me to componentization?
Solve the problem of ModuleApplication coexistence in componentization