Android Learning Series (18)-app Engineering Structure Construction

Source: Internet
Author: User

This article is a casual talk about how we can build a good architecture during the initial stage of project initialization in Android development.
I don't think there is much talk about the android architecture because of mobile phone restrictions. But from the perspective of development, we can see that it is neat.Code, Beautiful layering is always a kind of comfortable to enjoy.
From the perspective of art, we are pursuing a kind of beauty.

This article first analyzes several popular Android software packages, and finally builds our own general Android Engineering Template Based on what we think is excellent.
1. micro disk
2. Jiuyi Diary
3. NetEase news
4. Xiaomi Application

1. micro disk
The microdisk architecture is relatively simple. I drew the most basic and backbone:

Level 1: COM. Sina. vdisk: COM. Sina (Company domain name) + app (ApplicationProgramName ).
Layer 2: name of each module (main module vdiskclient and entity module)
Layer 3: specific sub-packages under each module, implementation class.
We can draw a simple and classic structure in the above analysis. Generally, some global packages or classes are put under the application package. If there are multiple large modules, they can be divided into multiple packages, it includes a main module.
Define base classes in the main module, such as baseactivity. If there are sub-modules under the main module, you can create corresponding packages for sub-modules under the main module. Note: Sometimes, if there is only one main module, we can omit the Module layer, that is, baseactivity. Java and Its submodules are directly taken to the second layer.
In the entity module, only the corresponding entity classes should be defined and defined for global calling (however, this may not be the case, which will be discussed later ). In a micro-disk application, almost all entity classes are named after XXX + info. This kind of naming is also a kind I agree with. In terms of semantics, I think xxxmodel. the naming of Java is more vivid and authentic. The xxxmodel gives me a feeling that it is too mechanical and rigid. This is entirely my personal opinion. In specific operations, I mainly use my habits. Another point is that in xxxinfo, many entity classes in Java do not use the get/set method, but directly use the public field name. I recommend this method. Especially in mobile development, the get/set method is not necessary in many cases and consumes performance. Of course, if you need to set certain control over fields, the get/set method can also be used as appropriate.

2. Jiuyi Diary
Compared with the micro-disk engineering structure, the structure of the Jiu Yi diary is slightly more complex. For example:

1). the first layer is the same as the previous micro disk.
2). There is no module classification in the second layer. You can directly put all the required implementation classes below, mainly for some diary-related activities.
3 ). the object package command on the second layer is the model package, which not only stores the object class XXX. java, and stores related classes of higher-level object classes, such as xxxmanager. java, xxxfactory. java. for more information, see android. jar structure, we found that activity. java and activitymanager. java, view. java and ViewManager. java, bitmap. java and bitmapfactory. java and so on. n similar classes are all under the same package. I personally think the corresponding manager and factoty classes for storing object classes under the entity package are also correct, which is a structure we should adopt. This breaks through the statement that the physical package is stored under the previous micro-disk and only stores the object class. In reality, from a flexible and reasonable perspective, the object content stored in the object package of Jiuyi diary is more practical.
4) added the config, constant, and common packages not involved in the previous micro-disk in the second layer. First, config stores some system configurations, such as system-level constants or static variables such as names and application parameters. If you have configurations for other large modules, for example, if you have a complicated user management module, you can add a userconfig. java stores some user configuration information. Second, the constant package stores all public static final constants, defined states, types, and so on. For performance considerations, enumeration is not recommended in Android development. Define a public library in the common package. The content structure of the common package cannot be well described because the application is single.
5). The common package involves the comparison of multiple later software, and then we can draw a conclusion.
Through the analysis of Jiuyi diary, we have learned a lot to make our architecture more plump and powerful.

