In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail "how to use ARouter to achieve componentization in Android". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to use ARouter to achieve componentization in Android" can help you solve your doubts.
1. No matter how well the subcontract is done, as the project grows, the project will gradually lose its sense of hierarchy, and it will be very difficult for others to take over.
two。 We need to re-build the whole project every time we modify the code when we have a small function in debug, which is very unreasonable (I wonder if the hot deployment of AS has solved this problem)
3. Multi-person joint development is prone to conflicts and code coverage in version management.
# in componentized projects
In addition to commonLib and app modules, we divide each business component module according to function (eg: × × × can be divided into four large chat,contract,find,mine modules). The previous package has become the current module, increasing the sense of hierarchy; each functional module can be compiled separately, speeding up the compilation speed and providing support for unit module testing. Multi-person developers are only responsible for their own modules, directly avoiding version management conflicts.
# after understanding that componentization is the main problem we solve, let's see what needs to be done
In fact, there are only two problems we have to solve in the end:
# 1. Set dependencies between modules and allow business modules to be compiled separately-- solved by configuring gradle
# 2. Page jump and communication between business modules-- it can be solved by using Ali open source ARouter
# next, let's take a look at how to do it.
# first of all, let's take a look at the problem of dependency between modules.
# We can configure it by referring to the four modules (chat,contract,find,mine) of × ×
First of all, the basic structure of our project is as follows:
We need to build a total of 6 module, in addition to 4 functional modules, there is a basic common library and a startup application.
After building the project, we need to configure the four module with a switch of whether to compile separately:
This is a problem with the configuration location of the switch, and we add it to the gradle.properties file so that every time we change the value, we can trigger the reconstruction of gradle, so that we can compile module separately.
Now that the switch we compiled separately is configured, let's configure the dependencies between the six module:
First of all, we borrowed Ali's full ARouter library to facilitate interaction between module, so I strongly recommend adding dependencies on ARouter and commonlib in every non-common library (including the main Application).
Second, the four functional module libraries we need to install the switch we previously configured whether to compile separately or not, we need to modify the following two places:
You can see that what we want to modify is where I am framed. When our switch is turned on, we compile it as a separate application and give it a unique applicationId, so we can control whether it is compiled as a separate application through the switch just configured in gradle.properties.
For our entry module--app module, we need to do the following configuration:
In addition to configuring basic ARouter and commonlib dependencies, we also need to choose whether we need to rely on our functional modules according to the switch in the gradle file of the app module, which echoes the configuration in each functional module.
For other component modules, repeat the above steps to complete the construction of the component-based framework:
# after completing the construction of the component-based framework, let's simply take a look at some unique usage methods in the framework.
# Let's first take a look at how the pages of each module jump.
We have relied on ARouter before (see https://github.com/alibaba/ARouter for more details), and we need the following steps to help us jump:
As shown in figure 2-1, we need to mark the target page with the parameters to be transmitted, and then call navigation () to jump, but someone asked how the target page looks like a path and how is it defined?
The first step is to annotate the page with @ Route and define a path to the page in the path variable
For the passed variable, we directly define a field with the same name marked with the @ Autowired variable, and Arouter will automatically assign a value to the field.
Finally, we need to inject the page into ARouter (the principle is similar to ButterKnife) and ask him to help us do the work we need.
In this way, we have completed the jump between pages, is it more simple and reasonable than our traditional method?
# finally, let's take a look at how components provide services to each other.
Here I want to call the sayHello method of the home component in the main module to Toast a person's name
So how can the methods in home be called by other modules (including the main module and other component modules)
First, create an interface in the commonlib module that exposes the method, define the interface signature, and inherit the Iprovider interface
Then inherit the interface defined in commonlib in the home module and implement the signature method.
Here we also use the @ Router annotation of Arouter to provide routing for this service.
Finally, we can call this method using the @ Autowired annotation in other modules
After reading this, the article "how to use ARouter to realize componentization in Android" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.