Android系统启动流程

梳理了Android的启动流程,记录一下,要不然又忘了。

阅读源码要有窍门:1.带着问题,有一个线索。2.忽视与线索无关的代码。3.画图,做笔记。4.反复跟读。5.不能只看别人的分析,要动手操作。

源码版本4.2.2

使用到的文件的路径如下:

system\core\init\init.c

frameworks\base\cmds\app_process\app_main.cpp

frameworks\base\core\jni\AndroidRuntime.cpp

frameworks\base\core\java\java\com\android\internal\os\ZygoteInit.java

frameworks\base\core\java\java\com\android\internal\os\RuntimeInit.java

frameworks\base\services\java\com\android\server\SystemServer.java

frameworks\base\services\jni\com_android_server_SystemServer.cpp

frameworks\base\cmds\system_server\library\system_init.cpp

frameworks\base\services\java\com\android\server\am\ActivityManagerService.java

frameworks\base\core\java\com\android\internal\os\ZygoteConnection.java

1.init进程启动

init进程是linux kernel的第一个用户进程,也是分析的起点。

init进程的可执行文件:init 。adb shell连接到模拟器,根目录下就可以看到这个文件。对应的源码位置:system\core\init\。

入口:init.c中的main()函数。

最重要的一句话:init_parse_config_file("/init.rc")。解析一个叫Init.rc的文件。adb shell连接到模拟器,根目录下就可以看到这个init.rc文件。这个文件有固定的格式和执行顺序。

service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server

这句话很重要。用于启动zygote进程。zygote进程的可执行文件,对应的源码:app_main.cpp

2.zygote进程

app_main.cpp的main()函数:

AppRuntime runtime;

runtime.start("com.android.internal.os.ZygoteInit",startSystemServer ? "start-system-server" : "");

AppRuntime继承于AndroidRuntime。

AndroidRuntime的start()方法:

JNIEnv* env;

if (startVm(&mJavaVM, &env) != 0) {

return;

}

//jni调用ZygoteInit的main()方法

ZygoteInit的main()方法:

public static void main(String argv[]) {

startSystemServer();

}

private static boolean startSystemServer(){

/* Hardcoded command line to start the system server */

String args[] = {

"--setuid=1000",

"--setgid=1000",

"--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1018,3001,3002,3003,3006,3007",

"--capabilities=130104352,130104352",

"--runtime-init",

"--nice-name=system_server",

"com.android.server.SystemServer",

};

ZygoteConnection.Arguments parsedArgs = null;

int pid;

try {/* Request to fork the system server process */

pid = Zygote.forkSystemServer(

parsedArgs.uid, parsedArgs.gid,

parsedArgs.gids,

parsedArgs.debugFlags,

null,

parsedArgs.permittedCapabilities,

parsedArgs.effectiveCapabilities);

} catch (IllegalArgumentException ex) {

throw new RuntimeException(ex);

}

/* For child process */

if (pid == 0) {

//子进程的代码

handleSystemServerProcess(parsedArgs);

}

return true;

}

/**

* Finish remaining work for the newly forked system server process.

*/

private static void handleSystemServerProcess(

RuntimeInit.zygoteInit(parsedArgs.targetSdkVersion, parsedArgs.remainingArgs);

}

public static final void zygoteInit(int targetSdkVersion, String[] argv)

throws ZygoteInit.MethodAndArgsCaller {

nativeZygoteInit();

applicationInit(targetSdkVersion, argv);

}

AndroidRuntime.cpp

static void com_android_internal_os_RuntimeInit_nativeZygoteInit(JNIEnv* env, jobject clazz)

{

gCurRuntime->onZygoteInit();

}

app_main.cpp

onZygoteInit()是AppRuntime的方法

private static void applicationInit(int targetSdkVersion, String[] argv)

throws ZygoteInit.MethodAndArgsCaller {

//调用SystemServer的main()方法

invokeStaticMain(args.startClass, args.startArgs);

}

SystemServer的main方法:

init1();//native方法

com_android_server_SystemServer.cpp
static void android_server_SystemServer_init1(JNIEnv* env, jobject clazz)

{

system_init();

}

system_init.cpp

extern "C" status_t system_init(){  

ALOGI("Entered system_init()");  

spproc(ProcessState::self());   

spsm = defaultServiceManager();   

ALOGI("ServiceManager: %p\n", sm.get());   

spgrim = new GrimReaper();

sm->asBinder()->linkToDeath(grim, grim.get(), 0);

char propBuf[PROPERTY_VALUE_MAX];

property_get("system_init.startsurfaceflinger", propBuf, "1");

if (strcmp(propBuf, "1") == 0) {

// Start the SurfaceFlinger

SurfaceFlinger::instantiate();

}

property_get("system_init.startsensorservice", propBuf, "1");

if (strcmp(propBuf, "1") == 0) {

// Start the sensor service

SensorService::instantiate();

}

// And now start the Android runtime.  We have to do this bit

// of nastiness because the Android runtime initialization requires

// some of the core system services to already be started.

// All other servers should just start the Android runtime at

// the beginning of their processes's main(), before calling

// the init function.

ALOGI("System server: starting Android runtime.\n");

AndroidRuntime* runtime = AndroidRuntime::getRuntime();

ALOGI("System server: starting Android services.\n");

JNIEnv* env = runtime->getJNIEnv();

if (env == NULL) {

return UNKNOWN_ERROR;

}

jclass clazz = env->FindClass("com/android/server/SystemServer");

if (clazz == NULL) {

return UNKNOWN_ERROR;

}

jmethodID methodId = env->GetStaticMethodID(clazz, "init2", "()V");

if (methodId == NULL) {

return UNKNOWN_ERROR;

}

env->CallStaticVoidMethod(clazz, methodId);

ALOGI("System server: entering thread pool.\n");

ProcessState::self()->startThreadPool();

IPCThreadState::self()->joinThreadPool();

ALOGI("System server: exiting thread pool.\n");

return NO_ERROR;

}

//a.启动ServiceManager b.调用SystemServier的init2方法


init2{

new ServerThread().run();

}

ServerThread中启动ActivityManagerService,AMS准备好后,启动了launcher.

3.Zygote进程孵化App进程

ZygoteInit的runSelectLoopMode()

你可能感兴趣的:(Android系统启动流程)