3. NetEase news
Netease news is indeed doing well. From an application perspective, it is one of my favorite applications. What is its engineering structure?
the engineering structure of Netease news is quite different from that of the apps in the previous two. It is not divided by module, but by component type, put all the classes of this type under it. This method of placing all the activities under the activity package is indeed common in Android development.
1 ). the first layer is divided into two layers. It can be seen that the public package jar is used here, when developing a public package, we should also follow the "Company domain name + public module name" combination method to better name it.
2). In the third layer (Green layer), both the activity and service package store all the activity components and service components. In fact, this includes a kind of code habit. There are often many classes related to activity, such as listeners, threads, and adapters. These classes are not directly stored in the activity package, but are directly written in the corresponding activity and defined in the form of anonymous or internal classes, otherwise, the activity package and service package will look messy.
because the Android app may not be very large and the activity or service package will not be messy, this method of Netease news is also of great reference value.

4. xiaomi applications
Xiaomi applications include three applications: Xiaomi sharing, Xiaomi reading, and Xiaomi tag. From the perspective of actual code development, I don't feel like I am in the same team, or developed by the same group of people. In this case, how does their architecture work?
the above structure and internal details are not quite the same in many places, but it is worth learning to make good things, so I just want to take out one of the points that I think are the most worth learning.
first, create a separate module package for the widgets and providers.
second, through observation, we found that each application in Xiaomi sharing has a common package, which not only has a common package at the application level, but also has a common package at the application level. What I want to say is that with the accumulation of project development in Android development, many common methods, classes, and functional modules can be extracted. This is the case between projects and within each project. Therefore, you can extract the corresponding public libraries for the methods called by each module of the Project class.
there is a problem here. The internal package of Public common package may involve a lot of content. Do you want to subscribe to the classification? How can we subscribe to the classification? I think this is the case. Generally, in mobile development, to reduce the package size, we will control the expansion of the common package. Often, the common package only includes some of the most concise and classic things, if there are few items, you do not need to subscribe to them. However, if your company develops hundreds of items and each project uses public modules such as behavior analysis and feedback, it is clearer to split the packages. All in all, it doesn't matter if you do not subscribe to it. Try to make your code structure clear and keep your thinking straight.

5. Gather the strengths of each family to build a family.
after a rough analysis above, we should have a clear and messy feeling about the android program architecture. I did not go to view the structure of other applications. Now I will summarize it and come up with a general engineering structure.
assume that the company is named Tianxia. Currently, the company is preparing to develop a book application, dating application, and life service application.
The following architecture should be drawn in the first place (the specific app development must be separated):
the company develops three applications, reader, friend, and life, among them, the common package is shared by these three applications. config and oauth are optional. View stores some of the most common custom views, such as dialog boxes and custom lists, if you think some views may not be generic, you 'd better place them in the common package of the application class.
if you have read Android Learning Series (6) -- for app modularization and engineering extension, this multi-application model should be shared with Android libraries to replace resources, engineering reuse problems. So I modified it as follows:

here, baseapplication performs basic initialization or configuration that will be used by all apps. Other applications should inherit this baseapplication.

Base is an android library and a complete Android project. Common is only a jar file. Of course, you can also develop it as needed. Other main project reader, friend, and life should reference the base project.
The ad package stores some of the company's custom soft ads.
The feedback package stores some user feedback and other common functional modules.
In fact, in many cases, the upgrade module can also be added to the base project to develop a unified software upgrade mechanism.
Next we will take reader as an example to complete the detailed design of its engineering structure.
Appconfig. Java under the config package stores the root configuration of the application, such as version and directory configuration.
The module package is divided into various modules. The blog module is the blog module, the BBS module is the forum module, and the person module is the personal information module of the entire site. The widget represents a special function module.
The common package stores some tool classes and custom views of the application.
Combined with the previous content, we concatenate the whole program and improve the final architecture of a reader as follows (the two freind and life are similar ):

Note: 1) functional modules and type modules can be divided. If not required, the modules can be omitted.
2) Division of components such as activity and service can be omitted if not needed.
3). All partitions can be omitted if not needed.
However, I personally think that the classification is clear, the architecture is clear, and it is worth your reference. Of course, I have not considered many of the details. If there is anything wrong, please point it out.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.