Other服务
startOtherServices() 是 SystemServer 中最容易被写成“服务列表”的方法,但 Android 17 的实现更像一个条件驱动的编排器:它读取 feature/property,保存跨服务实例,启动 secondary Zygote preload,并在 PHASE_WAIT_FOR_SENSOR_SERVICE 后建立 WindowManager、InputManager、DisplayManager 的闭环。本文只解释代表性的依赖与状态,不复制数百个服务名。
1. 入口状态
源码文件:frameworks/base/services/java/com/android/server/SystemServer.java
private void startOtherServices(@NonNull TimingsTraceAndSlog t) {
t.traceBegin("startOtherServices");
mSystemServiceManager.updateOtherServicesStartIndex();
final Context context = mSystemContext;
DynamicSystemService dynamicSystem = null;
IStorageManager storageManager = null;
WindowManagerService wm = null;
InputManagerService inputManager = null;
boolean disableNetworkTime = SystemProperties.getBoolean(
"config.disable_networktime", false);
boolean disableCameraService = SystemProperties.getBoolean(
"config.disable_cameraservice", false);
boolean isWatch = RoSystemFeatures.hasFeatureWatch(context);
boolean isTv = RoSystemFeatures.hasFeatureLeanback(context);
boolean isAutomotive = RoSystemFeatures.hasFeatureAutomotive(context);updateOtherServicesStartIndex() 记录 Other 组在 SystemServiceManager 服务数组中的起点,方便后续 boot phase 和诊断区分。局部变量保存跨段传递的服务实例;feature/property 则决定某些服务是否进入启动图。isWatch/isTv/isAutomotive 不是展示信息,它们会直接改变 Vibrator、输入、网络和安全服务分支。
2. Secondary预加载
源码文件:frameworks/base/services/java/com/android/server/SystemServer.java
final String SECONDARY_ZYGOTE_PRELOAD = "SecondaryZygotePreload";
mZygotePreload = SystemServerInitThreadPool.submit(() -> {
try {
TimingsTraceAndSlog traceLog =
TimingsTraceAndSlog.newAsyncLog();
traceLog.traceBegin(SECONDARY_ZYGOTE_PRELOAD);
String[] abis32 = Build.SUPPORTED_32_BIT_ABIS;
if (abis32.length > 0
&& !Process.ZYGOTE_PROCESS.preloadDefault(abis32[0])) {
Slog.e(TAG,
"Unable to preload default resources for secondary");
}
traceLog.traceEnd();
} catch (Exception ex) {
Slog.e(TAG,
"Exception preloading default resources", ex);
}
}, SECONDARY_ZYGOTE_PRELOAD);该任务提前约一秒启动 secondary Zygote 默认资源预加载,以便后面的 WebView 32 位 RELRO fork 不必等待资源初始化。Future mZygotePreload 是状态 owner;任务失败只记录日志,真正需要它的消费者会在后续阶段等待或继续走降级路径。异步 trace 使用 newAsyncLog(),不能和主线程 trace 当成同一时间线。
3. 条件服务
ServiceManager.addService("sec_key_att_app_id_provider",
new KeyAttestationApplicationIdProviderService(context));
mSystemServiceManager.startService(KeyChainSystemService.class);
mSystemServiceManager.startService(BinaryTransparencyService.class);
ServiceManager.addService("scheduling_policy",
new SchedulingPolicyService());
if (com.android.server.ui_latency_stats.Flags
.uiLatencyStatsService()) {
mSystemServiceManager.startService(
com.android.server.uilatencystats.UiLatencyStatsService.class);
}Other 组同时使用 ServiceManager.addService() 和 SystemServiceManager.startService():前者发布跨进程 Binder 名称,后者管理 SystemService 实例和生命周期。UI latency service 由 flag 控制,不能从未启动的 Binder 名称反推设备故障。
4. 内容服务
if (mPackageManager.hasSystemFeature(
PackageManager.FEATURE_MICROPHONE)
|| mPackageManager.hasSystemFeature(
PackageManager.FEATURE_TELECOM)
|| mPackageManager.hasSystemFeature(
PackageManager.FEATURE_TELEPHONY)) {
mSystemServiceManager.startService(
TelecomLoaderService.class);
}
TelephonyRegistry telephonyRegistry =
new TelephonyRegistry(context,
new TelephonyRegistry.ConfigurationProvider());
ServiceManager.addService(
"telephony.registry", telephonyRegistry);
mSystemServiceManager.startService(
AccountManagerService.Lifecycle.class);
mSystemServiceManager.startService(
ContentService.Lifecycle.class);
mActivityManagerService.getContentProviderHelper()
.installSystemProviders();TelecomLoader 只有在设备具备麦克风、telecom 或 telephony feature 时启动;TelephonyRegistry 直接发布 Binder。AccountManager 必须先于 ContentService,系统 provider 安装又必须在 ContentService 之后进行,随后 SettingsProvider 就绪,SQLite compatibility WAL flags 才重新激活。这是一条“服务构造→provider 消费→全局配置恢复”的依赖链。
5. UI与存储前置
if (mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
mSystemServiceManager.startService(
StorageManagerService.Lifecycle.class);
storageManager = IStorageManager.Stub.asInterface(
ServiceManager.getService("mount"));
mSystemServiceManager.startService(
StorageStatsService.Lifecycle.class);
}
mSystemServiceManager.startService(
UiModeManagerService.class);
mSystemServiceManager.startService(
LocaleManagerService.class);
mSystemServiceManager.startService(
AppHibernationService.class);低级工厂模式跳过 StorageManager;正常模式先启动 StorageManager,再获取其 Binder 代理供后续通知/媒体服务使用。UiMode、Locale、AppHibernation 等服务不都拥有独立 Binder 调用链,但都依赖已建立的 SystemContext 和 PackageManager 状态。
6. WMS与Input
源码文件:frameworks/base/services/java/com/android/server/SystemServer.java
mSystemServiceManager.startBootPhase(
t, SystemService.PHASE_WAIT_FOR_SENSOR_SERVICE);
wm = WindowManagerService.main(
context, inputManager, !mFirstBoot,
new PhoneWindowManager(),
mActivityManagerService.mActivityTaskManager);
ServiceManager.addService(
Context.WINDOW_SERVICE, wm, false,
DUMP_FLAG_PRIORITY_CRITICAL
| DUMP_FLAG_PRIORITY_HIGH
| DUMP_FLAG_PROTO);
mActivityManagerService.setWindowManager(wm);
wm.onInitReady();
inputManager.setWindowManagerCallbacks(
wm.getInputManagerCallback());
inputManager.start();
mDisplayManagerService.windowManagerAndInputReady();WMS 启动前先推进到 PHASE_WAIT_FOR_SENSOR_SERVICE,因为 WMS 需要 SensorService;WMS 创建后发布 Binder,再回填 AMS。InputManager 只有在拿到 WMS callback 后才启动,DisplayManager 又要等 WindowManager 和 Input 都 ready。这三个服务形成双向依赖闭环,不能按“类名顺序”拆开理解。
7. 网络与设备条件
if (mFactoryTestMode == FactoryTest.FACTORY_TEST_LOW_LEVEL) {
Slog.i(TAG, "No Bluetooth Service (factory test)");
} else if (!context.getPackageManager().hasSystemFeature(
PackageManager.FEATURE_BLUETOOTH)) {
Slog.i(TAG,
"No Bluetooth Service (Bluetooth Hardware Not Present)");
} else {
mSystemServiceManager.startServiceFromJar(
BLUETOOTH_SERVICE_CLASS,
BLUETOOTH_APEX_SERVICE_JAR_PATH);
}Bluetooth 的启动同时受工厂测试模式和硬件 feature 控制;NetworkTime、Camera、SystemTextClassifier 等也由 property 控制。读取条件后再看服务 trace,才能区分“跳过”和“启动失败”。
8. 失败与恢复
Other 主体用一个 try/catch 包围大量服务启动,异常会记录 Failure starting core service 并重新抛出,最终回到 SystemServer.run()。部分 UI/Storage/Locale 服务则单独 reportWtf(),允许 SystemServer 继续启动并把局部故障交给默认 WTF handler。两种策略不同:前者是编排主线失败,后者是可降级服务失败。
# 输入 Other 入口,输出 feature/property、并行任务和主要服务阶段。
rg -n "startOtherServices|updateOtherServicesStartIndex|FEATURE_|disableNetworkTime|disableCameraService|SecondaryZygotePreload" \
frameworks/base/services/java/com/android/server/SystemServer.java
# 输入 WMS/Input/Display 关键调用,输出阶段依赖和 ready 闭环。
rg -n "PHASE_WAIT_FOR_SENSOR_SERVICE|WindowManagerService\.main|setWindowManagerCallbacks|inputManager\.start|windowManagerAndInputReady" \
frameworks/base/services/java/com/android/server/SystemServer.java下一篇将展开 startApexServices() 和服务启动封口;不会重复本文的 Other 条件分支、WMS/Input 闭环和 secondary preload。
