"Tencent's WeChat business department recruits two sides"-what process does an APP go through from launch to main page display?

I left in August and it has been 2 months. After the surprise review, the result can be regarded as an explanation. You Zan (Shenzhen), the investment has passed three sides, waiting for the HR side, Tencent has passed the two sides, and the state at the interview was OK. I wonder if there will be three sides. HR noodles! ( I prefer to go to Tencent), Kingdee died on the second side. What's the reason? (There are reasons for my own status at the time, as well as questions from the interviewer, so I don’t make judgments.) In general, I interviewed 4 major factories and the results were okay!

This is an interview question that Tencent encountered on the second side. I didn't answer it very well at the time. I wanted to repeat it! It is a summary, and I want to warn everyone to avoid entering the pit.

 

1. Process overview

Startup process:

① Click on the desktop App icon, the Launcher process uses Binder IPC to initiate a startActivity request to the system_server process;

② After the system_server process receives the request, it sends a request to create a process to the zygote process;

③The Zygote process forks a new child process, the App process;

The App process initiates an attachApplication request to the sytem_server process through the Binder IPC;

⑤ After receiving the request, the system_server process performs a series of preparations, and then sends a scheduleLaunchActivity request to the App process through the binder IPC;

⑥ The binder thread (ApplicationThread) of the App process sends a LAUNCH_ACTIVITY message to the main thread through the handler after receiving the request;

⑦ After receiving the Message, the main thread creates the target Activity through the launch mechanism and calls back methods such as Activity.onCreate().

⑧At this point, the App is officially started, and it begins to enter the Activity life cycle. After executing the onCreate/onStart/onResume methods, you can see the main interface of the App after the UI rendering is over.

The above series of steps briefly introduce the process of starting an APP to the main page display. Maybe some of the terms in these processes are a bit confusing, what is Launcher, what is zygote, what is applicationThread...

Below we introduce them one by one.

2. Theoretical basis

1.zygote

Zygote means "fertilized egg". Android is based on the Linux system. In Linux, all processes are directly or indirectly forked by the init process, and the zygote process is no exception.

In the Android system, zygote is the name of a process. Android is based on the Linux System. When your phone is turned on, a process called "init" will be started after the Linux kernel is loaded. In Linux System, all processes are forked by the init process, and our zygote process is no exception.

We all know that every App is actually

● A separate dalvik virtual machine

● A separate process

So when the first zygote process in the system runs, opening the App after that is equivalent to starting a new process. In order to achieve resource sharing and faster startup speed, the way the Android system starts a new process is achieved by fork the first zygote process. So, except for the first zygote process, the processes of other applications are all child processes of zygote. Now, do you understand why this process is called "fertilized egg"? Because it is like a fertilized egg, it can divide rapidly and produce cells that are the same as genetic material!

2.system_server

SystemServer is also a process, and it is fork by the zygote process.

Knowing the essence of SystemServer, we are not too unfamiliar with it. This process is one of the two very important processes in the Android Framework-the other process is the above zygote process.

Why is SystemServer very important? Because important services in the system are all started in this process, such as ActivityManagerService, PackageManagerService, WindowManagerService, etc.

3.ActivityManagerService

ActivityManagerService, abbreviated as AMS, is a server-side object responsible for the life cycle of all activities in the system.

The timing of the initialization of ActivityManagerService is very clear, that is, when the SystemServer process is started, the ActivityManagerService will be initialized.

The following describes the concepts of server and client in the Android system.

In fact, the concept of server client does not only exist in Web development, but also in the design of the Android framework. The server side refers to the system services shared by all apps, such as the ActivityManagerService we mentioned here, and the previously mentioned PackageManagerService, WindowManagerService, etc. These basic system services are shared by all apps. When an App wants to implement During an operation, you must tell these system services. For example, if you want to open an App, then we can open it after we know the package name and the MainActivity class name

Intent intent = new Intent(Intent.ACTION_MAIN);  
intent.addCategory(Intent.CATEGORY_LAUNCHER);              
ComponentName cn = new ComponentName(packageName, className);              
intent.setComponent(cn);  
startActivity(intent);

However, our App cannot open another App directly by calling startActivity(). This method will go through a series of calls, and finally tell AMS: "I want to open this App. I know his address and name. You can help Let me open it!” So ​​AMS informs the zygote process to fork a new process to start our target App. This is like the browser wants to open a hyperlink, the browser sends the web page address to the server, and then the server sends the required resource files to the client.

After knowing the client-server architecture of the Android Framework, we still need to understand one thing, that is, our App, AMS (SystemServer process) and zygote process belong to three separate processes. How do they communicate?

App and AMS carry out IPC communication through Binder, and AMS (SystemServer process) and zygote carry out IPC communication through Socket. Detailed introduction later.

So what is the use of AMS? As we know before, if you want to open an App, you need AMS to notify the zygote process. In addition, in fact, all the activation, suspension, and shutdown of activities need to be controlled by AMS, so we said that AMS is responsible for everything in the system Activity life cycle.

In the Android system, the startup of any Activity is completed by the cooperation between AMS and the application process (mainly ActivityThread). The AMS service uniformly schedules the activity startup of all processes in the system, and the startup process of each Activity is specifically completed by the process to which it belongs.

4.Launcher

When we click on the icon on the mobile phone desktop, the App is started by the Launcher. But, have you ever thought about what a Launcher is?

Launcher is essentially an application, like our App, it also inherits from Activity

packages/apps/Launcher2/src/com/android/launcher2/Launcher.java

public final class Launcher extends Activity
        implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
                   View.OnTouchListener {
                   }

