As known, android is based on Linux kernel however in this article, we wont deal with kernel. We will try to discover android initialization (particularly zygote) step by step after the kernel is done.
- init, source can be found in /system/core/init/init.c, is started first after the kernel finished his job.(init’s pid is 1)
- init process parces the init.rc file which has a specific format called the Android Init Language and sets up the system according to init.rc. There is a readme file that gives information about Android Init Language in the /system/core/init folder. Services are started and system properties are set according to init.rc file. Also zygote, java and native services started.
- This is the line about zygote in init.rc file
service zygote /system/bin/app_process -Xzygote /system/bin –zygote –start-system-server
- “app_process” source code can be found in “frameworks/cmds/app_process/app_main.cpp”. ”app_process” calls the static main function in ZygoteInit.java class(frameworks/core/java/com/android/internal/os/)
runtime.start(“com.android.internal.os.ZygoteInit”, startSystemServer);
- ZygoteInit.java’s static main function starts the system server by calling “startSystemServer()” function if proper parameters given. Then “startSystemServer()” calls the static main function of SystemServer.java class(frameworks/base/services/java/com/android/server).
- SystemServer.java’s static main function firstly call native “init1()” function. “init1()” is defined in “android_servers” library function and “init1()” is registered in “frameworks/base/services/jni/com_android_server_SystemServer.cpp” to call “system_init()”
- “system_init()” is defined in “/frameworks/base/cmds/system_server/library/system_init.cpp”. In “system_init()” function, native services(SurfaceFlinger, AudioFlinger, Camera, MediaPlayer), Androidruntime is started and “init2()” function in SystemServer.java is called.
- “init2()” function creates a thread that starts all critical services(PowerManager, ActivityManager, Battery, Vibrator,Connectivity, etc.). Lastly “ActivityManagerService”’s “systemReady()” function is called to run third party code.