适用平台:MTK
文章内容包括Android启动流程相关知识。
Android 开机启动流程可以分为三个部分,硬件启动,内核启动,Android启动。
硬件启动包括了
Pre-Loader,UBOOT
内核启动
linux kernel
Android启动
init
services
zygote
system server
server manager
home launcher
1 首先来看 内核启动
文件<project root path>/kernel/init/main.c
内核进程寻找并启动init进程。
init进程是linux kernel起来之后启动的第一个用户进程,执行了目录创建挂载、rc文件解析执行、环境设置、启动服务、socket监听等动作。
USER PID PPID VSIZE RSS WCHAN PC NAME
root 1 0 924 568 c018b9f4 0001c040 S /init
2 再来看Android 启动
/system/bin/servicemanager 启动
路径:frameworks/base/cmds/servicemanager/service_manager.c
int main(int argc, char **argv) { struct binder_state *bs; void *svcmgr = BINDER_SERVICE_MANAGER; bs = binder_open(128*1024); if (binder_become_context_manager(bs)) { ALOGE("cannot become context manager (%s)\n", strerror(errno)); return -1; } svcmgr_handle = svcmgr; binder_loop(bs, svcmgr_handler); return 0; }
Zygote启动
路径:frameworks/base/cmds/system_server/app_process/app_main.cpp
int main( ){ AppRuntime runtime; if (zygote) { runtime.start("com.android.internal.os.ZygoteInit", startSystemServer ? "start-system-server" : ""); } } class AppRuntime : public AndroidRuntime{}
zygoteInit
路径:frameworks\base\core\java\com\android\internal\os\ZygoteInit.java
public static void main(String argv[]) { if (argv[1].equals("start-system-server")) { startSystemServer(); } else if (!argv[1].equals("")) { throw new RuntimeException(argv[0] + USAGE_STRING); } }
SystemServer
路径:frameworks\base\services\java\com\android\server\SystemServer.java
public static void main(String[] args) { System.loadLibrary("android_servers"); init1(args); } native public static void init1(String[] args);
init1()
路径:frameworks/base/services/jni/com_android_server_SystemServer.cpp
static void android_server_SystemServer_init1(JNIEnv* env, jobject clazz) { system_init(); } /* * JNI registration. */ static JNINativeMethod gMethods[] = { /* name, signature, funcPtr */ { "init1", "([Ljava/lang/String;)V", (void*) android_server_SystemServer_init1 }, };
system_init()
路径:frameworks/base/cmds/system_server/library/system_init.cpp
JNIEnv* env = runtime->getJNIEnv(); jclass clazz = env->FindClass("com/android/server/SystemServer"); jmethodID methodId = env->GetStaticMethodID(clazz, "init2", "()V"); env->CallStaticVoidMethod(clazz, methodId);
init2()
路径:frameworks\base\services\java\com\android\server\SystemServer.java
wm = WindowManagerService.main(context, power, display, inputManager, uiHandler, wmHandler, factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL, !firstBoot, onlyCore); ActivityManagerService.self().setWindowManager(wm); ActivityManagerService.self().systemReady(...);
systemReady()
路径: 路径: frameworks/base/services/java/com/android/server/am/ActivityManagerService.java
public void systemReady(final Runnable goingCallback) { mMainStack. resumeTopActivityLocked(null); }
Home Launcher 启动
resumeTopActivityLocked()
路径:frameworks/base/services/java/com/android/server/am/ActivityStack.java
final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) { mService.startHomeActivityLocked(0); }
startHomeActivityLocked()
路径:frameworks/base/services/java/com/android/server/am/ActivityManagerService.java
boolean startHomeActivityLocked(int userId) { Intent intent = new Intent(mTopAction, mTopData != null ? Uri.parse(mTopData) : null); .... intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK); mMainStack.startActivityLocked(null, intent, null, aInfo, null, null, 0, 0, 0, 0, null, false, null); }