在Android操作系统的源码中,SystemServer 类的 main 方法是 Android 应用程序启动的入口点。它在应用程序进程启动时被调用。具体来说,它是在 Zygote 进程中通过反射机制被调用的。
SystemServer.main 方法实际上是由 ZygoteInit 类启动应用程序进程时通过 RuntimeInit 类来调用的。以下是调用关系的简要说明:
ZygoteInit 启动新进程: Zygote 是一个守护进程,负责孵化新进程。当系统请求启动一个新的应用程序时,ZygoteInit 会创建一个新进程。
ZygoteInit 通过 fork 创建新进程: ZygoteInit 初始化应用程序进程,并在新进程中调用 ZygoteInit.zygoteInit 方法,该方法进一步调用 RuntimeInit 的 commonInit 和 applicationInit 方法。
RuntimeInit 初始化: 在 applicationInit 方法中,会通过反射调用 SystemServer.main 方法
以下是关键代码片段:
// ZygoteInit.java
public static void main(String[] argv) {
// Initialize the system and start the Zygote process
ZygoteServer zygoteServer = new ZygoteServer();
...
if (startSystemServer) {
Runnable r = forkSystemServer(abiList, zygoteSocketName, zygoteServer);
// {@code r == null} in the parent (zygote) process, and {@code r != null} in the
// child (system_server) process.
if (r != null) {
r.run();
return;
}
}
...
}
private static boolean forkSystemServer(String abiList, String socketName,
ZygoteServer zygoteServer) {
// Fork and start the system server process.
...
/* For child process */
if (pid == 0) {
if (hasSecondZygote(abiList)) {
waitForSecondaryZygote(socketName);
}
zygoteServer.closeServerSocket();
return handleSystemServerProcess(parsedArgs);
}
...
}
private static Runnable handleSystemServerProcess(ZygoteArguments parsedArgs) {
...
/*
* Pass the remaining arguments to SystemServer.
*/
return ZygoteInit.zygoteInit(parsedArgs.mTargetSdkVersion,
parsedArgs.mDisabledCompatChanges,
parsedArgs.mRemainingArgs, cl);
...
}
public static final Runnable zygoteInit(int targetSdkVersion, long[] disabledCompatChanges,
String[] argv, ClassLoader classLoader) {
if (RuntimeInit.DEBUG) {
Slog.d(RuntimeInit.TAG, "RuntimeInit: Starting application from zygote");
}
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ZygoteInit");
RuntimeInit.redirectLogStreams();
RuntimeInit.commonInit();
ZygoteInit.nativeZygoteInit();
return RuntimeInit.applicationInit(targetSdkVersion, disabledCompatChanges, argv,
classLoader);
}
// RuntimeInit.java
protected static Runnable applicationInit(int targetSdkVersion, long[] disabledCompatChanges,
String[] argv, ClassLoader classLoader) {
...
final Arguments args = new Arguments(argv);
// The end of of the RuntimeInit event (see #zygoteInit).
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
// Remaining arguments are passed to the start class's static main
return findStaticMain(args.startClass, args.startArgs, classLoader);
}
protected static Runnable findStaticMain(String className, String[] argv,
ClassLoader classLoader) {
Class<?> cl;
try {
cl = Class.forName(className, true, classLoader);
} catch (ClassNotFoundException ex) {
throw new RuntimeException(
"Missing class when invoking static main " + className,
ex);
}
Method m;
try {. //这里就是调用ActivityThread的地方
m = cl.getMethod("main", new Class[] { String[].class });
} catch (NoSuchMethodException ex) {
throw new RuntimeException(
"Missing static main on " + className, ex);
} catch (SecurityException ex) {
throw new RuntimeException(
"Problem getting static main on " + className, ex);
}
int modifiers = m.getModifiers();
if (! (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))) {
throw new RuntimeException(
"Main method is not public and static on " + className);
}
return new MethodAndArgsCaller(m, argv);
}
通过以上调用链,SystemServer.main 方法在应用程序进程启动时得以执行。所以,最终是通过 Zygote 进程的初始化和反射机制来调用的 SystemServer.main 方法
网友评论