Android Boot insights
As we all know Android system is built upon linux system with some additional features like wake locks, battery management and some other.
When we think about Android Boot, what comes in out mind is,
Today, I am going to arrange all this information in a sequence and will give insights for some of these topics.
So, Lets start…
Init, is first process started by Android kernel when we boots up device. As its first process, it has processId as 1.
INIT checks for some configuration files present at root directory, such as init.rc and init.XXXX.rc, where XXXX varies for each android device and its manufacturer.
This rc files contains information about
- mounting file systems
- creating directories (if required)
- setting up permissions and owners for directories and mounted files.
INIT further does 3 important things which plays vital role in loading android system.
- Start Native Daemons
- Start Runtime
- Start Zygote
Native Daemons:
INIT starts many native daemons such as,
a. installd: responsible for app installations, creating or removing directories on android file system.
b. rild: Radio interface layer daemon
c. adbd: Android device bridge daemon, responsible to perform work assigned by adb server module.
and some other daemons such as netd, logd, storaged etc.
Start Runtime:
Its not Dalvik/ART, its an instance of AppRuntime class.
It’s main job is to start ServiceManager and acts as an contextManager for binder transactions which are extremely important to do all low level communications in android system.
Start Zygote:
As we know, Zygote is base of all Java/Kotlin related classes, whenever we install any application or start any application then zygote creates its child process with which our application runs.
INIT executes app_process command to start zygote base process which will further creates its child processes. The base process works in following way,
- It registers socket for listening further requests
- It preloads all classes and resources which are common to be used for android system to run. These sets of classes and resources will be shared with all further applications. This will help to maintain a single copy of resources and saves a lot of memory for android system. If a particular app require to modify any of these classes then copy of that particular resource will be created and further modified by application and saved separately.
- Forks systemServer which further creates services required by different applications to communicate with sensors and other resources
- And waits in a loop to listen for any request to create child process.
SystemServer
SystemServer plays vital role by starting services which are very important to load Android system and useful for applications as well.
As I had mentioned earlier, systemServer creates many services and registers it to ServiceManager(created by INIT). Whenever we need any service to be accessed, then we request serviceManager to get it.
Let’s find out how systemServer does its job,
- At first it loads libraries and resources required to communicate with sensors or other hardware components which are present on android device.
- It sets up context to use these libraries and fetch data from sensors
- Starts important bootstrap services such as ActivityManagerService and PackageManagerService.
- Starts WindowManagerService which is responsible for drawing application’s windows, menus. It also responsible for activity transitions as well.
- Starts other remaining services such as AlarmManagerService, Wifi or network related services, bluetooth services and many others.
- Loads system UI including navigationBar, system bar, wallpaper and other stuff
- Then it reports ActivityManagerService that UI has been loaded successfully, which then broadcasts BOOT_COMPLETED event which can be subscribed by applications to do stuff when system completes its boot.
ActivityManagerService:
ActivityManagerService is mainly responsible for maintaining lifecycle of activities and taking decisions while starting, closing and pausing activities.
It manages actvityRecords in its own tasks. For different app processes in application, different tasks will be maintained in ActivityManagerService which has single or multiple activityRecords.
PackageManagerService:
PackageManagerService plays vital role in application installation and uninstallation.
When we try installing an application, PackageManagerService takes instance of installd to create directories on file system (Since packageManagerService don’t have root access, it cannot do it by itself.)
It further scans apk, checks for assets, resources, manifests, permissions and then install it on device with required permissions.
These all important things happens in background after starting the device before loading UI screen.