Note: 半原创,英语不好的同学别扔鞋。未完待续...
This is how Android start up:
1) Bootloader loads the Linux kernel and starts the init process(/system/init).
2) Init starts some android daemons, just as usbd, adbd, debuggerd, rild, vold, netd, installd, qemud and so on. All these daemon are implemented as Android native program. Most of them are in the /system/bin.
3) Init starts the zygote process. Zygote is a core process that:
· A nascent process which initializes a Dalvik VM instance
· Loads classes and listens on socket for requests to spawn VMs
· Forks on request to create VM instances for managed processes
· Copy-on-write to maximize re-use and minimize footprint
4) Init starts runtime process. Initializes Service Manager, the context manager for Binder that handles service registration and lookup. Then registers Service Manager as default context manager for Binder services.
5) Runtime process sends request for Zygote to start System Server. Zygote forks a new VM instance for the System Service process and starts the service.
6) System Server starts the native system servers, including Surface Flinger and Audio Flinger. Native system servers register with Service Manager as IPC service targets. By now, runtime process, Service Manager, System Server and native servers are all implement as Android native program.
7) System Service starts the Android managed services, just as Window Manager, Activity Manager, Package Manager, Power Manager, Content Manager, Telephony Service, Bluetooth Service, Connectivity Service, Location Manager. Android managed Services register with Service Manager.
8) After system server loads all services, the system is ready. Then start the launcher and home.
This is the major code of Android starting up.
/system/core/init/init.c
806: int main(int argc, char **argv)
855: parse_config_file("/init.rc");
877-908: " A N D R O I D ";
/system/core/rootdir/init.rc 261-384:
## Daemon processes to be run by init. …. (2)
on property:persist.service.adb.enable=1
start adbd …
service servicemanager /system/bin/servicemanager (4)
user system
critical
onrestart restart zygote
onrestart restart media …
service debuggerd /system/bin/debuggerd …
service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server (3) (5)
socket zygote stream 666
onrestart write /sys/android_power/request_state wake
onrestart write /sys/power/state on
onrestart restart media …
service bootanim /system/bin/bootanimation
user graphics
group graphics
disabled
oneshot …
/frameworks/base/cmds/app_process/app_mian.cpp (3)
157: set_process_name("zygote");
158: runtime.start("com.android.internal.os.ZygoteInit",startSystemServer); //call main
/frameworks/base/core/java/com/android/internal/os/ZygoteInit.java (3)
581: public static void main(String argv[]) {
registerZygoteSocket(); ...
preloadClasses(); ...
preloadResources(); ...
startSystemServer(); ...
if (ZYGOTE_FORK_MODE) {
runForkMode();
} else {
runSelectLoopMode();
} …
537: private static boolean startSystemServer() { …. (5)
pid = Zygote.forkSystemServer(parsedArgs.uid, parsedArgs.gid,parsedArgs.gids, debugFlags, null);
if (pid == 0) {
handleSystemServerProcess(parsedArgs);
}
508: private static void handleSystemServerProcess(ZygoteConnection.Arguments parsedArgs){ …(5)
RuntimeInit.zygoteInit(parsedArgs.remainingArgs);
/dalvik/vm/native/dalvik_system_Zygote.c
436: static void Dalvik_dalvik_system_Zygote_forkSystemServer(const u4* args, JValue* pResult){
/frameworks/base/core/java/com/android/internal/os/RuntimeInit.java (5)//start SystemServer.java main
255: public static final void zygoteInit(String[] argv) { ...
invokeStaticMain(startClass, startArgs);
/frameworks/base/services/java/com/android/server/SystemServer.java (5)
508: public static void main(String[] args) {
...
init1(args);
/frameworks/base/services/jni/com_android_server_SystemServer.cpp
26: static void android_server_SystemServer_init1(JNIEnv* env, jobject clazz)
{
system_init();
}
/frameworks/base/cmds/system_server/library/system_init.cpp (5) //start native service
53: extern "C" status_t system_init() {
...
// Start the SurfaceFlinger
SurfaceFlinger::instantiate();
...
// 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();
...
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");
}
/frameworks/base/services/java/com/android/server/SystemServer.java (5) //start framework service
578: public static final void init2() {
Thread thr = new ServerThread();
thr.start();
74: public void run() {
...
Slog.i(TAG, "Entropy Service");
ServiceManager.addService("entropy", new EntropyService());
Slog.i(TAG, "Power Manager");
power = new PowerManagerService();
ServiceManager.addService(Context.POWER_SERVICE, power);
Slog.i(TAG, "Activity Manager");
context = ActivityManagerService.main(factoryTest);
Slog.i(TAG, "Telephony Registry");
ServiceManager.addService("telephony.registry", new TelephonyRegistry(context));
Slog.i(TAG, "Account Manager");
ServiceManager.addService(Context.ACCOUNT_SERVICE,new AccountManagerService(context));
Slog.i(TAG, "Content Manager");
ContentService.main(context,factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL);
Slog.i(TAG, "System Content Providers");
ActivityManagerService.installSystemProviders();
Slog.i(TAG, "Battery Service");
battery = new BatteryService(context);
ServiceManager.addService("battery", battery);
Slog.i(TAG, "Lights Service");
lights = new LightsService(context);
Slog.i(TAG, "Vibrator Service");
ServiceManager.addService("vibrator", new VibratorService(context));
…
wm.systemReady(); //lock screen entry
...
((ActivityManagerService)ActivityManagerNative.getDefault())
.systemReady(new Runnable() { public void run() {
Slog.i(TAG, "Making services ready");
if (batteryF != null) batteryF.systemReady();
if (connectivityF != null) connectivityF.systemReady();
if (dockF != null) dockF.systemReady();
if (uiModeF != null) uiModeF.systemReady();
if (recognitionF != null) recognitionF.systemReady();
Watchdog.getInstance().start();
// It is now okay to let the various system services start their
// third party code...
if (appWidgetF != null) appWidgetF.systemReady(safeMode);
if (wallpaperF != null) wallpaperF.systemReady();
if (immF != null) immF.systemReady();
if (locationF != null) locationF.systemReady();
if (throttleF != null) throttleF.systemReady(); //21 Service is started in froyo
/frameworks/base/services/java/com/android/server/am/ActivityManagerService.java //start Launcher and home
8725: public void systemReady(final Runnable goingCallback) {
...
resumeTopActivityLocked(null); }
2597: private final boolean resumeTopActivityLocked(HistoryRecord prev){
if (next == null) {
// There are no more activities! Let's just start up the
// Launcher...
return startHomeActivityLocked();
}
2478: private boolean startHomeActivityLocked() {
...
intent.addCategory(Intent.CATEGORY_HOME);
...
startActivityLocked(null, intent, null, null, 0, aInfo, null, null, 0, 0, 0, false, false);
frameworks/policies/base/phone/com/android/internal/policy/impl/PhoneWindowManager.java
2177: public void systemReady() {
mKeyguardMediator.onSystemReady();
...
Note:
1. All framework and core jar is compiled into dex file in /data/dalvik-cache, as well as all apk in /data/app and /system/app.
2. Application and framework and service finally use JNI to invoke android native functions.