第九期 基于模拟器的Helloworld JNI方法 《手机就是开发板》

来源:互联网 发布:五子棋雾化器数据 编辑:程序博客网 时间:2024/05/18 21:05
这一期我们来写一个JNI方法,JNI是Java Native Interface的缩写,它提供了若干的API实现了Java和其他语言的通信(主要是C&C++),JNI是Java调用Native机制,是Java语言自己的特性全称为Java Native Interface,类似的还有微软.Net Framework上的p/invoke,可以让C#或Visual Basic.NET可以调用C/C++的API。使用java与本地已编译的代码交互,通常会丧失平台可移植性。但是想android这种必须与硬件打交道的系统使用JNI时必须的。
Java通过JNI机制和C/C++沟通的具体步骤
1、编写包含native本地方法的java类;
2、通过javah工具生成C/C++语言的头文件;
3、使用C/C++语言实现头文件;
4、使用交叉编译工具对C/C++本地代码进行编译,最后通过链接生成*.so可执行的C/C++库;
5、实际执行Java代码去和本地的C/C++代码互相沟通。
上面的步骤是通过JAVA建立本地代码的过程,而我们的学习路径是从底层到顶层,所以只要规则能对应上,也是殊途同归。
下面提到的代码保存在https://github.com/aggresss/PHDemo.git 的Code目录的hello_JNI文件中,也可以直接访问:
https://github.com/aggresss/PHDemo/tree/master/Code/hello_JNI

进入 frameworks/base/services/core/jni 目录,新建com_android_server_HelloService.cpp文件:
#define LOG_TAG "HelloService"  #include "jni.h"  #include "JNIHelp.h"  #include "android_runtime/AndroidRuntime.h"  #include <utils/misc.h>  #include <utils/Log.h>  #include <hardware/hardware.h>  #include <hardware/hello.h>  #include <stdio.h>  namespace android  {      /*在硬件抽象层中定义的硬件访问结构体,参考<hardware/hello.h>*/          struct hello_device_t* hello_device = NULL;      /*通过硬件抽象层定义的硬件访问接口设置硬件寄存器val的值*/          static void hello_setVal(JNIEnv* env, jobject clazz, jint value) {          int val = value;          ALOGI("Hello JNI: set value %d to device.", val);          if(!hello_device) {              ALOGI("Hello JNI: device is not open.");              return;          }                    hello_device->set_val(hello_device, val);      }          /*通过硬件抽象层定义的硬件访问接口读取硬件寄存器val的值*/      static jint hello_getVal(JNIEnv* env, jobject clazz) {          int val = 0;          if(!hello_device) {              ALOGI("Hello JNI: device is not open.");              return val;          }          hello_device->get_val(hello_device, &val);                    ALOGI("Hello JNI: get value %d from device.", val);                return val;      }          /*通过硬件抽象层定义的硬件模块打开接口打开硬件设备*/      static inline int hello_device_open(const hw_module_t* module, struct hello_device_t** device) {          return module->methods->open(module, HELLO_HARDWARE_MODULE_ID, (struct hw_device_t**)device);      }          /*通过硬件模块ID来加载指定的硬件抽象层模块并打开硬件*/      static jboolean hello_init(JNIEnv* env, jclass clazz) {          hello_module_t* module;                    ALOGI("Hello JNI: initializing......");          if(hw_get_module(HELLO_HARDWARE_MODULE_ID, (const struct hw_module_t**)&module) == 0) {              ALOGI("Hello JNI: hello Stub found.");              if(hello_device_open(&(module->common), &hello_device) == 0) {                  ALOGI("Hello JNI: hello device is open.");                  return 0;              }              ALOGE("Hello JNI: failed to open hello device.");              return -1;          }          ALOGE("Hello JNI: failed to get hello stub module.");          return -1;            }          /*JNI方法表*/      static const JNINativeMethod method_table[] = {          {"init_native", "()Z", (void*)hello_init},          {"setVal_native", "(I)V", (void*)hello_setVal},          {"getVal_native", "()I", (void*)hello_getVal},      };          /*注册JNI方法*/      int register_android_server_HelloService(JNIEnv *env) {              return jniRegisterNativeMethods(env, "com/android/server/HelloService", method_table, NELEM(method_table));      }  };  
修改同目录下的onload.cpp文件,首先在namespace android增加register_android_server_HelloService函数声明:
  namespace android {      ..............................................................................................      int register_android_server_HelloService(JNIEnv *env);      };
在JNI_onLoad增加register_android_server_HelloService函数调用:
   extern "C" jint JNI_onLoad(JavaVM* vm, void* reserved)      {       .................................................................................................       register_android_server_HelloService(env);       .................................................................................................      }
这样,在Android系统初始化时,就会自动加载该JNI方法调用表。
修改同目录下的Android.mk文件,在LOCAL_SRC_FILES变量中增加一行:
LOCAL_SRC_FILES:= \      com_android_server_AlarmManagerService.cpp \      com_android_server_BatteryService.cpp \      com_android_server_InputManager.cpp \      com_android_server_LightsService.cpp \      com_android_server_PowerManagerService.cpp \      com_android_server_SystemServer.cpp \      com_android_server_UsbService.cpp \      com_android_server_VibratorService.cpp \      com_android_server_location_GpsLocationProvider.cpp \      com_android_server_HelloService.cpp \      onload.cpp
在AOSP根目录下执行 mmm frameworks/base/ 重新编译;
然后 make snod 重新生成system.img。
因为JNI方法需要配合HelloService的java类写的service才能进行验证,所以我们和下一期一期进行验证,到这里JNI方法已经添加成功。
0 0
原创粉丝点击