参考文献:https://blog.csdn.net/luoshengyang/java/article/details/6568411 //罗升阳博客
Android_10.0.0_r41自定义驱动Framework JNI调用
因为android10之后已不再支持HDIL调用,故仅支持此jni调用
1.实现JNI
在frameworks/base/services/core/jni/
目录新建com_android_server_HelloService.cpp
文件: 实现JNI方法。
#define LOG_TAG "HelloService" #include "jni.h" #include <nativehelper/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> // 定义hello_init、 hello_getVal 和 hello_setVal 三个JNI方法: 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 方法表*/ // java和native的方法对应表,第一个是java中的方法, //第二个是参数(括号中为参数)和返回值,含义为:[B -- byte数组,Z -- bool, I -- int, V -- void, // Ljava/lang/String; -- String对象, [Ljava/lang/String; -- String对象数组 //第三个对应的是native中的函数 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方法*/ // 第二个参数的值必须对应HelloService所在的包的路径,即com.android.server.HelloService int register_android_server_HelloService(JNIEnv* env) { return jniRegisterNativeMethods(env, "com/android/server/hello/HelloManagerService", method_table, NELEM(method_table)); } }
2.同时在此文件夹下修改onload.cpp文件
namespace android { .........添加一 int register_android_server_HelloService(JNIEnv *env); }; extern "C" jint JNI_OnLoad(JavaVM* vm, void* /* reserved */) { ............添加二 register_android_server_HelloService(env); // 这样,在Android系统初始化时,就会自动加载该JNI方法调用表 return JNI_VERSION_1_4; }
3.修改同目录下的Android.bp文件
srcs: [ ********************** "com_android_server_HelloService.cpp", //添加这一行 "onload.cpp", ":lib_networkStatsFactory_native", ],
4.编译,单模块编译如下:
root@ubuntu:frameworks/base/services/core/jni$ mm -j16