Android-FrameWork架构C++源码剖析
Android 系统整体架构
+-------------------------------------------+
| APP: home, Tel, Media, Camera ... |
+-------------------------------------------+
| FrameWork(Java): Activity, Window ... |
+-------------------------------------------+
+----------------------+--------------------+
| ThirdParty Library | |
| FrameWork(C++) | Dalvik |
+----------------------+--------------------+
+-------------------------------------------+
| HAL: Audio, sensor, WIFI, Camera ... |
+===========================================+
+==============================================+
| Linux kernel: Memory, VFS, Driver, Binder ...|
+==============================================+
虚拟机创建和第一个java程序引导
启动脚本中定义
system/core/rootdir/init.zygote32.rc:
service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server
system/core/rootdir/init.zygote64.rc:
service zygote /system/bin/app_process64 -Xzygote /system/bin --zygote --start-system-server
/system/bin/app_process -Xzygote
/system/bin --zygote --start-system-server
app_process
AndroidRuntime.start()
app_process 程序入口
frameworks\base\cmds\app_process\app_main.cpp
其中部分 AndroidRuntime 方法实现在这里
frameworks\base\core\jni\AndroidRuntime.cpp
runtime.start("com.android.internal.os.ZygoteInit", startSystemServer)
{
-> startVm() -> JNI_CreateJavaVM() -> dvmCreateJNIEnv()
-> startReg() -> register_jni_procs();
-> start zygote service()
{
startClass = env->FindClass(slashClassName);
startMeth = env->GetStaticMethodID(startClass, "main", "([Ljava/lang/String;)V");
env->CallStaticVoidMethod(startClass, startMeth, strArray);
}
}
app 启动过程
Launcher process::ActivityManagerProxy --> system_server::ActivityManagerService: startActivity
system_server::ActivityManagerService --> Zygote: create process req
Zygote --> Zygote: fork for app
APP::ActivityManagerProxy --> system_server::ActivityManagerService: attach_application
system_server::ActivityManagerService --> system_server::ApplicationThreadProxy: realStartActivityLocked
system_server::ApplicationThreadProxy --> APP:ApplicationThread: scheduledLaunchActivity
APP::ApplicationThread -> APP::ActivityThread: H.LAUNCH_ACTIVITY
APP::ActivityThread -> APP::ActivityThread: handleLaunchActivity
APP::ActivityThread -> APP::Activity.onCreate
plantuml 图代码如下
@startuml
box LauncherProcess
boundary LauncherProcess_ActivityManagerProxy
end box
box system_server
boundary ActivityManagerService
boundary ApplicationThreadProxy
end box
box APP
boundary ActivityManagerProxy
boundary ApplicationThreadProxy
boundary ApplicationThread
boundary Activity
boundary ActivityThread
end box
LauncherProcess_ActivityManagerProxy --> ActivityManagerService: startActivity
ActivityManagerService --> Zygote: create process req
Zygote --> ActivityManagerProxy: fork app
ActivityManagerProxy --> ActivityManagerService: attach_application
ActivityManagerService --> ApplicationThreadProxy: realStartActivityLocked
ApplicationThreadProxy --> ApplicationThread: scheduledLaunchActivity
ApplicationThread --> ActivityThread: H.LAUNCH_ACTIVITY
ActivityThread -> ActivityThread: handleLaunchActivity
ActivityThread -> Activity: Activity.onCreate
@enduml
未完待续...