android take pictures! This answer made me miss the offer! It's too late if you don't brush up the questions!

beginning

Engaged in Android development for many years, the bends and hurdles that I have gone through, the sadness and sorrow of the outsiders.

My deepest feeling is that choice is greater than effort . By choosing the right direction, you will be able to go further and firmer.

Mr. Lei said before, "Pigs can fly in the wind". So now the Hongmeng system is on fire, and many developers want to switch to Hongmeng.

We have been chasing the wind, and we have been missing. Almost every time a project is changed, programmers must master new technologies: "NET Unit Testing Art", "Interesting Algorithms (C Language Implementation)", "Android Application Development Secrets" ...

I read more and more books about Android development. It seems that I have learned a lot, but the salary and ability growth are very limited. My colleagues even have 3 years of work experience and an annual salary of 400,000 yuan, but I still don't know when the gap was secretly opened.

In fact, many of us just float on the surface during the development process, and the resumes we write are unsightly. For example, in our development, everyone should know and understand the importance of performance optimization. However, due to insufficient knowledge of the entire knowledge at work, they will not have their own understanding of performance optimization.

First, let's take a look at the difference between componentized projects and traditional projects:

In traditional projects

We usually have a Libary module of commonLib and an application module of app. The logic of the business is written in each functional module of the app and placed in different packages. This has the following main disadvantages:

1. No matter how well the subcontracting 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.

2. When we debug a small function, we need to rebuild the entire project every time we modify the code, which is very unreasonable (I don't know if the hot deployment of AS solves this problem)

3. Multi-person joint development is prone to conflicts and code coverage issues in version management

In a componentized project

In addition to the commonLib and app modules, we divide the business component modules according to their functions (eg: WeChat can be divided into four major modules: chat, contract, find, and mine). The previous package becomes the current module, which adds a sense of hierarchy; Each functional module can be compiled separately, which speeds up the compilation speed and also provides support for providing unit module testing; multi-person development is only responsible for their own modules, which directly avoids conflicts in version management.

Figure 1-0 Componentized basic project structure diagram

After understanding the main problem that componentization solves for us, let's take a look at what we need to do

In fact, the initial realization of organization is actually only two problems that we have to solve in the end:

1. Set up the dependencies between modules, and make the business modules can be compiled separately-can be solved by configuring gradle
2. Page jump and communication between business modules-use Alibaba's open source ARouter to solve it
Next, let’s take a look at how to do it
First look at the problem of inter-module dependencies
We can refer to the four modules of WeChat (chat, contract, find, mine) to configure

First of all, the basic structure of our project is as follows:

Figure 1-1 Project structure

We need to build a total of 6 modules, in addition to 4 functional modules, there is a basic common library and an application as a startup.

After the project is built, we need to configure a switch for whether to compile separately for the 4 modules:

Figure 1-2 Switches to compile each module separately

Regarding the configuration position of the switch, this is a problem, we add it to the gradle.properties file, so that every time we modify the value, we can trigger the rebuild of gradle, so that we can compile the module separately.

The switches that we compiled separately are configured, now let's configure the dependencies between the 6 modules:

Figure 1-3 Basic dependencies of app and function lib

First of all, in order to facilitate the interaction between modules, we borrowed Ali’s full ARouter library, so I strongly recommend adding dependencies on ARouter and commonlib in every non-common library (including the main Application).

Secondly, we need to install the switch of whether to compile separately for the 4 functional module libraries. We need to modify the following 2 places:

Figure 1-4 Function module configuration separate compilation switch

It can be seen that what we want to modify is the place where I am framed by the red box. When our switch is turned on, we compile it as a separate application and give it a unique applicationId so that we can pass the The switch configured in gradle.properties controls whether it is compiled separately as an application.

For our entry module-app module, we need to do the following configuration:

Figure 1-5 Gradle configuration of the main module

In addition to configuring the basic ARouter and commonlib dependencies, we also need to select whether we need to rely on our functional modules according to the switch in the gradle file of the app module. This corresponds to the configuration in each functional module.

For other component modules, repeat the above steps to complete the construction of the componentized framework:

Figure 1-6 Project structure diagram

After completing the construction of the componentized framework, let's briefly take a look at some of the distinctive usage methods in the framework.

Let's first look at how the pages of each module jump.

We have already relied on ARouter before, and we need the following steps to use it to help us realize the jump:

picture 2-1

The method of jumping is as shown in Figure 2-1. We need to mark the target page, attach the parameters to be transmitted, and then call navigation() to jump. However, someone asked how the target page looks like a path. How is it defined?

Figure 2-2 Configuration of the target page

  • First, annotate the page with @Route annotation, and define a path to the page in the path variable
  • For the transmitted variable, we directly define a field with the same name and mark it with the @Autowired variable, and Arouter will automatically assign a value to the field
  • Finally, we also need to inject the page into ARouter (the principle is similar to ButterKnife), let him help us complete the work we need

In this way, we have completed the jump between pages, is it simpler and more reasonable than our traditional method?

Finally, let's take a look at how the 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
  • How can the methods in the home be called by other modules (including the main module and other component modules)

Figure 3-1 Definition of public interface
First create an interface that exposes methods in the commonlib module, 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.

Figure 3-2 Configuration in the module that provides the method

Here we also use Arouter's @Router annotation to provide routing for this service.

Finally, we can use the @Autowired annotation in other modules to call the method

Figure 3-3 Method call

It can be seen that we also used the @Autowired annotation to initialize the baseService service, and inject the page into the Arouter to call the method in the service, and the dependency on the service is based on the interface, which greatly improves its flexibility!

If you lack direction on the way to advancement, you can click on my [Github] to join our circle and learn and communicate with Android developers!
All the content below is available on GitHub!

  • A full set of manuals for Android advanced learning

    img

  • Android benchmarking Ali P7 learning video

    img

  • BATJ Android high-frequency interview questions

    img

Finally, borrow my favorite Steve Jobs quotation as the end of this article:

People can't do too many things in one's life, so everything must be done wonderfully.
Your time is limited, so don't live for others. Don't be limited by dogma, don't live in other people's ideas. Don't let the opinions of others influence your inner voice.
The most important thing is to be brave to follow your own heart and intuition. Only your own heart and intuition know your own true thoughts. Everything else is secondary.

People can't do too many things in one's life, so everything must be done wonderfully.
Your time is limited, so don't live for others. Don't be limited by dogma, don't live in other people's ideas. Don't let the opinions of others influence your inner voice.
The most important thing is to be brave to follow your own heart and intuition. Only your own heart and intuition know your own true thoughts. Everything else is secondary.

Guess you like

Origin blog.csdn.net/Sunbuyi/article/details/114097703