Android虚拟机的启动 源码分析

frameworks/base/core/java/com/android/internal/os/ZygoteInit.java#main()是怎么执行的 ? 这需要从虚拟机启动的执行开始说起。

虚拟机启动的入口从frameworks/base/core/jni/AndroidRuntime.cpp#start方法开始

void AndroidRuntime::start(const char* className, const Vector<String8>& options)
{
	jni_invocation.Init(NULL); //初始化JNI
	JNIEnv* env; //JNI环境指针
	if (startVm(&mJavaVM, &env) != 0) { //启动一个虚拟机,会给mJavaVM、env赋值
	    return;
	}
	onVmCreated(env); //虚拟机Created
	
	//注册android的一些函数
	if (startReg(env) < 0) {
	    ALOGE("Unable to register all android natives\n");
	    return;
	}
	//...
	//通过env->FindClass 加载String字符串
	stringClass = env->FindClass("java/lang/String"); 
    assert(stringClass != NULL);
    strArray = env->NewObjectArray(options.size() + 1, stringClass, NULL);
    assert(strArray != NULL);
    classNameStr = env->NewStringUTF(className);
    assert(classNameStr != NULL);
    env->SetObjectArrayElement(strArray, 0, classNameStr);
 
 	//className是方法传参过来的,toSlashClassName是个转换的方法,把点变成斜杠之类的
	char* slashClassName = toSlashClassName(className); 
	//找到frameworks/base/core/java/com/android/internal/os/ZygoteInit.java这个类
	jclass startClass = env->FindClass(slashClassName); 
	if (startClass == NULL) {
	        ALOGE("JavaVM unable to locate class '%s'\n", slashClassName);
	        /* keep going */
	    } else {
	        jmethodID startMeth = env->GetStaticMethodID(startClass, "main",
	            "([Ljava/lang/String;)V"); //找ZygoteInit的main方法,其中jmethodID就是ArtMethod
	        if (startMeth == NULL) {
	            ALOGE("JavaVM unable to find main() in '%s'\n", className);
	            /* keep going */
	        } else {
	            env->CallStaticVoidMethod(startClass, startMeth, strArray); //传入类、方法、参数,通过反射调用ZygoteInit的main方法
            }
        }
    }
    //...
}

小结

可以看到,Android虚拟机的启动是在AndroidRuntime.cpp#start()方法。

  1. 首先会初始化JNI
  2. 然后启动一个虚拟机
  3. 找到ZygoteInit.java这个类
  4. 找ZygoteInit的main方法
  5. 传入类、方法、参数,通过反射调用ZygoteInit的main方法
其他

源码为Android 5.1.0_r3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Android虚拟机Dalvik完整源码,宝贵资源,欢迎下载! This directory contains the Dalvik virtual machine and core class library, as well as related tools, libraries, and tests. A note about the licenses and header comments --------------------------------------------- Much of the code under this directory originally came from the Apache Harmony project, and as such contains the standard Apache header comment. Some of the code was written originally for the Android project, and as such contains the standard Android header comment. Some files contain code from both projects. In these cases, the header comment is a combination of the other two, and the portions of the code from Harmony are identified as indicated in the comment. Here is the combined header comment: /* * Copyright (C) The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * ---------- * * Portions of the code surrounded by "// BEGIN Harmony code" and * "// END Harmony code" are copyrighted and licensed separately, as * follows: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Native SH call bridge --------------------- Native SH call bridge is written by Shin-ichiro KAWASAKI and Contributed to Android by Hitachi, Ltd. and Renesas Solutions Corp.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

氦客

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值