Android 添加JNI文件到源码流程
2016-05-07 10:53
357 查看
1:添加cpp文件到\frameworks\base\core\jni
对应目录下的Android.mk中添加编译目标
LOCAL_SRC_FILES:= \ 标签下
LOCAL_SHARED_LIBRARIES := \ 标签下
对应目录下的AndroidRuntime.cpp文件中注册cpp文件
如extern int register_android_hardware_SerialPort(JNIEnv *env);
REG_JNI(register_android_hardware_SerialPort),
cpp文件中要添加注册函数,不然Java的本地调用找不到
namespace android
{
........
static JNINativeMethod method_table[] = {
{"native_open", "(Ljava/io/FileDescriptor;I)V",
(void *)android_hardware_SerialPort_open},
{"native_close", "()V", (void *)android_hardware_SerialPort_close},
{"native_read_array", "([BI)I",
(void *)android_hardware_SerialPort_read_array},
{"native_read_direct", "(Ljava/nio/ByteBuffer;I)I",
(void *)android_hardware_SerialPort_read_direct},
{"native_write_array", "([BI)V",
(void *)android_hardware_SerialPort_write_array},
{"native_write_direct", "(Ljava/nio/ByteBuffer;I)V",
(void *)android_hardware_SerialPort_write_direct},
{"native_send_break", "()V", (void *)android_hardware_SerialPort_send_break},
};
int register_android_hardware_SerialPort(JNIEnv *env)
{
jclass clazz = env->FindClass("android/hardware/SerialPort");
if (clazz == NULL) {
ALOGE("Can't find android/hardware/SerialPort");
return -1;
}
field_context = env->GetFieldID(clazz, "mNativeContext", "I");
if (field_context == NULL) {
ALOGE("Can't find SerialPort.mNativeContext");
return -1;
}
return AndroidRuntime::registerNativeMethods(env, "android/hardware/SerialPort",
method_table, NELEM(method_table));
}
}
2:或者添加cpp文件到\frameworks\base\services\jni
对应目录下的Android.mk中添加编译目标
LOCAL_SRC_FILES:= \ 标签下
LOCAL_SHARED_LIBRARIES := \ 标签下
对应目录下onload.cpp文件中注册cpp文件
此目录下的JNI文件主要是用来注册系统服务时使用的的如上一张讲到的需要调用本地方法时使用。
如:int register_android_server_SerialService(JNIEnv* env);
register_android_server_SerialService(env);
cpp文件中要添加注册函数,不然java的本地调用找不到
namespace android
{
........
static JNINativeMethod method_table[] = {
{ "native_open", "(Ljava/lang/String;)Landroid/os/ParcelFileDescriptor;",
(void*)android_server_SerialService_open },
};
int register_android_server_SerialService(JNIEnv *env)
{
jclass clazz = env->FindClass("com/android/server/SerialService");
if (clazz == NULL) {
ALOGE("Can't find com/android/server/SerialService");
return -1;
}
clazz = env->FindClass("android/os/ParcelFileDescriptor");
LOG_FATAL_IF(clazz == NULL, "Unable to find class android.os.ParcelFileDescriptor");
gParcelFileDescriptorOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
gParcelFileDescriptorOffsets.mConstructor = env->GetMethodID(clazz, "<init>", "(Ljava/io/FileDescriptor;)V");
LOG_FATAL_IF(gParcelFileDescriptorOffsets.mConstructor == NULL,
"Unable to find constructor for android.os.ParcelFileDescriptor");
return jniRegisterNativeMethods(env, "com/android/server/SerialService",
method_table, NELEM(method_table));
}
}
对应目录下的Android.mk中添加编译目标
LOCAL_SRC_FILES:= \ 标签下
LOCAL_SHARED_LIBRARIES := \ 标签下
对应目录下的AndroidRuntime.cpp文件中注册cpp文件
如extern int register_android_hardware_SerialPort(JNIEnv *env);
REG_JNI(register_android_hardware_SerialPort),
cpp文件中要添加注册函数,不然Java的本地调用找不到
namespace android
{
........
static JNINativeMethod method_table[] = {
{"native_open", "(Ljava/io/FileDescriptor;I)V",
(void *)android_hardware_SerialPort_open},
{"native_close", "()V", (void *)android_hardware_SerialPort_close},
{"native_read_array", "([BI)I",
(void *)android_hardware_SerialPort_read_array},
{"native_read_direct", "(Ljava/nio/ByteBuffer;I)I",
(void *)android_hardware_SerialPort_read_direct},
{"native_write_array", "([BI)V",
(void *)android_hardware_SerialPort_write_array},
{"native_write_direct", "(Ljava/nio/ByteBuffer;I)V",
(void *)android_hardware_SerialPort_write_direct},
{"native_send_break", "()V", (void *)android_hardware_SerialPort_send_break},
};
int register_android_hardware_SerialPort(JNIEnv *env)
{
jclass clazz = env->FindClass("android/hardware/SerialPort");
if (clazz == NULL) {
ALOGE("Can't find android/hardware/SerialPort");
return -1;
}
field_context = env->GetFieldID(clazz, "mNativeContext", "I");
if (field_context == NULL) {
ALOGE("Can't find SerialPort.mNativeContext");
return -1;
}
return AndroidRuntime::registerNativeMethods(env, "android/hardware/SerialPort",
method_table, NELEM(method_table));
}
}
2:或者添加cpp文件到\frameworks\base\services\jni
对应目录下的Android.mk中添加编译目标
LOCAL_SRC_FILES:= \ 标签下
LOCAL_SHARED_LIBRARIES := \ 标签下
对应目录下onload.cpp文件中注册cpp文件
此目录下的JNI文件主要是用来注册系统服务时使用的的如上一张讲到的需要调用本地方法时使用。
如:int register_android_server_SerialService(JNIEnv* env);
register_android_server_SerialService(env);
cpp文件中要添加注册函数,不然java的本地调用找不到
namespace android
{
........
static JNINativeMethod method_table[] = {
{ "native_open", "(Ljava/lang/String;)Landroid/os/ParcelFileDescriptor;",
(void*)android_server_SerialService_open },
};
int register_android_server_SerialService(JNIEnv *env)
{
jclass clazz = env->FindClass("com/android/server/SerialService");
if (clazz == NULL) {
ALOGE("Can't find com/android/server/SerialService");
return -1;
}
clazz = env->FindClass("android/os/ParcelFileDescriptor");
LOG_FATAL_IF(clazz == NULL, "Unable to find class android.os.ParcelFileDescriptor");
gParcelFileDescriptorOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
gParcelFileDescriptorOffsets.mConstructor = env->GetMethodID(clazz, "<init>", "(Ljava/io/FileDescriptor;)V");
LOG_FATAL_IF(gParcelFileDescriptorOffsets.mConstructor == NULL,
"Unable to find constructor for android.os.ParcelFileDescriptor");
return jniRegisterNativeMethods(env, "com/android/server/SerialService",
method_table, NELEM(method_table));
}
}
相关文章推荐
- Android spinner下拉框和dialog
- Android 用户定位
- Android三种XML解析
- Android 6.0 中新的新技术
- Android中自定义属性基本步骤
- Android 系统移植与调试(一)Android编译环境编译服务器搭建
- Android自定义attr的各种坑
- 第一个Android项目--简易计算器的设计与实现
- android中禁止GridView滑动的方法
- Android的自定义View详解
- Android应用发送短信的实现
- Android Studio配置指南总结
- android开发中即时聊天软件中时间的获得
- AndroidStudio使用总结
- 7. Android框架和工具之 android-percent-support-lib-sample(百分比支持)
- 经常用到的小细节
- Android库. 1 UDP客户端
- 安卓笔记(一)
- 6. Android框架和工具之 JSON解析
- 5. Android框架和工具之 ZXing(二维码)