Android Application launch process
Asked Answered
A

3

6

I am searching for some information about how are apps launch on android. I want ot found infromation what make zygote and about fork() . Do you know some usefull web-sites or books?

Ajmer answered 3/2, 2012 at 16:23 Comment(2)
What is the actual problem? Activities are started with startActivity. For getting in-depth knowledge how android handles processes you better search for the linux kernel instead of android :)Remediosremedy
i want to know how exactly android apps are launchning. Something like this multi-core-dump.blogspot.com/2010/04/…, but more detailedAjmer
C
18

I have written a two part series to explain Android Application launch process on my blog -

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

I hope you will find it useful.

Codger answered 5/4, 2012 at 22:28 Comment(0)
O
2

There is a nice explanation in this presentation. It's written partially in Korean, but the most part of information in English.

Oswaldooswalt answered 3/2, 2012 at 17:7 Comment(2)
Thanks.it is very nice. And there are some more documents on kandroid.org, but in Korean. Do you know some others websites with the same content but on English?Ajmer
Sorry, but not. I do not know. People from Korea are really good in Android OS development. I sometimes find required information that is only in Korean (translate tools help me :)Oswaldooswalt
I
0

Here is an concise(the process is very complex so a concise one still won't be a short one) yet precise answer based on AOSP 9.0.0.

Every Android Java process is forked from Zygote, so first is how Zygote starts.

init process is the first process in linux, it start the executable "app_process", whose internal is :

(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  

Then here is the most important java method: ZygoteInit.main, we get here from the above native code "env->CallStaticVoidMethod ".

This is also the first method in the call stack when you set a break point in main Activity's onCreate and start debuging your application and break there. But in fact your application never get to the beginning of ZygoteInit.main, it is executed from the beginning only in app_process(or say 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();

When you start an Activity, finally you get into in Activity Manager Service(AMS, which is in system server process). If the process for that Activity is not created yet, AMS will send process-start request request to Zygote server (in the zygote process described above,started by init process), the process is like:

//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

At this time, the above listed Zygote code will wake up from

Os.poll(pollFds, -1);

and fork the child process, and after this, the parent process i.e. Zygote will again execute poll waiting for the next request, and the forked child process will return from runSelectLoop and execute ActivityThread.main as described in the above code listing.

So the exact entry point of the new process will be after the native fork() deep down in Zygote.forkAndSpecialize, in a native function called ForkAndSpecializeCommon to be exactly, then all the way up through a return route up to

caller = zygoteServer.runSelectLoop

in ZygoteInit.main. So although the call stack of an Android application start at ZygoteInit.main, the code executed in ZygoteInit.main begins after the call to runSelectLoop instead of the beginning of ZygoteInit.main.

About Activity: in fact Activity has nothing to do with entry point or start up process. An Activity is started when AMS send an Activity-start request to a process at any time. So Activity start process always begin in the main message loop when the start request is received, it is driven by message from AMS and is fully decoupled from the application start process.

Imposition answered 22/10, 2019 at 13:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.