ActivityThread应用进程

article/2025/11/5 5:29:24

ActivityThread应用进程

android12-release

  • Zygote进程
  • SystemServer进程(即system_server)
  • Launcher启动过程、AMS:startActivity桌面启动应用 缺少具体应用进程启动

1. 涉及进程

  • Zygote进程:init进程 fork 第一个Java进程,Zygote是所有Java进程的父进程,包括system_server进程以及所有的App进程都是Zygote的子进程,注意这里说的是子进程,而非子线程。
  • system_server进程:启动和管理整个Java framework中服务。
  • Launcher/APP进程:应用进程之间;应用进程内部处理。

2. 时序图

在这里插入图片描述

2.1 简明流程图

在这里插入图片描述

  1. Launcher/App发起:当从桌面启动应用,则发起进程便是Launcher所在进程;当从某App内启动远程进程,则发送进程便是该App所在进程。发起进程先通过binder发送消息给system_server进程
  2. system_server进程:调用Process.start()方法,通过socket向zygote进程发送创建新进程的请求。
  3. zygote进程:在执行ZygoteInit.main()后便进入zygoteServer.runSelectLoop()循环体内,当有客户端连接时便会执行ZygoteConnection.processCommand()方法,最后fork出新的应用进程。
  4. 新进程:执行handleChildProc()方法,最后调用ActivityThread.main()方法。

3. Launcher进程启动APP Activity

Launcher --> Instrumentation --> AMS/ATMS
最终调用ActivityTaskSupervisor.startSpecificActivity,冷启动执行mService.startProcessAsync()先fork应用进程

  • ActivityTaskSupervisor.startSpecificActivity 对应ActivityRecord没有运行ActivityThread
  • mService.startProcessAsync()通过ATMS调用到AMS中startProcess()

在这里插入图片描述 在这里插入图片描述

4. system_server进程中调用Process.start()方法

4.1 ProcessList中判断

frameworks/base/services/core/java/com/android/server/am/ProcessList.java

  • getProcessRecordLocked(processName, info.uid) 判断是否已启动ProcessRecord
    在这里插入图片描述
  • app==null创建app = newProcessRecordLocked(info, processName, isolated, isolatedUid, hostingRecord)
  • startProcessLocked() 重载方法层层调用

4.2 Process通过socket连接到Zygote

frameworks/base/core/java/android/os/Process.java
frameworks/base/core/java/android/os/ZygoteProcess.java

  • 调用startResult = Process.start()
    在这里插入图片描述

  • argsForZygote数组保存了进程的uid、gid、groups、target-sdk-version、nice-name等一系列参数。

  • openZygoteSocketIfNeeded(abi)根据当前的abi来选择与zygote还是zygote64来进行通信。通过socket向Zygote进程发送消息。

private Process.ProcessStartResult startViaZygote(@NonNull final String processClass,@Nullable final String niceName,final int uid, final int gid,@Nullable final int[] gids,int runtimeFlags, int mountExternal,int targetSdkVersion,@Nullable String seInfo,@NonNull String abi,@Nullable String instructionSet,@Nullable String appDataDir,@Nullable String invokeWith,boolean startChildZygote,@Nullable String packageName,int zygotePolicyFlags,boolean isTopApp,@Nullable long[] disabledCompatChanges,@Nullable Map<String, Pair<String, Long>>pkgDataInfoMap,@Nullable Map<String, Pair<String, Long>>allowlistedDataInfoList,boolean bindMountAppsData,boolean bindMountAppStorageDirs,@Nullable String[] extraArgs)throws ZygoteStartFailedEx {ArrayList<String> argsForZygote = new ArrayList<>();// --runtime-args, --setuid=, --setgid=,// and --setgroups= must go firstargsForZygote.add("--runtime-args");argsForZygote.add("--setuid=" + uid);argsForZygote.add("--setgid=" + gid);// ... ...if (niceName != null) {argsForZygote.add("--nice-name=" + niceName);}// ... ...argsForZygote.add(processClass);if (extraArgs != null) {Collections.addAll(argsForZygote, extraArgs);}synchronized(mLock) {// The USAP pool can not be used if the application will not use the systems graphics// driver.  If that driver is requested use the Zygote application start path.return zygoteSendArgsAndGetResult(openZygoteSocketIfNeeded(abi),zygotePolicyFlags,argsForZygote);}
}

5. zygote进程中fork()\RuntimeInit.applicationInit()

5.1 fork()

frameworks/base/core/jni/com_android_internal_os_Zygote.cpp

