The Zygote Process starts the Android Runtime with the following command.
The code for this is at frameworks/base/core/jni/AndroidRuntime.cpp
runtime.start("com.android.internal.os.ZygoteInit", args, zygote);
This involves starting the virtual machine and calling the "static void main(String[] args)" method of ZygoteInit. Event Log for Boot Progrsess Start is printed here.
Steps:
jni_invocation.Init(NULL);
-Jni_invocation: adds a layer of indirection for applications using the JNI invocation API to allow the JNI implementation to be selected dynamically. Apps can specify a specific implementation to be used by calling InitJniInvocation.
If this is not done, the library will chosen based on the value of Android system property "persist.sys.dalvik.vm.lib"(say libart.so, libdvm.so or libartd.so) on the device, and otherwise fall back to a hard-coded default implementation ("libart.so").
Get The Java VM init properties from the dalvik.vm.xxx system properties and initialize Java VM.
We can call jni methods once this call succeeds
JNI_CreateJavaVM(pJavaVM, pEnv, &initArgs)
@/art/runtime/java_vm_ext.cc -> JNI_CreateJavaVM
Runtime* runtime = Runtime::Current();
runtime->Start();
pEnv = Thread::Current()->GetJniEnv();
pJavaVM = runtime->GetJavaVM();
pEnv is per Thread & pJavaVM is per process
This registers all Native system services like
register_com_android_internal_os_RuntimeInit
register_android_os_SystemClock
register_android_os_Process
register_android_view_Surface
register_android_graphics_Camera
register_android_view_InputChannel
etc...
Finally call the main() of the "startClass", In our case this is ZygoteInit
jmethodID startMeth = env->GetStaticMethodID(startClass, "main", "([Ljava/lang/String;)V");
env->CallStaticVoidMethod(startClass, startMeth, strArray);
The code for this is at frameworks/base/core/jni/AndroidRuntime.cpp
runtime.start("com.android.internal.os.ZygoteInit", args, zygote);
This involves starting the virtual machine and calling the "static void main(String[] args)" method of ZygoteInit. Event Log for Boot Progrsess Start is printed here.
Steps:
Check is /system/ directory is available
Initialize Jni_invocation
jni_invocation.Init(NULL);
-Jni_invocation: adds a layer of indirection for applications using the JNI invocation API to allow the JNI implementation to be selected dynamically. Apps can specify a specific implementation to be used by calling InitJniInvocation.
If this is not done, the library will chosen based on the value of Android system property "persist.sys.dalvik.vm.lib"(say libart.so, libdvm.so or libartd.so) on the device, and otherwise fall back to a hard-coded default implementation ("libart.so").
StartVM
startVm(&mJavaVM, &env, zygote)Get The Java VM init properties from the dalvik.vm.xxx system properties and initialize Java VM.
We can call jni methods once this call succeeds
JNI_CreateJavaVM(pJavaVM, pEnv, &initArgs)
@/art/runtime/java_vm_ext.cc -> JNI_CreateJavaVM
Runtime* runtime = Runtime::Current();
runtime->Start();
pEnv = Thread::Current()->GetJniEnv();
pJavaVM = runtime->GetJavaVM();
pEnv is per Thread & pJavaVM is per process
Register all native android functions.
startReg(env)This registers all Native system services like
register_com_android_internal_os_RuntimeInit
register_android_os_SystemClock
register_android_os_Process
register_android_view_Surface
register_android_graphics_Camera
register_android_view_InputChannel
etc...
Finally call the main() of the "startClass", In our case this is ZygoteInit
jmethodID startMeth = env->GetStaticMethodID(startClass, "main", "([Ljava/lang/String;)V");
env->CallStaticVoidMethod(startClass, startMeth, strArray);
Clearly explained with simple source code. I enjoyed with your blog.
ReplyDeleteHave a look at Way2Smile for any Android App Development needs or suggestion.
Hi LostCauses, informative one for all Android Developers. your source code can easily understandable even for beginners.
ReplyDelete- Vicky from Devolve (Trusted Mobile Application Development Company in Calgary).
Thanks for enlightening us with the Android runtime source codes. This is easy to understand.
ReplyDeleteBest Regards - Vigneshwaran P ( Android App Development Company )