文章大纲

  • 引言
  • 一、Intsaller系统服务概述
  • 二、com.android.server.SystemService概述
  • 三、Intsaller系统服务的启动
    • 1、com.android.server.SystemServer#startBootstrapServices 触发Installer系统服务启动
    • 2、com.android.server.SystemServiceManager#startService反射创建并回调com.android.server.pm.Installer#onStart
      • 2.1、com.android.server.SystemServiceManager#startService(String className)获取Installer的字节码对象
      • 2.2、com.android.server.SystemServiceManager#startService(java.lang.Class< T >)反射创建Installer对象
      • 2.3、com.android.server.SystemServiceManager#startService(SystemService) 注册到SystemServerManager并触发Installer#onStart回调
    • 3、com.android.server.pm.Installer#onStart 获取installd 的Binder服务代理对象
      • 3.1、com.android.server.pm.Installer#onStart 触发获取installd 的Binder服务代理对象
      • 3.2、com.android.server.pm.Installer#connect 真正获取installd 的Binder服务代理对象

引言

SystemServer进程在执行startBootstrapServices方法后,首先就通过SystemServiceManager创建和启动Installer系统服务。

以后更多精选系列文章以高度完整的系列形式发布在公众号,真正的形成一套完整的技术栈,欢迎关注,目前第一个系列是关于Android系统启动系列的文章,大概有二十几篇干货。

  • Android 进阶——系统启动之BootLoader 简介及内核启动(一)
  • Android 进阶——系统启动之Linux init进程的创建和启动(二)
  • Android 进阶——系统启动之Android init进程的创建和启动(三)
  • Android 进阶——系统启动之Android init.rc脚本解析(四)
  • Android 进阶——系统启动之Android init进程解析init.rc脚本(五)
  • Android 进阶——系统启动之Android进程造物者Zygote 进程启动详解(六)
  • Android 进阶——系统启动之核心SystemServer进程启动详解(七)
  • Android 进阶——系统启动之SystemServer创建并启动Installer服务(八)
  • Android 进阶——系统启动之SystemServer进程创建并启动Watchdog 监听系统服务详解(九)
  • Android 进阶——系统启动之Framework 核心ActivitityManagerService服务启动(十)

一、Intsaller系统服务概述