相关日志Zygote : Forked child process 26092
am_proc_start: [0,26092,10264,com.tencent.mobileqq,pre-top-activity,{com.tencent.mobileqq/com.tencent.mobileqq.activity.SplashActivity}]
am_proc_bound: [0,26092,com.tencent.mobileqq]

// Utility routine to fork a process from the zygote.
pid_t zygote::ForkCommon(JNIEnv* env, bool is_system_server,const std::vector<int>& fds_to_close,const std::vector<int>& fds_to_ignore,bool is_priority_fork,bool purge) {SetSignalHandlers();// Curry a failure function.auto fail_fn = std::bind(zygote::ZygoteFailure, env,is_system_server ? "system_server" : "zygote",nullptr, _1);// Temporarily block SIGCHLD during forks. The SIGCHLD handler might// log, which would result in the logging FDs we close being reopened.// This would cause failures because the FDs are not allowlisted.//// Note that the zygote process is single threaded at this point.BlockSignal(SIGCHLD, fail_fn);// ... ...pid_t pid = fork();if (pid == 0) {// ... ...} else {ALOGD("Forked child process %d", pid);}// ... ...return pid;
}

bionic/libc/bionic/fork.cpp
bionic/libc/bionic/pthread_atfork.cpp

fork()采用copy on write技术,这是linux创建进程的标准方法,调用一次,返回两次,返回值有3种类型。

  • 父进程中,fork返回新创建的子进程的pid;
  • 子进程中,fork返回0;
  • 当出现错误时,fork返回负数。(当进程数超过上限或者系统内存不足时会出错)
