“This is the first day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021”

App startup process

1. About the startup of Android system

The startup process of the system is very complicated, and this is just a brief overview. Start by going to Google for an architectual layering diagram

Zygote (in c++ framework) ->zygote (in Java) ->zygote (in Java The framework layer) * *

  • The system_server process is responsible for starting and managing the entire Java FramWork layer including ActivityManagerService, PackageManagerService, WindowManagerService and other services

    The essence of Service is Binder. If you look at the implementation of ActivityManagerService, it is a Stub subclass of Binder

public class ActivityManagerService extends IActivityManager.Stub
        implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback, ActivityManagerGlobalLock {}
Copy the code
  • The first process that the Zygote process forks out of the APP layer is the Launch process, which is the desktop that the user sees
  • All app processes are forked by zygote

2. APP startup process

  • When clicking on the desktop APP, the startActvity method in the Launch process calls the startActivity in the AMS managed by the System_server process via binder communication. And AMS and continue to call ATMS StartActivity (# ActivityTaskManagerService) method for the real start.
  • When the system_server process receives the message, it sends a request to Zygote to create the process (through socket communication).
  • The Zygote process forks out the app process and executes the Main method of ActivityThread
Public static void main(String[] args) {// There is a sysCall layer between sysCall Native and the Kernel AndroidOs.install(); CloseGuard.setEnabled(false); Environment.initForCurrentUser(); / / get the user space configuration files final File configDir = Environment. GetUserConfigDirectory (UserHandle. MyUserId ()); TrustedCertificateStore.setDefaultUserDirectory(configDir); / / initialize each process (MediaServiceManager TelphoneServiceManager) initializeMainlineModules (); // Initialize the main thread looper looper.prepareMainLooper (); long startSeq = 0; if (args ! = null) { for (int i = args.length - 1; i >= 0; --i) { if (args[i] ! = null && args[i].startsWith(PROC_START_SEQ_IDENT)) { startSeq = Long.parseLong( args[i].substring(PROC_START_SEQ_IDENT.length())); ActivityThread thread = new ActivityThread(); thread.attach(false, startSeq); if (sMainThreadHandler == null) { sMainThreadHandler = thread.getHandler(); } if (false) { Looper.myLooper().setMessageLogging(new LogPrinter(Log.DEBUG, "ActivityThread")); } Looper.loop(); throw new RuntimeException("Main thread loop unexpectedly exited"); }Copy the code
  • As you can see from the main method, thread.attach(false, startSeq) is called after the ActivityThread is created and the ApplicationThread is initialized to communicate with AMS
  • The App sends attachApplication requests to SYtem_server through Binder. The APPLICATION uses ITS Binder to invoke AMS plication in sytem_server. AMS uses the attachApplication method to bind ApplicationThread objects to AMS
  • After receiving the attachApplication request and performing preparations, the system_server process HandleBindApplication requests (initializes the Application and calls the onCreate method) and scheduleLaunchActivity requests (creates and starts the Activity) are sent to the App process via Binder IPC.
  • The App’s binder threads send BIND_APPLICATION and LAUNCH_ACTIVITY messages to the main thread via the handler. Instead, AMS communicates with the main thread’s internal class ApplicationThread through the Binder, which in turn interacts with the main thread through Handler messages.
  • After receiving the Message, the main thread creates the Application and calls the onCreate method, then creates the target Activity through reflection and calls the activity.oncreate () and other methods
  • By this, the App was launched, begin to enter the Activity life cycle, after the onCreate/onStart/onResume method, after the UI rendering of App main interface