Android4.0.3源碼分析——開機流程之Zygote

來源:互聯網
上載者:User

Zygote

 


Zygote啟動是從

/frameworks/base/cmds/app_process/app_main.cpp

中的main()函數開始的。

 


啟動JavaVM:

main()函數中有啟動VM:

if(zygote) {

runtime.start("com.android.internal.os.ZygoteInit",

startSystemServer? "start-system-server" : "");

而runtime是AppRuntime的對象,同樣在main()中:

AppRuntimeruntime;

而appRuntime是AndroidRuntime的子類,它的定義就在app_main.cpp中:

classAppRuntime : public AndroidRuntime

所以,這裡的runtime.start()其實調用的是AndroidRuntime::start().

 


下面對AndroidRuntime::start()進行分析:

/frameworks/base/core/jni/AndroidRuntime.cpp

start()函數中啟動VM:

    /* start the virtual machine */
    JNIEnv* env;
    if (startVm(&mJavaVM, &env) != 0) {
        return;
    }
    onVmCreated(env);
其中startVm()函數和onVmCreated()函數也都在AndroidRuntime.cpp中。
startVm()函數中通過有如下代碼調用JNI建立JavaVM:
/*
* Initialize the VM.
*
* The JavaVM* is essentially per-process, and the JNIEnv* is per-thread.
* If this call succeeds, the VM is ready, and we can start issuing
* JNI calls.
*/
if (JNI_CreateJavaVM(pJavaVM, pEnv, &initArgs) < 0) {
LOGE("JNI_CreateJavaVM failed\n");
goto bail;
}
onVmCreated()函數其實是個空函數。
void AndroidRuntime::onVmCreated(JNIEnv* env)
{
// If AndroidRuntime had anything to do here, we'd have done it in 'start'.
}
同時在start()函數中調用startReg()函數註冊JNI介面:
    if (startReg(env) < 0) {
        LOGE("Unable to register all android natives\n");
        return;
    }
然後在start()函數中:

env->CallStaticVoidMethod(startClass,startMeth, strArray);

調用com.android.internal.os.ZygoteInit中的main()函數。

 


/fameworks/base/core/java/com/android/internal/os/ZygoteInit.java

main()調用了:

registerZygoteSocket();//來註冊Socket的Listen連接埠,用來接受請求。

preload();

startSystemServer();//啟動SystemServer。

其中preload()函數定義如下:

static void preload() {
    preloadClasses();
    preloadResources();
}
它主要進行預先載入類和資源,以加快啟動速度。preload的class列表儲存在/frameworks/base/preloaded-classes檔案中。

經過以上步驟,Zygote就建立完成。

 

作者:snsn1984

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.