\frameworks\base\services\core\java\com\android\server\pm\Installer.java继承自com.android.server.SystemService,同时持有installd守护进程对应Binder服务的代理对象,本质上就是通过Binder调用去与Linux底层installd守护进程通信完成真正的完成Apk文件格式的优化和转换建立相关的数据目录删除文件安装应用等工作。因此在其他系统核心服务启动前首先被启动,

    private void startBootstrapServices() {...Installer installer = mSystemServiceManager.startService(Installer.class);

SystemServiceManager.startService(Installer.class)触发com.android.server.pm.Installer#onStart回调时,首先通过ServerManager获取名为installdBinder服务代理对象,然后调用这个Binder服务代理对象通过socket与Linux的installd 守护进程通信。

package com.android.server.pm;import com.android.internal.os.BackgroundThread;
import com.android.server.SystemService;
public class Installer extends SystemService {private volatile IInstalld mInstalld;public Installer(Context context, boolean isolated) {super(context);mIsolated = isolated;}@Overridepublic void onStart() {if (mIsolated) {mInstalld = null;} else {connect();}}private void connect() {IBinder binder = ServiceManager.getService("installd");if (binder != null) {try {binder.linkToDeath(new DeathRecipient() {@Overridepublic void binderDied() {Slog.w(TAG, "installd died; reconnecting");connect();}}, 0);}}if (binder != null) {mInstalld = IInstalld.Stub.asInterface(binder);try {invalidateMounts();}} else {Slog.w(TAG, "installd not found; trying again");BackgroundThread.getHandler().postDelayed(() -> {connect();}, DateUtils.SECOND_IN_MILLIS);}}public void dexopt(String apkPath, int uid, @Nullable String pkgName, String instructionSet,int dexoptNeeded, @Nullable String outputPath, int dexFlags,String compilerFilter, @Nullable String volumeUuid, @Nullable String sharedLibraries,@Nullable String seInfo, boolean downgrade, int targetSdkVersion,@Nullable String profileName, @Nullable String dexMetadataPath,@Nullable String compilationReason) throws InstallerException {if (!checkBeforeRemote()) return;try {mInstalld.dexopt(apkPath, uid, pkgName, instructionSet, dexoptNeeded, outputPath,dexFlags, compilerFilter, volumeUuid, sharedLibraries, seInfo, downgrade,targetSdkVersion, profileName, dexMetadataPath, compilationReason);}}public void rmdex(String codePath, String instructionSet) throws InstallerException {try {mInstalld.rmdex(codePath, instructionSet);}}public void createUserData(String uuid, int userId, int userSerial, int flags)throws InstallerException {if (!checkBeforeRemote()) return;try {mInstalld.createUserData(uuid, userId, userSerial, flags);}}...public void linkNativeLibraryDirectory(String uuid, String packageName, String nativeLibPath32,int userId) throws InstallerException {if (!checkBeforeRemote()) return;try {mInstalld.linkNativeLibraryDirectory(uuid, packageName, nativeLibPath32, userId);}}public void createOatDir(String oatDir, String dexInstructionSet)throws InstallerException {if (!checkBeforeRemote()) return;try {mInstalld.createOatDir(oatDir, dexInstructionSet);}}public void linkFile(String relativePath, String fromBase, String toBase)throws InstallerException {if (!checkBeforeRemote()) return;try {mInstalld.linkFile(relativePath, fromBase, toBase);}}public void deleteOdex(String apkPath, String instructionSet, String outputPath)throws InstallerException {mInstalld.deleteOdex(apkPath, instructionSet, outputPath);}public void installApkVerity(String filePath, FileDescriptor verityInput, int contentSize)throws InstallerException {if (!checkBeforeRemote()) return;mInstalld.installApkVerity(filePath, verityInput, contentSize);}public boolean reconcileSecondaryDexFile(String apkPath, String packageName, int uid,String[] isas, @Nullable String volumeUuid, int flags) throws InstallerException {return mInstalld.reconcileSecondaryDexFile(apkPath, packageName, uid, isas,volumeUuid, flags);}public void invalidateMounts() throws InstallerException {if (!checkBeforeRemote()) return;mInstalld.invalidateMounts();}...
}

二、com.android.server.SystemService概述

\frameworks\base\services\core\java\com\android\server\SystemService.java本质上就是对于一些运行在系统进程服务进行了抽象,定义了一些统一的生命周期方法和运行规则(所有方法都必须在系统服务进程的main looper thread中被调用)。

package com.android.server;import android.os.ServiceManager;
/*** The base class for services running in the system process. Override and implement* the lifecycle event callback methods as needed.* {@hide}*/
public abstract class SystemService {...private final Context mContext;public SystemService(Context context) {mContext = context;}public abstract void onStart();...protected final void publishBinderService(String name, IBinder service) {publishBinderService(name, service, false);}protected final void publishBinderService(String name, IBinder service,boolean allowIsolated) {ServiceManager.addService(name, service, allowIsolated);}protected final IBinder getBinderService(String name) {return ServiceManager.getService(name);}protected final <T> void publishLocalService(Class<T> type, T service) {LocalServices.addService(type, service);}protected final <T> T getLocalService(Class<T> type) {return LocalServices.getService(type);}private SystemServiceManager getManager() {return LocalServices.getService(SystemServiceManager.class);}
}

三、Intsaller系统服务的启动

1、com.android.server.SystemServer#startBootstrapServices 触发Installer系统服务启动

    private void startBootstrapServices() {Installer installer = mSystemServiceManager.startService(Installer.class);

2、com.android.server.SystemServiceManager#startService反射创建并回调com.android.server.pm.Installer#onStart

\frameworks\base\services\core\java\com\android\server\SystemServiceManager.java

2.1、com.android.server.SystemServiceManager#startService(String className)获取Installer的字节码对象

    public SystemService startService(String className) {final Class<SystemService> serviceClass;serviceClass = (Class<SystemService>)Class.forName(className);return startService(serviceClass);}

2.2、com.android.server.SystemServiceManager#startService(java.lang.Class< T >)反射创建Installer对象

 /*** Creates and starts a system service. The class must be a subclass of* {@link com.android.server.SystemService}.*/public <T extends SystemService> T startService(Class<T> serviceClass) {try {final String name = serviceClass.getName();// Create the service.final T service;try {Constructor<T> constructor = serviceClass.getConstructor(Context.class);service = constructor.newInstance(mContext);} startService(service);return service;}}

2.3、com.android.server.SystemServiceManager#startService(SystemService) 注册到SystemServerManager并触发Installer#onStart回调

 public void startService(@NonNull final SystemService service) {// @link ArrayList<SystemService> mServices 注册到SystemServerManager中mServices.add(service);long time = SystemClock.elapsedRealtime();try {service.onStart();}warnIfTooLong(SystemClock.elapsedRealtime() - time, service, "onStart");}

3、com.android.server.pm.Installer#onStart 获取installd 的Binder服务代理对象

3.1、com.android.server.pm.Installer#onStart 触发获取installd 的Binder服务代理对象

    @Overridepublic void onStart() {if (mIsolated) {mInstalld = null;} else {connect();}}

3.2、com.android.server.pm.Installer#connect 真正获取installd 的Binder服务代理对象

    private void connect() {//首先通过ServiceManager 获取installd服务的Binder对象IBinder binder = ServiceManager.getService("installd");if (binder != null) {try {binder.linkToDeath(new DeathRecipient() {@Overridepublic void binderDied() {Slog.w(TAG, "installd died; reconnecting");connect();}}, 0);} catch (RemoteException e) {binder = null;}}if (binder != null) {//把Binder对象转为具体的Binder服务对象mInstalld = IInstalld.Stub.asInterface(binder);try {invalidateMounts();} catch (InstallerException ignored) {}} else {Slog.w(TAG, "installd not found; trying again");BackgroundThread.getHandler().postDelayed(() -> {connect();}, DateUtils.SECOND_IN_MILLIS);}}

至此Installer系统服务启动完毕,接着真正开始其他系统服务的启动,敬请参见下集。

Android 进阶——系统启动之SystemServer创建并启动Installer服务(八)相关推荐

  1. Android 进阶——系统启动之Android进程造物者Zygote 进程启动详解(六)

    引言 前面系列文章介绍了Android系统的第一个用户进程--init进程由解析init.rc脚本启动,完成属性系统的初始化等工作后紧接着启动Android系统上的造物者--Zygote进程,这篇文章 ...

  2. 【Android】系统启动流程(zygote 进程启动流程)

    前言 先上图,大致了解一下 Android 设备点击电源键开机到创建出 system_server 进程的流程, 里面细化的子流程和 system_server 之后发生的事情我将会在后续的文章中详细 ...

  3. Android 9.0 在init.rc中启动一个服务

    现在有一个blink .bin文件,需要拷贝到/system/bin/目录下面去,然后再init.rc文件中启动该服务 一.init.rc文件中启动服务 1.在init.rc文件中启动服务 代码路径: ...

  4. Android进阶——Small源码分析之启动流程详解

    前言 插件化现在已经是Android工程师必备的技能之一,只是学会怎么使用是不行的,所以蹭有时间研究一下Small的源码.对于插件化主要解决的问题是四大组件的加载和资源的加载,读懂所有Small源码需 ...

  5. android 8.0以后(sdk26)启动前台服务的问题探究

    https://blog.csdn.net/sinat_20059415/article/details/80584487 转载于:https://www.cnblogs.com/antble/p/1 ...

  6. Android 10.0 系统启动之SystemServer进程-[Android取经之路]

    摘要:上一节讲解了Zygote进程的整个启动流程.Zygote是所有应用的鼻祖.SystemServer和其他所有Dalivik虚拟机进程都是由Zygote fork而来.Zygote fork的第一 ...

  7. Android开发——Android系统启动以及APK安装、启动过程

    0. 前言   从Android手机打开开关,到我们可以使用其中的app时,这个启动过程到底是怎么样的? 1.  系统上电 当给Android系统上电,在电源接通的瞬间,CPU内的寄存器和各引脚均会被 ...

  8. android system_server中的dump_Android 10.0系统启动之SystemServer进程(二)

    感谢您的阅读与点赞!欢迎点击右上角关注:「大猫玩程序」 微信公众号:大猫玩程序 上一节讲解了SystemServer的架构以及被Zygote 进程fork的流程,这一节重点讲解SystemServer ...

  9. Android 9 (P)系统启动之SystemServer大揭秘下

          Android 9 (P)系统启动之SystemServer大揭秘下 Android 9 (P)系统启动及进程创建源码分析目录: Android 9 (P)之init进程启动源码分析指南之 ...

最新文章

  1. clion 远程连接linux服务器 开发调试
  2. MySQL高级 - 存储引擎 - 特性
  3. 让前端与后端异步起来
  4. 怎么用python自制计算公式_如何使用Python和Numpy计算r平方?
  5. Python基础知识__字符串
  6. 学习布局——getContentPane() setContentPane()
  7. docker快速搭建数据库测试环境
  8. 引用父类成员的关键字是java_Java 中对父类成员访问用的关键字是 ,而引用当前对象的关键字是 。_学小易找答案...
  9. 怎么在堆叠柱状图中体现百分比_微生物门类堆叠柱状图一文解决
  10. java sap_SAP系统安装之JAVA
  11. 2023年华中科技大学金融专硕考研参考书、难度分析及备考经验
  12. ethtool如何让接口闪灯_如何使用ethtool命令管理以太网卡
  13. 获取ua(user Agent)
  14. 集成 Jenkins 和 TestNG 实现自助式自动化测试平台
  15. java 算出下一个工作日_Java 计算一段时间段内除去周六日、节假日的工作日数———超详细(全)...
  16. anaconda安装dlib出现ImportError: libopenblas.so.0: cannot open shared object file***
  17. while(1);的作用
  18. 国际经济与贸易专业与计算机联系,经济与贸易专业(国际经济与贸易方向).docx...
  19. 用python实现一个【简易记事本】吧
  20. matlab非线性整数优化,fmincon整数优化

热门文章

  1. android jni 人头检测_英雄联盟举报系统真的有用吗?
  2. Josephus约瑟环问题
  3. MongoDB4.4 新特性
  4. 软件测试行业女生多么,软件测试行业为何女生越来越多?
  5. 应该没有人不知道吧?排名前三的python 开源 IDE
  6. 如何在HTML网页引入JavaScript
  7. 源码时代UI干货 | 只用5步教会你制作《这就是街舞》风格的字体设计
  8. 如何评价python知乎_如何评价《Python Web开发实战》?
  9. Johnson 全源最短路径算法 Java实现
  10. 最美手机开售 魅族魅蓝X曜石黑1699元