自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
Zygote 进程简介
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Zygote 是 Android 操作系统中的一种进程,可充当具有相同应用二进制接口 (ABI) 的所有系统和应用进程的根。Zygote
在现代设备(例如 Pixel 7 及更新型号)上,会有一个 64 位 Zygote 进程。此外,还有适用于主要 ABI 的 WebView Zygote,它是一种专门的 Zygote,包含运行 WebView 的进程专用的库和资源。
Zygote 执行的任务如下:
当 Android OS 初始化时,init 守护程序会生成 Zygote 进程。在某些双架构系统中,会生成两个 Zygote 进程(一个 64 位,一个 32 位)。本页仅介绍单一架构系统的情况。
Zygote 可以立即生成称为非专用应用进程 (USAP) 的进程,也可以等到应用需要时再生成进程。前一种选项必须通过系统属性或 Android 调试桥命令启用。如需详细了解如何将 Zygote 配置为立即生成进程,请参阅启用非专用应用进程池。
启用 USAP 池
如需启用 USAP 池,请执行以下操作之一:
此功能启用后,每个 Zygote 都会维护一个分支进程池,用于执行应用启动进程中与应用无关的部分。
排查 Zygote 问题
本部分介绍了 Zygote 相关问题的解决方案。
Zygote 崩溃了
如果您的设备无法正常重新启动,并且日志或崩溃报告显示 Zygote 存在问题,有可能是因为您最近做出了导致 initd 或系统服务器崩溃的更改。修正代码应该可以解决此问题。
SELinux 拒绝或 IO 失败
Zygote 非常注重跨进程边界的文件描述符卫生情况。如果复刻时有文件描述符,但许可名单中却没有,我们会使用 dup
系统调用来执行 /dev/null
,以防止意外使用缓存的文件描述符来访问新打开的文件。
如果您要做出框架更改,更改包括尝试将资源加载到 Zygote 中,但收到 SELinux 拒绝或 IO 失败错误,请采取以下办法:
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-03-26。
[null,null,["最后更新时间 (UTC):2025-03-26。"],[],[],null,["# About the Zygote processes\n\nThe *Zygote* is a process in the Android operating system that acts as the root\nof all system and app processes with the same application binary\ninterface (ABI).\n\nOn modern devices, such as Pixel 7 and later, there is a 64-bit Zygote process.\nIn addition, there is the *WebView Zygote* for the primary ABI, which is a\nspecialized Zygote\nthat contains libraries and resources specific to processes that run WebViews.\n\nHere are the tasks the Zygote performs:\n\n1. The init daemon spawns the Zygote process when the Android OS is\n initialized. On some dual architecture systems, two Zygote processes (a 64-bit\n and 32-bit) are spawned. This page covers only single architecture systems.\n\n2. The Zygote can immediately spawn processes called *unspecialized app\n processes (USAP)* or wait to spawn processes as needed by applications.\n The former option must be enabled through a system property or Android\n debug bridge command. For more information on configuring the Zygote to spawn\n processes immediately, see\n [Enable unspecialized app processes pool](#enable).\n\n - If the USAP pool is enabled on your device:\n\n 1. The system server uses a Unix domain socket to connect to an available USAP from a pool. The system server requests that the USAP is preconfigured for application use by changing the process's ID (PID), cgroup, and other information.\n 2. When the USAP is done preconfiguring, it replies to the system server with the PID.\n 3. When an application occupies one of these USAPs, the USAP is no longer part of the pool. When the pool reaches one or fewer USAPs in the pool, the Zygote replenishes the pool with new USAPs.\n - If your Zygote spawns processes using\n [lazy evaluation](https://en.wikipedia.org/wiki/Lazy_evaluation):\n\n 1. The system server receives a command that an app needs a process.\n 2. The system server uses a Unix domain socket to send a command to the appropriate Zygote.\n 3. The Zygote forks the process and changes the PID, cgroup, and other information.\n 4. When the process is complete, it sends the PID back to the Zygote which then passes it back to the system server.\n\nEnable USAP pool\n----------------\n\nTo enable the use of USAP pool, do one of the following:\n\n- Set the `dalvik.vm.usap_pool_enabled` system property to `true` in `/build/make/target/product/runtime_libart.mk`.\n\n- Run the following command:\n\n adb shell am broadcast -a \\\"com.google.android.gms.phenotype.FLAG_OVERRIDE\\\" --es package \\\"com.google.android.platform.runtime_native\\\" --es user \\\"\\*\\\" --esa flags \\\"usap_pool_enabled\\\" --esa values \\\"true\\\" --esa types \\\"string\\\" com.google.android.gms\n\nWhen this feature is enabled, each Zygote maintains a pool of forked processes\nthat perform the application-independent portions of the application startup\nprocess.\n\nTroubleshoot Zygote issues\n--------------------------\n\nThis section contains solutions to Zygote-related issues.\n\n### The Zygote is crashing\n\nIf your device doesn't reboot properly and your logs or crash reports show\nissues with the Zygote, it's likely because you made a recent change that\ncauses initd or the system server to crash. Fixing your code should fix\nthe problem.\n\n### SELinux denials or IO Failures\n\nThe Zygote is particular about file descriptor hygiene across process\nboundaries. When file descriptors are present at fork time but not in an\nallowlist, we use a `dup` system call to `/dev/null` to prevent cached file\ndescriptors from being used unintentionally to access newly opened files.\n\nIf you're making framework changes that include trying to load\nresources into the Zygote, and you're receiving SELinux denials or IO failures:\n\n- For unnamed file descriptors, include the file descriptors in the `fds_to_ignore` vector when `Restat` is called.\n\n- For named file descriptors:\n\n 1. Edit \u003cvar translate=\"no\"\u003eWORKING_DIRECTORY\u003c/var\u003e`/frameworks/base/core/jni/fd_utils.cpp`.\n 2. Add the path to the allowlist for open files."]]