主要参考:http://blog.csdn.net/roland_sun/article/details/49688311 一文,换成了Android6.0的实现过程。
从Android从5.0开始,在其ART虚拟机的实现中,引入了一个叫做NativeBridge的中间模块。这个模块基本上就是为了在JNI调用时进行动态转码用的,自带了基本上所有的处理逻辑。
首先,我们来看看NativeBridge的初始化过程。大家知道,ART虚拟机的初始化是在Runtime::Init函数中进行的,在这个函数的最后,有下面这行代码(代码位于art\runtime\runtime.cc文件中):
782 bool Runtime::Init(const RuntimeOptions& raw_options, bool ignore_unrecognized) {
...
1088 // Look for a native bridge.
1089 //
1090 // The intended flow here is, in the case of a running system:
1091 //
1092 // Runtime::Init() (zygote):
1093 // LoadNativeBridge -> dlopen from cmd line parameter.
1094 // |
1095 // V
1096 // Runtime::Start() (zygote):
1097 // No-op wrt native bridge.
1098 // |
1099 // | start app
1100 // V
1101 // DidForkFromZygote(action)
1102 // action = kUnload -> dlclose native bridge.
1103 // action = kInitialize -> initialize library
1104 //
1105 //
1106 // The intended flow here is, in the case of a simple dalvikvm call:
1107 //
1108 // Runtime::Init():
1109 // LoadNativeBridge -> dlopen from cmd line parameter.
1110 // |
1111 // V
1112 // Runtime::Start():
1113 // DidForkFromZygote(kInitialize) -> try to initialize any native bridge given.
1114 // No-op wrt native bridge.
1115 {
1116 std::string native_bridge_file_name = runtime_options.ReleaseOrDefault(Opt::NativeBridge);
1117 is_native_bridge_loaded_ = LoadNativeBridge(native_bridge_file_name);
1118 }
...
1124 return true;
1125 }
可以看到在初始化的最后,调用了LoadNativeBridge函数,并传入了一个文件名作为参数。
LoadNativeBridge函数的实现如下:
106 bool LoadNativeBridge(std::string& native_bridge_library_filename) {
107 VLOG(startup) << "Runtime::Setup native bridge library: "
108 << (native_bridge_library_filename.empty() ? "(empty)" : native_bridge_library_filename);
109 return android::LoadNativeBridge(native_bridge_library_filename.c_str(),
110 &native_bridge_art_callbacks_);
111 }
其中直接调用了android::LoadNativeBridge函数,但是多了一个参数native_bridge_art_callbacks_,看名字应该可以猜出来是一组回调函数表。而其中的多增加的参数的含义如下:
93 // Native bridge library runtime callbacks. They represent the runtime interface to native bridge.
94 //
95 // The interface is expected to expose the following methods:
96 // getMethodShorty(): in the case of native method calling JNI native function CallXXXXMethodY(),
97 // native bridge calls back to VM for the shorty of the method so that it can prepare based on
98 // host calling convention.
99 // getNativeMethodCount() and getNativeMethods(): in case of JNI function UnregisterNatives(),
100 // native bridge can call back to get all native methods of specified class so that all
101 // corresponding trampolines can be destroyed.
102 static android::NativeBridgeRuntimeCallbacks native_bridge_art_callbacks_ {
103 GetMethodShorty, GetNativeMethodCount, GetNativeMethods
104 };
而NativeBridgeRuntimeCallbacks的定义如下:
//~/android-6.0.1_r62/system/core/include/nativebridge/native_bridge.h中
175 struct NativeBridgeRuntimeCallbacks {
176 // Get shorty of a Java method. The shorty is supposed to be persistent in memory.
177 //
178 // Parameters:
179 // env [IN] pointer to JNIenv.
180 // mid [IN] Java methodID.
181 // Returns:
182 // short descriptor for method.
183