Android6.0中的NativeBridge

本文详细介绍了Android 6.0中NativeBridge的实现过程,包括其初始化、功能以及如何在应用程序启动时启用。NativeBridge是一个中间模块,用于在JNI调用时进行动态转码,确保在不同指令集之间的兼容性。文章还探讨了NativeBridge的初始化、加载过程以及在Zygote进程和应用程序进程中的使用条件。
摘要由CSDN通过智能技术生成

主要参考: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   
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值