可以看到,REG_JNI是一个宏,宏里面包括的就是那个参数为JNIEnv*,返回值为int的函数指针mProc,我们以register_android_debug_JNITest为例,源码位置为/frameworks/base/core/jni/android_debug_JNITest.cpp:
- #define NELEM(x) (sizeof(x)/sizeof(*(x)))
- int register_android_debug_JNITest(JNIEnv* env) {
- return jniRegisterNativeMethods(env, "android/debug/JNITest", gMethods, NELEM(gMethods)); }
可以看到,mProc其实就是为Java类注册JNI函数。
进入JAVA世界
可以看到CallStaticVoidMethod最终将调用com.android.internal.os.ZygoteInit的main函数,下面就来看一下这个Java世界的入口函数。源码位置:/frameworks/base/core/java/com/android/internal/os/ZygoteInit.java,源码如下:
- public static void main(String argv[]) {
- try { SamplingProfilerIntegration.start();
- // 1. 注册zygote用的socket
- registerZygoteSocket(); EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_START, SystemClock.uptimeMillis());
- // 2. 预加载类和资源
- preload(); EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_END, SystemClock.uptimeMillis());
- SamplingProfilerIntegration.writeZygoteSnapshot();
- // 强制执行一次垃圾收集
- gc();
- Trace.setTracingEnabled(false);
- if (argv.length != 2) { throw new RuntimeException(argv[0] + USAGE_STRING);










