I have been studying the implementation of Dalvik. Today I suddenly think of one thing. We can use java by default. lang. when is the system class such as Object loaded? Can our own class be loaded in advance like this?
Following this idea, let's review the previous process of starting Zygote and explore it!
Zygote Startup Process (many analyses on the Internet are not detailed ):
>>> Start Kernel
>>> Start the init program, which parses init. rc for execution.
>>> Start zygote code in init. rc
>>> After zygote is started, fork will output system_server for system service.
>>> Zygote waits for the socket event to prepare to hatch the user process
The following key calls are available at Zygote startup:
AppRuntime. start com. android. internal. OS. ZygoteInit class Processing
Now that the java class can be called for processing, which system basic class must be ready before this? Let's look at it.
AppRuntime inherits AndroidRuntime
The start function of AndroidRuntime calls the JNI_CreateJavaVM function to generate a virtual machine.
JNI_CreateJavaVM is a function in jni. c, that is, libdvm. so.
In JNI_CreateJavaVM, The dvmStartup function is called to start a virtual machine.
DvmStartup calls setCommandLineDefaults to implement the default value of the parameter. The BOOTCLASSPATH method is the most important method to get the environment change.
Looking back at init. rc
export BOOTCLASSPATH /system/framework/core.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/android.policy.jar:/system/framework/services.jar:/system/framework/core-junit.jar
It turns out that some basic class jar packages are specified here
Continue viewing code
In dvmStartup, setCommandLineDefaults is called to set the default BOOTCLASSPATH.
DvmStartup calls dvmClassStartup to load the default startup class.
DvmClassStartup calls the processClassPath processing class path, which is actually the value in BOOTCLASSPATH.
ClassPathEntry is created in processClassPath to store a path.
PrepareCpe is called in processClassPath to prepare the class path node.
In prepareCpe, dvmJarFileOpen is called to open a jar package.
All loaded Jar packages are stored in gDvm. bootClassPath.
GDvm. bootClassPath is represented by a special struct at the end of the object. kind = kCpeLastEntry indicates the last class path node, because some variables do not need to represent the size.
Probably clear!
-------------------------------------- Boring split line --------------------------------------
Which of the following methods can we add a basic package for the system:
- Change the initial value of the BOOTCLASSPATH environment variable of init. rc.
- Change the BOOTCLASSPATH environment variable before zygote starts.
- After zygote is started, try to load a jar package.
At this point, we found that the system starts and starts, and all the implementation is scalable, so it is easy to code without hard coding.
We will continue to study loading of other things in the future. For example, an interesting apk (ramework-res.apk) is a pack implemented by the system skin.