android art虚拟机安装,Android中art虚拟机启动流程
本文基于A(yíng)ndroid8.1系統(tǒng)進(jìn)行研究
一、啟動(dòng)zygote
在Linux內(nèi)核啟動(dòng)完成后,首先啟動(dòng)系統(tǒng)的第一個(gè)進(jìn)程init進(jìn)程
init進(jìn)程會(huì)讀取init.rc中的配置文件
其中有Zygote的配置,init進(jìn)程將啟動(dòng)zygote進(jìn)程
zygote的入口在app_main.cpp中的main函數(shù)中
二、解析傳入?yún)?shù),調(diào)用AndroidRuntime的start方法
在app_main.cpp中的main函數(shù)中,首先解析傳入的相關(guān)參數(shù),并通過(guò)如下代碼進(jìn)入ZygoteInit的main函數(shù)中
Java
if (zygote) {
runtime.start("com.android.internal.os.ZygoteInit", args, zygote);
}
1
2
3
if(zygote){
runtime.start("com.android.internal.os.ZygoteInit",args,zygote);
}
runtime是AppRuntime的一個(gè)實(shí)例,AppRuntim繼承自AndroidRuntime
start函數(shù)定義在A(yíng)ndroidRuntime中,下面是start函數(shù)的注釋
/*
* Start the Android runtime. This involves starting the virtual machine
* and calling the "static void main(String[] args)" method in the class
* named by "className".
*
* Passes the main function two arguments, the class name and the specified
* options string.
*
* 翻譯:
* 啟動(dòng)Android運(yùn)行時(shí),包括啟動(dòng)虛擬機(jī),調(diào)用className參數(shù)對(duì)應(yīng)的main函數(shù)
*/
1
2
3
4
5
6
7
8
9
10
11
/*
* Start the Android runtime.??This involves starting the virtual machine
* and calling the "static void main(String[] args)" method in the class
* named by "className".
*
* Passes the main function two arguments, the class name and the specified
* options string.
*
* 翻譯:
* 啟動(dòng)Android運(yùn)行時(shí),包括啟動(dòng)虛擬機(jī),調(diào)用className參數(shù)對(duì)應(yīng)的main函數(shù)
*/
start函數(shù)中的這段代碼切入到了startVm函數(shù)中
C++
/* start the virtual machine */
JniInvocation jni_invocation;
jni_invocation.Init(NULL);
JNIEnv* env;
if (startVm(&mJavaVM, &env, zygote) != 0) {
return;
}
onVmCreated(env);
1
2
3
4
5
6
7
8
/* start the virtual machine */
JniInvocationjni_invocation;
jni_invocation.Init(NULL);
JNIEnv*env;
if(startVm(&mJavaVM,&env,zygote)!=0){
return;
}
onVmCreated(env);
三、startVm函數(shù)
這個(gè)函數(shù)前邊也是一堆處理Vm啟動(dòng)參數(shù)的邏輯
在函數(shù)的最好調(diào)用JNI_CreateJavaVM函數(shù)通過(guò)c++層的庫(kù)創(chuàng)建虛擬機(jī)
四、JNI_CreateJavaVM函數(shù)
JNI_CreateJavaVM函數(shù)是在art/runtime/java_vm_ext.cc中實(shí)現(xiàn)的,代碼如下:
C++
// JNI Invocation interface.
extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
ScopedTrace trace(__FUNCTION__);
const JavaVMInitArgs* args = static_cast(vm_args);
if (JavaVMExt::IsBadJniVersion(args->version)) {
LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
return JNI_EVERSION;
}
RuntimeOptions options;
for (int i = 0; i < args->nOptions; ++i) {
JavaVMOption* option = &args->options[i];
options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
}
bool ignore_unrecognized = args->ignoreUnrecognized;
if (!Runtime::Create(options, ignore_unrecognized)) {
return JNI_ERR;
}
// Initialize native loader. This step makes sure we have
// everything set up before we start using JNI.
android::InitializeNativeLoader();
Runtime* runtime = Runtime::Current();
bool started = runtime->Start();
if (!started) {
delete Thread::Current()->GetJniEnv();
delete runtime->GetJavaVM();
LOG(WARNING) << "CreateJavaVM failed";
return JNI_ERR;
}
*p_env = Thread::Current()->GetJniEnv();
*p_vm = runtime->GetJavaVM();
return JNI_OK;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// JNI Invocation interface.
extern"C"jintJNI_CreateJavaVM(JavaVM**p_vm,JNIEnv**p_env,void*vm_args){
ScopedTracetrace(__FUNCTION__);
constJavaVMInitArgs*args=static_cast(vm_args);
if(JavaVMExt::IsBadJniVersion(args->version)){
LOG(ERROR)<version;
returnJNI_EVERSION;
}
RuntimeOptionsoptions;
for(inti=0;inOptions;++i){
JavaVMOption*option=&args->options[i];
options.push_back(std::make_pair(std::string(option->optionString),option->extraInfo));
}
boolignore_unrecognized=args->ignoreUnrecognized;
if(!Runtime::Create(options,ignore_unrecognized)){
returnJNI_ERR;
}
// Initialize native loader. This step makes sure we have
// everything set up before we start using JNI.
android::InitializeNativeLoader();
Runtime*runtime=Runtime::Current();
boolstarted=runtime->Start();
if(!started){
deleteThread::Current()->GetJniEnv();
deleteruntime->GetJavaVM();
LOG(WARNING)<
returnJNI_ERR;
}
*p_env=Thread::Current()->GetJniEnv();
*p_vm=runtime->GetJavaVM();
returnJNI_OK;
}
Runtime是在art/runtime/runtime.h中定義的類(lèi)
Runtime::Current()返回一個(gè)靜態(tài)的Runtime實(shí)例,代碼如下:
static Runtime* instance_;
1
staticRuntime*instance_;
runtime-Start()啟動(dòng)虛擬機(jī)
五、其他參考文章
Android ART運(yùn)行時(shí)無(wú)縫替換Dalvik虛擬機(jī)的過(guò)程分析
Android虛擬機(jī)art流程:從zygote開(kāi)始梳理art的啟動(dòng)(1)
打賞
微信掃一掃,打賞作者吧~
總結(jié)
以上是生活随笔為你收集整理的android art虚拟机安装,Android中art虚拟机启动流程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。