Launcher implements callback interfaces such as click and long press to receive user input. Since it is an ordinary app, our development experience is still applicable here. For example, how do we open the app when we click the icon? Capture the icon click event, and then startActivity() sends the corresponding Intent request! Yes, Launcher does this too, it's so easy!

5.Instrumentation和ActivityThread

Each Activity holds a reference to the Instrumentation object, but only one Instrumentation object exists in the entire process. Most of the methods in the Instrumentation class are related to Application and Activity. This class is a tool class that completes the initialization and life cycle of Application and Activity. The Instrumentation class is very important, and the call to the Activity life cycle method is inseparable from him. He can be said to be a big housekeeper.

ActivityThread depends on the UI thread. App and AMS transmit information through Binder, so ActivityThread is dedicated to diplomatic work with AMS.

6.ApplicationThread

We have already known that the start of the App and the display of the Activity require the control of AMS, so we need to communicate with the server, and this communication is two-way.

Client-->Server

And because it inherits the same public interface class, ActivityManagerProxy provides the same function prototype as ActivityManagerService, so that users can't feel whether the Server is running locally or remotely, so that these important system services can be called more conveniently.

Server-->Client

Communication is still through Binder, but another pair is replaced, ApplicationThread and ApplicationThreadProxy.

They all implement the same interface IApplicationThread

  private class ApplicationThread extends ApplicationThreadNative {}

  public abstract class ApplicationThreadNative extends Binder implements IApplicationThread{}

  class ApplicationThreadProxy implements IApplicationThread {}

Well, there are a lot of words in front of Luo Li, introduced a lot of nouns, may not be very clear, it does not matter, the following is introduced in conjunction with the flowchart.

Three, start-up process

1. Create a process

① From the startActivity() method of Launcher, call the startActivity method of ActivityManagerService through Binder communication.

② A series of tossing, finally call the startProcessLocked () method to create a new process.

③This method will pass parameters to the Zygote process through the socket channel mentioned earlier. Zygote hatches itself. Call the ZygoteInit.main() method to instantiate the ActivityThread object and finally return the pid of the new process.

④ Call ActivityThread.main() method, ActivityThread then calls Looper.prepareLoop() and Looper.loop() in turn to start the message loop.

The method call flow chart is as follows:

A more straightforward process explanation:

①App initiating process: When starting an application from the desktop, the initiating process is the process where the Launcher is located; when starting a remote process from an App, the sending process is the process where the App is located. The initiating process first sends a message to the system_server process through the binder;

②system_server process: call Process.start() method, send a request to create a new process to the zygote process through the socket;

③Zygote process: After executing ZygoteInit.main(), it enters the runSelectLoop() loop. When there is a client connection, it executes the ZygoteConnection.runOnce() method, and then forks out a new application process after layered calls;

④ New process: execute the handleChildProc method, and finally call the ActivityThread.main() method.

2. Binding Application

After creating the process above, execute the ActivityThread.main() method, and then call the attach() method.

Bind the process to the specified Application. This is done by calling the bindApplication() method in the ActivityThread object of the previous section. This method sends a BIND_APPLICATION message to the message queue, and finally processes the message through the handleBindApplication() method. Then calls the makeApplication() method to load the App classes into the memory.

The method call flow chart is as follows:

A more straightforward process explanation:

(If you don’t understand terms such as AMS, ATP, etc., there will be an explanation later)

3. Show the Activity interface

After the first two steps, the system already has the application process. The subsequent calling sequence is the normal activity of starting a new process from an existing process.

The actual calling method is realStartActivity(), it will call scheduleLaunchActivity() in the application thread object to send a LAUNCH_ACTIVITY message to the message queue, and handle the message through handleLaunchActivity(). In handleLaunchActivity(), call back the onCreate() method and onStart() method of Activity through the performLaunchActiivty() method, and then call back the onResume() method of Activity through the handleResumeActivity() method, and finally display the Activity interface.

A more straightforward process explanation:

4. Binder communication

Abbreviation:

ATP: ApplicationThreadProxy

AT: ApplicationThread

AMP: ActivityManagerProxy

**AMS: **ActivityManagerService

Diagram:

① The startProcessLocked method is called in the system_server process. This method finally informs the Zygote process of the need to create a new process through the socket method, and blocks waiting for the Socket to return the pid of the newly created process;

② The Zygote process receives the message sent by the system_server, and then uses the fork method to copy the zygote process to generate a new process, and load the ActivityThread-related resources into the new process app process, which may be used to carry components such as activity;

③ In the new process app process, query the servicemanager for the binder server AMS in the system_server process, and obtain the corresponding client, which is AMP. With this pair of binder c/s pairs, then the app process can pass the binder to the cross-process system_server Send the request, namely attachApplication()

④ After the system_server process receives the corresponding binder operation, after multiple calls, it uses ATP to send a binder request to the app process, that is, bindApplication. The system_server has ATP/AMS, and each newly created process will have a corresponding AT/AMP, which can cross The processes communicate with each other. This is the complete ecological chain of the process creation process.

The above roughly introduces the process of an APP from startup to the main page display. The process is mainly introduced from a macro perspective, which can be understood in conjunction with the source code.


After reading the likes, develop a habit, search "Programming Ape Development Center" on WeChat to follow this programmer who likes to write dry goods.

In addition, there is a complete test site for interviews with major Android first-line companies, and the information is updated in my [Github] . Friends who need interviews can refer to them. If it is helpful to you, you can order a Star!

Address : [https://github.com/733gh/xiongfan]

Guess you like

Origin blog.csdn.net/qq_39477770/article/details/108985807