JNI笔记
frameworks/base/core/jni/
被编译成库libandroid_runtime.so
一般JNI的调用流程
JAVA程序 -> JAVA程序中声明的本地接口 -> JNI函数(在JNI文件中) -> JNI所需要的库。
jni最好的的例子是下载的ndk的samples中自带的hellojni,如果无法运行就有可能是设置ndk路径的问题,从ndk8开始就不用mingw了。
找不到ndk
1、如果Eclipse中没有可以设置ndk路径的选项,说明对应eclipse ndk工具包没有更新1、1、通过Eclipse的help菜单中的install new software选项,输入https://dl-ssl.google.com/android/eclipse/尝试更新
2、如果还是不行可以上网下载别人更新好的接口包
3、如果都不行,那就只能通过设置环境变量,例如设置个NDKROOT,里面添加你NDK的路径,然后在jni工程右键,设置C/C++编译器地址的时候,选择NDKROOT环境变量。
Eclipse中jni无法编译
1、可能需要右键Add native libray
2、prject->clean
3、重启eclipse
Eclipse中jni使用其他第三方库
本机基于Android2.3.3 x86平台,可以将第三方库放入以下目录:
android-ndk-r9c\platforms\android-9\arch-x86\usr\lib
引入函数库方法 :
在JNI文件下Android.mk中使用 LOCAL_LDLIBS += -l函数库名, 注意函数库名不带lib前缀 和.so 后缀, 同时可以添加多个库, 使用 -l库1 -l库2 -库3。类似:
LOCAL_LDLIBS := -ladd add为库,真正完整的库名是libadd.so。
HAL笔记
Android为了不公开硬件驱动模块源代码特意提出了HAL层,当然这样也让Android FrameWork层与Llinu相对更加独立。这样Linux只是实现基本的硬件驱动,硬件的驱动逻辑主要是在HAL中实现。HAL的方法主要有两种。
第一种是直接调用HAL的*.so共享库。
第二种采用stup的方式,调用HAL中的各个操作函数。(这种是正式的方式,也是google推荐的方式)。
Android HAL实现的源码路径:
hardware/libhardware_legacy
hardware/libhardware
这里主要介绍第二种方法(其实第一种方法就是类似于jni的直接调用,少了个stub的概念)。
采用stup的方式,调用HAL中的各个操作函数:
HAL Module是以*.so形式存在的,但是应用不是直接装载库,而是通过stub的方式获取HAL提供的各种操作,然后Runtime则通过HAL得到HAL Module的stub的operations,然后callback(返回操作)。HAL stub是通过回调函数间接的调用函数。
HAL的实现
相关源码
hardware/libhardware/include/hardware/hardware.h
hardware/libhardware/hardware.c
HAL中三个关键的结构体(定义位于hardware)
struct hw_device_t
struct hw_module_t
struct hw_module_methods_t
通过查看HAL源码的实现,可以发现其调用路径如下:
hw_get_module()->property_get()->__property_get()->__system_property_get()->__system_property_find()->__system_property_read()->load()->snprintf()->dlopen()->dlsym()
从中可以发现HAL是通过调用C++的dl库来打开库文件,并dlsym来赋予函数指针(同理,如果我们缺少标准HAL库,但是又想引用某些so库,就可以采用这种方法)。
一个简单的标准HAL模块
可以仿照源码中自带的HAL模块
hardware/libhardware/include/hardware目录下新建hello.h
#ifndef ANDROID_HELLO_INTERFACE_H
#define ANDROID_HELLO_INTERFACE_H
#include <hardware/hardware.h>
__BEGIN_DECLS
/*定义模块ID*/
#define HELLO_HARDWARE_MODULE_ID "hello"
/*硬件模块结构体*/
struct hello_module_t {
struct hw_module_t common;
};
/*硬件接口结构体*/
struct hello_device_t {
struct hw_device_t common;
int fd;
int (*set_val)(struct hello_device_t* dev, int val);
int (*get_val)(struct hello_device_t* dev, int* val);
};
__END_DECLS
#endif
hardware/libhardware/modules目录下新建hello目录,并在其中新建hello.c
#define LOG_TAG "HelloStub"
#include <hardware/hardware.h>
#include <hardware/hello.h>
#include <fcntl.h>
#include <errno.h>
#include <cutils/log.h>
#include <cutils/atomic.h>
#define DEVICE_NAME "/dev/hello"
#define MODULE_NAME "Hello"
#define MODULE_AUTHOR "shyluo@gmail.com"
/*设备打开和关闭接口*/
static int hello_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device);
static int hello_device_close(struct hw_device_t* device);
/*设备访问接口*/
static int hello_set_val(struct hello_device_t* dev, int val);
static int hello_get_val(struct hello_device_t* dev, int* val);
/*模块方法表*/
static struct hw_module_methods_t hello_module_methods = {
open: hello_device_open
};
/*模块实例变量*/
struct hello_module_t HAL_MODULE_INFO_SYM = {
common: {
tag: HARDWARE_MODULE_TAG,
version_major: 1,
version_minor: 0,
id: HELLO_HARDWARE_MODULE_ID,
name: MODULE_NAME,
author: MODULE_AUTHOR,
methods: &hello_module_methods,
}
};
static int hello_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device) {
struct hello_device_t* dev;dev = (struct hello_device_t*)malloc(sizeof(struct hello_device_t));
if(!dev) {
LOGE("Hello Stub: failed to alloc space");
return -EFAULT;
}
memset(dev, 0, sizeof(struct hello_device_t));
dev->common.tag = HARDWARE_DEVICE_TAG;
dev->common.version = 0;
dev->common.module = (hw_module_t*)module;
dev->common.close = hello_device_close;
dev->set_val = hello_set_val;dev->get_val = hello_get_val;
if((dev->fd = open(DEVICE_NAME, O_RDWR)) == -1) {
LOGE("Hello Stub: failed to open /dev/hello -- %s.", strerror(errno));free(dev);
return -EFAULT;
}
*device = &(dev->common);
LOGI("Hello Stub: open /dev/hello successfully.");
return 0;
}
static int hello_device_close(struct hw_device_t* device) {
struct hello_device_t* hello_device = (struct hello_device_t*)device;
if(hello_device) {
close(hello_device->fd);
free(hello_device);
}
return 0;
}
static int hello_set_val(struct hello_device_t* dev, int val) {
LOGI("Hello Stub: set value %d to device.", val);
write(dev->fd, &val, sizeof(val));
return 0;
}
static int hello_get_val(struct hello_device_t* dev, int* val) {
if(!val) {
LOGE("Hello Stub: error val pointer");
return -EFAULT;
}
read(dev->fd, val, sizeof(*val));
LOGI("Hello Stub: get value %d from device", *val);
return 0;
}
继续在hello目录下新建Android.mk文件:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_PRELINK_MODULE := false
LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
LOCAL_SHARED_LIBRARIES := liblog
LOCAL_SRC_FILES := hello.c
LOCAL_MODULE := hello.default
include $(BUILD_SHARED_LIBRARY)
编译:
直接在当前目录下执行“mm”命令即可(前提是已经配置好源码环境)
注:由于HAL层通常是调用硬件的,所以上层app在使用的时候不一定具有相应的权限,可以在系统的ueventd.rc文件中,加一行:
/dev/hello 0666 root root