Zygote进程
在init.rc中有
service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server
zygote进程通过init进程启动,zygote进程就奠定了Android的基础。
从zygote的main()开始,见/frameworks/base/cmds/app_process/app_main.cpp
int main(int argc, const char * const argv[])
{
......
if(0 == strcmp("--zygote", arg)){
......
runtime.start("com.android.internel.os.ZygoteInit",
startSystemServer);
......
}
接下来运行/frameworks/base/core/jni/AndroidRuntime.cpp的start()
void AndroidRuntime::start(const char * className, const bool startSystemServer)
{
......
classNameStr = env->NewStringUTF(className);
......
startMeth = env->GetStaticMethodID(startClass, "main",
"([Ljava/lang/String;)V");
......
}
因此运行/frameworks/base/java/com/android/internal/os/ZygoteInit.java的main().
public static void main(String argv[]){
......
registerZygoteSocket();
preloadClasses();
if(argc[1].equals("true")){
startSystemServer();
}
......
}
在startSystemServer()有
private static boolean startSystemServer(){
......
pid = Zygote.forkSystemServer(
parseArgs.uid, parseArgs.gid,
parseArgs.gids, debugFlags, null)
......
if(pid == 0)
{
handleSystemServerProcess(parseArgs);
}
}
private static void handleSystemServerProcess(
ZygoteConnection.Arguments parsedArgs)
throws ZygoteInit.MethodAndArgsCaller {
......
RuntimeInit.zygoteInit(parsedArgs.remainingArgs);
}
在zygoteInit(String[] argv)@/frameworks/base/core/java/com/android/internal/os/RuntimeInit.java
依次调用zygoteNative()@/frameworks/base/core/java/com/android/internal/os/RuntimeInit.java
invokeStaticMain() @/frameworks/base/core/java/com/android/internal/os/RuntimeInit.java
在invokeStaticMain()中
m = cl.getMethod("main", new Class[] { String[].class });
执行的是SystemServer类main函数
public static void main(String[] args){
......
System.loadLibrary("android_servers");
init1(args);
}
init1依次调用
android_server_SystemServer_init1()@ /frameworks/base/services/jni/com_android_server_SystemServer.cpp
system_init() @/frameworks/base/cmds/system_server/library/System_init.cpp
extern "C" status_t system_init()
{
sp<ProcessState> proc(ProcessState::self());
sp<IServiceManager> sm = defaultServiceManager();
sp<GrimReaper> grim = 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) {
SurfaceFlinger::instantiate();
}
if (!proc->supportsProcesses()) {
// Start the AudioFlinger
AudioFlinger::instantiate();
// Start the media playback service
MediaPlayerService::instantiate();
// Start the camera service
CameraService::instantiate();
// Start the audio policy service
AudioPolicyService::instantiate();
}
AndroidRuntime* runtime = AndroidRuntime::getRuntime();
runtime->callStatic("com/android/server/SystemServer", "init2");
if (proc->supportsProcesses()) {
LOGI("System server: entering thread pool./n");
ProcessState::self()->startThreadPool();
IPCThreadState::self()->joinThreadPool();
LOGI("System server: exiting thread pool./n");
}
return NO_ERROR;
}
在system_init()初始化AudioFlinger, MediaPlayerService, CameraService, AudioPolicyService等,最后由如下
runtime->callStatic("com/android/server/SystemServer", "init2")调用init2。
public static final void init2() {
Slog.i(TAG, "Entered the Android system server!");
Thread thr = new ServerThread();
thr.setName("android.server.ServerThread");
thr.start();
}
最后运行ServerThread的run.
在run中启动电源管理、蓝牙等核心服务以及状态。
在ServerThread后半段,启动完所有Android服务后,使用xxx.systemReady()通知个服务,系统已经就绪。
ActivityManagerService.systemReady()中依次调用resumeTopActivityLocked()和startHomeActivityLocked().
如果是第一个则启动HomeActivity.
private boolean startHomeActivityLocked() {
if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL
&& mTopAction == null) {
return false;
}
Intent intent = new Intent(
mTopAction,
mTopData != null ? Uri.parse(mTopData) : null);
intent.setComponent(mTopComponent);
if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
intent.addCategory(Intent.CATEGORY_HOME);
}
ActivityInfo aInfo =
intent.resolveActivityInfo(mContext.getPackageManager(),
STOCK_PM_FLAGS);
if (aInfo != null) {
intent.setComponent(new ComponentName(
aInfo.applicationInfo.packageName, aInfo.name));
ProcessRecord app = getProcessRecordLocked(aInfo.processName,
aInfo.applicationInfo.uid);
if (app == null || app.instrumentationClass == null) {
intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityLocked(null, intent, null, null, 0, aInfo,
null, null, 0, 0, 0, false, false);
}
}
return true;
}
这是"android.process.acore"进程。
在启动的时候, ActivityManagerService.systemReady将启动所有常驻应用. 如下:
List apps = ActivityThread.getPackageManager().
getPersistentApplications(PackageManager.GET_SHARED_LIBRARY_FILES);
if (apps != null) {
int N = apps.size();
int i;
for (i=0; i<N; i++) {
ApplicationInfo info
= (ApplicationInfo)apps.get(i);
if (info != null &&
!info.packageName.equals("android")) {
addAppLocked(info);
}
}
}
常驻应用在AndroidManifest.xml中有android:persistent="true"