Android 添加JNI文件到源码流程
来源:互联网 发布:标准院成都分院知乎 编辑:程序博客网 时间:2024/06/06 03:57
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 添加JNI文件到源码流程
- Android 添加JNI文件到源码中
- android中布局文件添加到窗口流程分析
- android中jni加载流程源码分析
- Android - JNI添加标准C++文件
- Android JNI打包so文件到apk
- Android JNI打包so文件到apk .
- android 添加应用到android源码中
- android 添加应用到android源码中
- android 添加应用到android源码中
- android 添加应用到android源码中
- Android JNI 加载 so 文件流程 (Android 6.0)
- 在android源码中添加库模块&&jni实例二
- android jni 开发流程
- Android JNI开发流程
- Android Studio JNI 流程
- android jni开发流程
- Android JNI开发流程
- 命令使用 su passwd ls
- 文件打开时open系统调用
- Linux 文件系统
- Java面试题集(一)
- Linux获取帮助
- Android 添加JNI文件到源码流程
- 文件管理与权限
- shell基础
- Linux配置文件
- SVN 版本管理:两种开发模式
- shell编程中的单引号和双引号的使用
- Android spinner下拉框和dialog
- 查看用户账户过期信息
- passwd文件和shadow文件