我正在搜索一些有关如何在Android上启动应用程序的信息。我想让人们发现信息使合子和fork()结合起来。您知道一些有用的网站或书籍吗?
我写了两部分的系列文章,在我的博客上解释了Android应用程序的启动过程-
http://multi-core-dump.blogspot.com/2010/04/android-application-launch.html
http://multi-core-dump.blogspot.com/2010/04/android-application-launch-part-2.html
我希望您会发现它有用。
this演示中有一个很好的解释。它用韩语部分编写,但大部分信息用英语编写。
这是一个简洁的方法(该过程非常复杂,因此简洁的方法不会太短),但基于AOSP 9.0.0的准确答案。
每个Android Java进程都是从Zygote分叉的,所以首先是Zygote的启动方式。
init进程是linux中的第一个进程,它启动可执行文件“ app_process”,其内部为:
(entry point of app_process)int main
->void AndroidRuntime::start
startVm //start Java VM
startReg //register common native functions
//call java method ZygoteInit.main from native code
env->CallStaticVoidMethod
这是最重要的java方法:ZygoteInit.main,我们从上面的本地代码“ env-> CallStaticVoidMethod”中获得。
这也是在主Activity的onCreate中设置断点并开始调试应用程序并在其中中断时,调用堆栈中的第一个方法。但是实际上您的应用程序永远不会到达ZygoteInit.main的开头],它仅在app_process(或说Zygote)中从头开始执行。
//Java code ->ZygoteInit.main //Android application never get here(the beginning) //start system server process which contains AMS/WMS,etc forkSystemServer //for Zygote, runSelectLoop never return //for Android application, runSelectLoop returns a Runnable //whose run() method will just execute ActivityThread.main //which is considered as the real main entry of Android application //since it contains the message loop caller = zygoteServer.runSelectLoop ->zygoteServer.runSelectLoop //this is the main loop of Zygote who is a //server that receive process-creating requests //from client processes and fork them loop forever //Zygote wait here for other process's requests to start new Java process Os.poll(pollFds, -1); //after wake up upon requests arrive, process the request final Runnable command = connection.processOneCommand(this); ->ZygoteConnection.processOneCommand read and parse process-start request command from client process //the native linux fork() is executed in this methdod //after it returns, we can decide which process we are in //from pid's value just like the native fork() pid = Zygote.forkAndSpecialize //if in child return handleChildProc ->ZygoteConnection.handleChildProc return ZygoteInit.zygoteInit ->ZygoteInit.zygoteInit RuntimeInit.commonInit(); ZygoteInit.nativeZygoteInit();//JNI method //native code ->AppRuntime::onZygoteInit() sp<ProcessState> proc = ProcessState::self(); //starting thread pool which contains binder threads proc->startThreadPool(); return RuntimeInit.applicationInit ->RuntimeInit.applicationInit return findStaticMain ->RuntimeInit.findStaticMain //MethodAndArgsCaller is a Runnable, //whose run() is constructed so that //it just call ActivityThread.main() //it is ActivityThread since there is a string parameter //whose content is "android.app.ActivityThread" from the client process's request return new MethodAndArgsCaller //if in parent process i.e. Zygote return null //if in forked child process return command; //if in parent process i.e. Zygote continue to run the loop //Zygote never get here //for Android application, now caller contains a MethodAndArgsCaller which is a Runnable //whose run() calls ActivityThread.main and never return //since ActivityThread.main runs the main message loop caller.run();
启动活动时,最后进入到活动管理器服务(AMS,它在系统服务器进程中)。如果尚未创建该活动的进程,则AMS将向Zygote服务器发送进程启动请求请求(在上述的zygote进程中,由init进程启动),该进程类似于:
//in AMS (system server process) final String entryPoint = "android.app.ActivityThread"; return startProcessLocked(....,entryPoint, ....); ->startProcessLocked ->Process.start ->ZygoteProcess.start ->ZygoteProcess.startViaZygote ->ZygoteProcess.zygoteSendArgsAndGetResult //send the request to Zygote server through sockets //note that "android.app.ActivityThread" is send to Zygote server as a parameter
这时,上面列出的Zygote代码将从中唤醒
Os.poll(pollFds, -1);
并分叉子进程,然后,父进程(即Zygote)将再次执行轮询以等待下一个请求,并且分叉的子进程将从runSelectLoop返回并执行ActivityThread.main,如上面的代码清单所述。] >
因此,新过程的确切入口将在Zygote.forkAndSpecialize中的本机fork()之后,在称为ForkAndSpecializeCommon的本机函数中,确切地说,然后一直通过返回路径向上到达
caller = zygoteServer.runSelectLoop
在ZygoteInit.main中。因此,尽管Android应用程序的调用堆栈始于ZygoteInit.main,但在ZygoteInit.main中执行的代码在调用runSelectLoop之后开始,而不是ZygoteInit.main的开始。
关于活动:实际上,活动与入口点或启动过程无关。当AMS随时向流程发送“活动开始”请求时,就会启动“活动”。因此,活动启动过程总是在收到启动请求时在主消息循环中开始,它由来自AMS的消息驱动,并与应用程序启动过程完全分离。