int fork() {__bionic_atfork_run_prepare();int result = __clone_for_fork();if (result == 0) {// Disable fdsan and fdtrack post-fork, so we don't falsely trigger on processes that// fork, close all of their fds, and then exec.android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_DISABLED);android_fdtrack_set_globally_enabled(false);// Reset the stack_and_tls VMA name so it doesn't end with a tid from the// parent process.__set_stack_and_tls_vma_name(true);__bionic_atfork_run_child();} else {__bionic_atfork_run_parent();}return result;
}

5.2 新创建APP进程进入handleChildProc

  • closeSocket()关闭Zygote socket连接
  • Zygote.setAppProcessName(parsedArgs, TAG) 设置APP进程名称
  • ZygoteInit.zygoteInit() - ZygoteInit.nativeZygoteInit() JNI到app_main.cpp中AppRuntimeonZygoteInit()启动APP进程线程池proc->startThreadPool(),就是Binder线程
    在这里插入图片描述
  • RuntimeInit.applicationInit()经过class反射调用到ActivityThread.main
private Runnable handleChildProc(ZygoteArguments parsedArgs,FileDescriptor pipeFd, boolean isZygote) {/** By the time we get here, the native code has closed the two actual Zygote* socket connections, and substituted /dev/null in their place.  The LocalSocket* objects still need to be closed properly.*/closeSocket();Zygote.setAppProcessName(parsedArgs, TAG);// End of the postFork event.Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);if (parsedArgs.mInvokeWith != null) {WrapperInit.execApplication(parsedArgs.mInvokeWith,parsedArgs.mNiceName, parsedArgs.mTargetSdkVersion,VMRuntime.getCurrentInstructionSet(),pipeFd, parsedArgs.mRemainingArgs);// Should not get here.throw new IllegalStateException("WrapperInit.execApplication unexpectedly returned");} else {if (!isZygote) {return ZygoteInit.zygoteInit(parsedArgs.mTargetSdkVersion,parsedArgs.mDisabledCompatChanges,parsedArgs.mRemainingArgs, null /* classLoader */);} else {return ZygoteInit.childZygoteInit(parsedArgs.mRemainingArgs  /* classLoader */);}}
}

5.2 RuntimeInit.applicationInit()反射调用到ActivityThread.main

  • 此处args.startClass为android.app.ActivityThread
    protected static Runnable applicationInit(int targetSdkVersion, long[] disabledCompatChanges,String[] argv, ClassLoader classLoader) {// If the application calls System.exit(), terminate the process// immediately without running any shutdown hooks.  It is not possible to// shutdown an Android application gracefully.  Among other things, the// Android runtime shutdown hooks close the Binder driver, which can cause// leftover running threads to crash before the process actually exits.nativeSetExitWithoutCleanup(true);VMRuntime.getRuntime().setTargetSdkVersion(targetSdkVersion);VMRuntime.getRuntime().setDisabledCompatChanges(disabledCompatChanges);final Arguments args = new Arguments(argv);// The end of of the RuntimeInit event (see #zygoteInit).Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);// Remaining arguments are passed to the start class's static mainreturn findStaticMain(args.startClass, args.startArgs, classLoader);}
  • 反射调用到ActivityThread.main()
Class<?> cl = Class.forName(className, true, classLoader);
Method m = cl.getMethod("main", new Class[] { String[].class });
mMethod.invoke(null, new Object[] { mArgs });

6. ActivityThread.main

  • 如果是startActivity则将要进入Activity的onCreate/onStart/onResume等生命周期。
  • 如果是startService则将要进入Service的onCreate等生命周期。

frameworks/base/core/java/android/app/ActivityThread.java

    public static void main(String[] args) {Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ActivityThreadMain");// Install selective syscall interceptionAndroidOs.install();// CloseGuard defaults to true and can be quite spammy.  We// disable it here, but selectively enable it later (via// StrictMode) on debug builds, but using DropBox, not logs.CloseGuard.setEnabled(false);Environment.initForCurrentUser();// Make sure TrustedCertificateStore looks in the right place for CA certificatesfinal File configDir = Environment.getUserConfigDirectory(UserHandle.myUserId());TrustedCertificateStore.setDefaultUserDirectory(configDir);// Call per-process mainline module initialization.initializeMainlineModules();Process.setArgV0("<pre-initialized>");Looper.prepareMainLooper();// Find the value for {@link #PROC_START_SEQ_IDENT} if provided on the command line.// It will be in the format "seq=114"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(newLogPrinter(Log.DEBUG, "ActivityThread"));}// End of event ActivityThreadMain.Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);Looper.loop();throw new RuntimeException("Main thread loop unexpectedly exited");}

http://chatgpt.dhexx.cn/article/WeRu6peM.shtml

相关文章

Android-Activity

配置Activity&#xff1a; 如果Activity所在的包与AndroidManifest.xml文件的<manifest></manifest>标签中通过 package属性指定的包名一致&#xff0c;则android:name属性的值可以直接设置为“.Activity名称” <activity android:name"…

ActivityThread的main方法究竟做了什么?

ActivityThread的main方法究竟做了什么&#xff1f; 本文原创&#xff0c;转载请经过本人准许。 写在前面&#xff1a; 在暴雨天能去上课的都是好学生&#xff0c;能去上班的都是游泳运动员~ 问大家一个问题&#xff1a; Android中一个应用程序的真正入口是什么&#xff1f; …

Android Activity.startActivity流程简介

1. 基本概念 1.1 Instrumentation是什么? 顾名思义&#xff0c;仪器仪表&#xff0c;用于在应用程序中进行“测量”和“管理”工作。一个应用程序中只有一个Instrumentation实例对象&#xff0c;且每个Activity都有此对象的引用。Instrumentation将在任何应用程序运行前初始化…

android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context

问题描述 FATAL EXCEPTION: main Process: com.wuchen.juexiao_mvvm, PID: 11732 android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? 原因分析…

Android主线程(ActivityThread)源代码分析

在写这篇博客之前,先抛出一个问题&#xff0c;安卓应用程序的入口是什么呢&#xff1f;我想不少人可能回答说:application的onCreate方法&#xff0c;其实并不是的&#xff0c;即使是application&#xff0c;也有一个方法比onCreate先执行&#xff0c;这个方法就是attachBaseCo…

Android :Activity

Activity Activity 代表手机或平板电脑中的一屏&#xff0c;它提供了和用户交互的可视化界面。 一个 Android 应用中&#xff0c;可以有多个 Activity。这些 Activity 组成了 Activity 栈&#xff08;Stack&#xff09;&#xff0c;当前活动的 Activity 位于栈顶。 Activity …

ActivityThread

ActivityThread运行框架 在分析中&#xff0c;我们可以看到真正对应应用进程的不是Application而是ActivityThread。我们从实际的应用堆栈可以看到&#xff1a; NaiveStart.main() ZygoteInit.main ZygoteInit$MethodAndArgsCall.run Method.Invoke method.invokeNative Activi…

【Android 启动过程】Activity 启动源码分析 ( AMS -> ActivityThread、AMS 线程阶段 )

文章目录 一、Activity 启动源码分析 ( AMS | ActivityManagerService )1、Instrumentation 调用 AMS 方法2、ActivityStarter 调用 AMS 方法3、Process 启动新进程 二、Activity 启动源码分析 ( ActivityStarter )1、ActivityStarter.startActivityMayWait() 方法2、ActivityS…

Android 中你碰不到但是很重要的类之ActivityThread

作者&#xff1a;Drummor 通过本文能了解一下内容 1、和系统进程打交道的桥头堡 应用进程起来之后ART(Android Runtime)第一站就是ActivityThread&#xff0c;代码层面上就是ActivityThread的main()方法&#xff0c;是不是很熟悉&#xff0c;爷青回啊&#xff0c;这不就是java…

浅析ActivityThread#main()方法和生命周期事务处理(代码基于Android-12)

浅析ActivityThread#main()方法和生命周期事务处理&#xff08;代码基于Android-12&#xff09; 问题&#xff1a; Activity、Application的onCreate()在哪被调用的&#xff1f; ContentProvider为什么比Application先创建&#xff1f; Activity#attach()在哪里被调用的&#…

Android中的ActivityThread和ApplicationThread

一&#xff1a;ActivityThread和ApplicationThread 1.ActivityThread&#xff1a;主线程、UI线程&#xff0c;程序的入口&#xff08;main函数&#xff09;&#xff0c;不是线程是运行在主线程中的一个对象 主要的成员变量如下&#xff1a; mActivities、mServices和mProvide…

一文读懂ActivityThread

ActivityThread是什么&#xff0c;是主线程吗&#xff1f;它是如何被创建的&#xff0c;以及它的作用又是什么呢&#xff1f;带着这些问题&#xff0c;我们一起来分析下ActivityThread。 全文分为以下几个部分&#xff0c;大家可根据需求阅读 文章目录 ActivityThread是什么Act…

Android ActivityThread(主线程或UI线程)简介

1. ActivityThread功能 它管理应用进程的主线程的执行(相当于普通Java程序的main入口函数)&#xff0c;并根据AMS的要求&#xff08;通过IApplicationThread接口&#xff0c;AMS为Client、ActivityThread.ApplicationThread为Server&#xff09;负责调度和执行activities、broa…

ActivityThread的理解和APP的启动过程

ActivityThread的理解和APP的启动过程 ActivityThreadActivityThread的初始化主线程Looper的初始化主线程Handler的初始化ApplicationThread及Activity的创建和启动 APP的启动系统的启动过程APP的启动过程APP启动过程的部分代码思考 总结 ActivityThread ActivityThread就是我…

validate中remote返回函数中+号导致submitHandler无法执行

validate中remote返回函数中号导致submitHandler无法执行 这是2017年以来我遇到的最无语的bug&#xff0c;现在暂时还没想到原因&#xff0c;但是这个错误真的很无语。 这是我的validate中rule的定义&#xff0c;其中 new Date; 采用至慕课网上validate插件视频中的例子。 rul…

jQuery(六)插件、Validate验证提交表单、submitHandler、更改错误信息显示的位置、required、Validator、内置验证方式表、validate ()的可选项汇总

jQuery&#xff08;六&#xff09;插件、Validate验证提交表单、submitHandler、更改错误信息显示的位置、required、Validator、内置验证方式表、validate ()的可选项汇总 文章目录 jQuery&#xff08;六&#xff09;插件、Validate验证提交表单、submitHandler、更改错误信息…

ajax post 不起作用,jQuery验证submitHandler在$ .ajax post表单数据中不起作用

我使用$.ajax发送数据并使用jQuery验证插件进行验证&#xff0c;如下所示&#xff1a; Send JS&#xff1a; jQuery(document).ready(function ($) { $(#myform).validate({ rules: { name: { required: true, rangelength: [4, 20], }, }, submitHandler: function (form) { $…

FileReader()用法

FileReader()用法HTML5定义了FileReader作为文件API的重要成员用于读文件&#xff0c;根据W3C的定义&#xff0c;FileReaderr接口提供了读取文件的方法和包含读取 结果的事件模型。 FileReader的方法使用比较简单&#xff0c;可以按照以下步骤创建FileReader对象并调用其他的方…

read/write/fsync与fread/fwrite/fflush的关系和区别

read/write/fsync&#xff1a; 1. linux底层操作&#xff1b; 2. 内核调用&#xff0c; 涉及到进程上下文的切换&#xff0c;即用户态到核心态的转换&#xff0c;这是个比较消耗性能的操作。 fread/fwrite/fflush&#xff1a; 1. c语言标准规定的io流操作&#xff0c;建立…

FileReader详解

我在Google Chrome Web Store上发布了一个案例hahaOCR&#xff0c;该扩展程序可以帮助用户识别出图片中的文字信息&#xff0c;并以文本形式显示&#xff0c;大家可以在chrome网上应用商店中找到我发布的应用程序&#xff0c;如图所示&#xff1a; 图1 - hahaOCR 该扩展程序支持…