The Android startup process starts with INIT, which is the ancestor process for all subsequent processes. The system startup process can be broadly divided into the following steps
1,init.c的启动 挂载目录 初始化 解析配置文件2,init.rc3,在init.rc中app_main中启动了zygote(孵化器),AndroidRuntime Zygote这个进程是非常重要的一个进程,Zygote进程的建立是真正的Android运行空间,初始化建立的Service都是Navtive service4,在zygote java类中开启各种服务,并且开启systemServer类5,在systemServer中main--->init1(system_init)--->init2(systemThread)6,home界面显示, 这时Android系统启动完毕. 进入到待机画面
When the
system boot program launches the Linux kernel, the kernel loads various data structures and drivers. With the driver, start the Android system and load the first process init (SYSTEM/CORE/INIT/INIT.C) at the user level.
int main(int argc, char **argv) { ... // 创建各种文件夹和挂载目录. mkdir("/dev", 0755); ... // 初始化日志. log_init(); // 解析配置文件. init_parse_config_file("/init.rc"); ... return 0; }
load the init.rc file. A zygote (incubator) process is launched, which is a parent process for Android to start a critical service.
service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server socket zygote stream 666 onrestart write /sys/android_power/request_state wake onrestart write /sys/power/state on onrestart restart media onrestart restart netd
The initialization of the
zygote process is opened in the App_main.cpp file with the following code snippet:
int main(int argc, const char* const argv[]){ // 定义Android运行时环境. AppRuntime runtime; int i = runtime.addVmArguments(argc, argv); ... bool startSystemServer = (i < argc) ? strcmp(argv[i], "--start-system-server") == 0 : false; setArgv0(argv0, "zygote"); set_process_name("zygote"); // 使用运行时环境启动Zygote的初始化类. runtime.start("com.android.internal.os.ZygoteInit", startSystemServer); ...}
now, from the C or C + + code into the Java code, Zygoteinit.java initializes the class with the following code:
public static void Main (String argv[]) {//Load system run dependent class. Preloadclasses (); ... if (Argv[1].equals ("true")) {//Zygote incubator process begins incubation system core services. Startsystemserver (); } else if (!argv[1].equals ("false")) {throw new RuntimeException (Argv[0] + usage_string); } ... } private static Boolean Startsystemserver () throws Methodandargscaller, RuntimeException {String args[] = { "--setuid=1000", "--setgid=1000", "--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,10 09,1010,3001,3002,3003 ","--capabilities=130104352,130104352 ","--runtime-init ","--nice- Name=system_server "," Com.android.server.SystemServer ",}; ...//Incubator fork Open the Systemserver class, and put the parameters defined above. Passed to this class. Used to start system-critical services. PID = Zygote.forksystemserver (Parsedargs.uid, Parsedargs.gid, Parsedargs.gids, debugFlags, NULL, parsedargs.permittedcapabilities, parsedargs.effectivecapabilities); ... }
zygote Process fork out the Systemserver class, the main function is as follows:
public static void main(String[] args) { ... // 加载本地的动态链接库. System.loadLibrary("android_servers"); // 调用动态链接库中的c函数. init1(args); } // 这里init1的函数定义在frameworks\base\services\jni\com_android_server_SystemServer.cpp下的方法. native public static void init1(String[] args);
the code snippet for Com_android_server_systemserver.cpp is as follows:
static JNINativeMethod gMethods[] = { /* name, signature, funcPtr */ // 把native方法init1, 映射到android_server_SystemServer_init1. (这里是定义的函数指针) { "init1", "([Ljava/lang/String;)V", (void*) android_server_SystemServer_init1 }, }; static void android_server_SystemServer_init1(JNIEnv* env, jobject clazz) { // 转调 system_init(); } // 此方法没有方法体. extern "C" int system_init();
The
method body of the System_init method, in the System_init.cpp class. The code is as follows:
extern "C" status_t system_init() { ... // 开启一些硬件相关的服务. SensorService::instantiate(); ... // 获取Android运行时环境 AndroidRuntime* runtime = AndroidRuntime::getRuntime(); LOGI("System server: starting Android services.\n"); // 调用SystemServer类中静态方法init2. 从native层转到java层. runtime->callStatic("com/android/server/SystemServer", "init2"); ... }
Systemserver The Init2 method is as follows:
public static final void init2() { Slog.i(TAG, "Entered the Android system server!"); // 进入Android系统服务的初始化. Thread thr = new ServerThread(); thr.setName("android.server.ServerThread"); thr.start(); }
the Run method in Serverthread is as follows:
@Override public void run() { ... // 初始化系统的服务, 并且把服务添加ServiceManager中, 便于以后系统进行统一管理. ServiceManager.addService("entropy", new EntropyService()); ... // 调用了ActivityManagerService的systemReady的方法. ((ActivityManagerService)ActivityManagerNative.getDefault()) .systemReady(new Runnable() { public void run() { ... } }); ... }
the Systemready method under Activitymanagerservice is as follows:
public void systemReady(final Runnable goingCallback) { ... // 调用了ActivityStack中的resumeTopActivityLocked去启动Activity mMainStack.resumeTopActivityLocked(null);}
the Resumetopactivitylocked method in Activitystack is as follows:
final boolean resumeTopActivityLocked(ActivityRecord prev) { // 找到第一个当前没有关闭的Activity, 系统刚刚系统没有任何Activity执行, 所以next为null ActivityRecord next = topRunningActivityLocked(null); // Remember how we‘ll process this pause/resume situation, and ensure // that the state is reset however we wind up proceeding. final boolean userLeaving = mUserLeaving; mUserLeaving = false; if (next == null) { // There are no more activities! Let‘s just start up the // Launcher... if (mMainStack) { // 开启Launcher应用的第一个Activity界面. return mService.startHomeActivityLocked(); } } }
The Home screen displays, and the Android system is up and ready. Enter the standby screen.
"Source Analysis" Android system startup process.