Beginner Android framework series four (those processes in the system)

Open android studio, you can see that there is a process drop-down menu in the logcat toolbar, after opening, you can see all the processes of the current Android device

Screenshot 2023-02-20 at 9.40.04 PM.png

In addition to the installed third-party apps, there will be many processes that come with the system. The most important thing is the process where the system server is located. It looks like this

Screenshot 2023-02-20 at 9.44.49 PM.png

Today I plan to use a chapter to talk a little about the process in Android, including:

  1. The location of the system process in Android
  2. Android system server starts the phase of the system process
  3. shared process

Where is the code for each process?

The most important system process of Android is of course system_process. This process is the first process hatched by zygote, which manages the implementation of various large and small system services (SystemService) in the Android system. That is, the server-side implementation of various managers in the figure below (Android C/S architecture).

android_layers-1.jpg

In addition, many other system processes are in this position:

platform/packages/

Those starting with apps must be independent 1st party apps. Some that start with services are independent processes.

For example, the Telephony process, its name corresponds to the phone process in Android (see its manifest file)

Screenshot 2023-02-20 at 10.02.08 PM.png

You can also see this process by opening logcat

Screenshot 2023-02-20 at 10.02.36 PM.png

Some share processes with other packages, such as Telecom.

Screenshot 2023-02-20 at 10.05.54 PM.png

You can see that this process shares user id with another process called system, and they will eventually enter the same process. And this system process is the famous system_process....

android.googlesource.com/platform/fr…

Screenshot 2023-02-20 at 10.10.36 PM.png

That is to say, although the two packages Telephony and Telecom are independent packages (in terms of code distribution, they run in different processes at runtime, and one of the two packages has its own independent process, and the other will eventually Run in the system service process.

Screenshot 2023-02-20 at 10.13.04 PM.png

打开logcat,filter到system_process进程的log,你可以搜到Telecom的log。证明Telecom这个package的确是跑在系统进程上了。

系统进程的启动

system_process作为第一个孵化出来的java service进程,除了要负责启动在system_process里面的系统服务之外

比如:

android.googlesource.com/platform/fr…

Screenshot 2023-02-21 at 9.30.17 PM.png

PS:android开发团队自己也觉得这样hardcode service name不太好, 想通过读取系统构建的某些文件来做。。。但是一直没改 :( 。

还需要启动的其他的系统进程。

当系统进程的关键性服务都启动好了之后,ActivityManagerService就会开始启动被标记为direct boot aware 的进程,中文叫直接启动模式

当设备已开机但用户尚未解锁设备时,Android 7.0 将在安全的“直接启动”模式下运行

Screenshot 2023-02-21 at 9.48.52 PM.png

要让当前进程(package )成为直接启动模式的进程, 只需要像上面截图一样把directBootAware 标记为true就行了。

ActivityManagerService在系统进程启动完毕所有的必要service之后,就开始启动direct boot aware的package了。

android.googlesource.com/platform/fr…

Screenshot 2023-02-21 at 9.52.04 PM.png

比如上文介绍到的Telephony package就是被标记成一个direct boot aware的app。

android.googlesource.com/platform/pa…

这里有个文档的坑,要想一个app成为直接启动模式的app(也就是Application class oncreate会被调用)的话还需要多加一个persistent=true的标记。但是文档里面没有详细写。。。。

Screenshot 2023-02-21 at 9.58.42 PM.png

这个package的application class会在系统服务启动结束之后马上被创建,onCreate会被调用

Screenshot 2023-02-21 at 9.59.10 PM.png

所以在安卓启动的过程中,有三个重要的阶段

  1. 启动系统进程服务阶段, critical service phase
  2. 启动完step 1,开始启动其他重要的进程,但是在用户还没解锁屏幕之前,这个阶段叫direct boot phase. 这个阶段启动的进程都是用户在没有解锁屏幕之前能做的,比如说打紧急电话.(所以Telephony进程必须在direct boot 阶段启动)
  3. 用户解锁结束,这个阶段叫boot complete phase

共享进程的package

上面介绍过某些package可以共享进程。比如Telecom的代码明明有自己独立的manifest,还有自己独立的package name

package="com.android.server.telecom"
复制代码

但是最后其实代码还是跑在system_process里面。

原因是在管理所有系统进程的类ProcessList里面,当要启动的package是系统进程的时候,安卓会在已经launch的进程中选择第一个作为系统进程的那个process。

Screenshot 2023-02-22 at 9.08.12 PM.png

所以简单来总结一下,安卓系统在启动过程中做了如下几件事情

Screenshot 2023-02-22 at 9.23.25 PM.png

安卓framework中的面向对象

最后提一嘴,安卓framework代码中的面向对象设计做得很好。基本上所有的现实中的实例都有在代码中一一对应。

比如设备中每一个package在代码中抽象成PackageInfo.java, 这个类中所有的field都一一对应着一个安卓package manifest 文件中的tag。比如android:sharedUserId,就是一个PackageInfo.java中 的 public String sharedUserId;

再比如一个进程,在安卓的framework中的java代码也就叫Process.java ,启动进程,杀死进程等等操作都被抽象成Process.java中的API (比如启动当前进程 -> 对应Process#start()方法 )

These abstractions bring great convenience to developers or source code readers. For example, if you want to find out how a certain manifest tag is used, you can open PackageInfo.javathe class, search for the field corresponding to the tag in the class, and perform a global search in the code.

Guess you like

Origin juejin.im/post/7203180600819744827