Android应用进程启动流程 发表于 2020-11-29 更新于 2023-10-18 分类于 Android 本文介绍Android应用进程的启动流程。从一个应用的Activity中(比如luncher),启动 本文基于Android8.0源码。 启动流程图点击可以看大图 ActivityManagerServiceActivityManagerServiceProcessProcessZygoteProcessZygoteProcessZygoteStateZygoteStateLocalSocketActivityThreadActivityThreadApplicationThreadZygoteInitZygoteInitZygoteServerZygoteConnectionZygoteZygoteRuntimeInitRuntimeInitmainZygoteServerregisterServerSocketnew LocalServerSocket()runSelectLoopwhile(true)Os.poll()startProcessLockedstartstartstartViaZygote组装zygote参数openZygoteSocketIfNeededconnectLocalSocketnew LocalSocketAddressconnectconnectacceptCommandPeercreateNewConnectionZygoteConnectionZygoteStatezygoteSendArgsAndGetResult(BufferedWriter)write(arg)read socketrunOnceforkAndSpecializepidif (pid>0) handleParentProcwrite pidread pidreturn ProcessStartResultreturn ProcessStartResultif (pid==0) handleChildProczygoteInitnativeZygoteInitapplicationInitinvokeStaticMainthrows Zygote.MethodAndArgsCallermain() catch ExceptionmainLooper.prepareMainLooper()new ActivityThread()ApplicationThreadattach(IApplicationThread)attachApplicationBinder.getCallingPid()attachApplicationLockedfind ProcessRecord by pidbindApplicationLooper.loop()