往android移植boa后,需要用java启动boa service。就要用到JNI的东西。
以前没有搞过这类东西。今天搞了一个早上,终于ok了,以下是本地实现的动态注册工作:
C代码:
#include "jni.h"
#include "JNIHelp.h"
static void
android_boa_start(
JNIEnv *env, jobject thiz, jstring locale)
{
initBoaService();
}
static void
android_boa_stop(
JNIEnv *env, jobject thiz, jstring locale)
{
destroyBoaService();
}
static JNINativeMethod gMethods[] = {
{
"startBoaService",
"()V",
(void *)android_boa_start
},
{
"stopBoaService",
"()V",
(void *)android_boa_stop
},
};
// This function only registers the native methods, and is called from
// JNI_OnLoad in android_media_MediaPlayer.cpp
int register_android_boa(JNIEnv *env)
{
jclass clazz;
static const char* const kClassName = "com/android/jni_second/JNI";
/* look up the class */
clazz = (*env)->FindClass(env,kClassBoa);
//clazz = env->FindClass(env,kClassBoa);
if (clazz == NULL) {
LOGE("Can't find class %s\n", kClassBoa);
return -1;
}
/* register all the methods */
if ((*env)->RegisterNatives(env,clazz, gMethods, sizeof(gMethods) / sizeof(gMethods[0])) != JNI_OK)
//if (env->RegisterNatives(env,clazz, gMethods, sizeof(gMethods) / sizeof(gMethods[0])) != JNI_OK)
{
LOGE("Failed registering methods for %s\n", kClassName);
return -1;
}
/* fill out the rest of the ID cache */
return 0;
}
jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
JNIEnv* env = NULL;
jint result = -1;
if ((*vm)->GetEnv(vm,(void**) &env, JNI_VERSION_1_4) != JNI_OK) {
//if (vm->GetEnv(vm,(void**) &env, JNI_VERSION_1_4) != JNI_OK) {
LOGE("ERROR: GetEnv failed\n");
goto bail;
}
assert(env != NULL);
if (register_android_boa(env) < 0) {
LOGE("ERROR: Boa Server native registration failed");
goto bail;
}
/* success -- return valid version number */
result = JNI_VERSION_1_4;
bail:
return result;
}
编译后,终于可以通过了。
特别注意:代码中的红色部分。如果你的代码是C++的,那么就是红色部分调用。如果是C,红色部分编译就会出错。
具体可以查看android源代码中jni.h的定义
development/boa-0.94.13/src/android_boaService.c:292: error: request for member 'FindClass' in something not a structure or union
development/boa-0.94.13/src/android_boaService.c:300: error: request for member 'RegisterNatives' in something not a structure or union
development/boa-0.94.13/src/android_boaService.c: In function 'JNI_OnLoad':
development/boa-0.94.13/src/android_boaService.c:315: error: request for member 'GetEnv' in something not a structure or union
以前没有搞过这类东西。今天搞了一个早上,终于ok了,以下是本地实现的动态注册工作:
C代码:
#include "jni.h"
#include "JNIHelp.h"
static void
android_boa_start(
JNIEnv *env, jobject thiz, jstring locale)
{
initBoaService();
}
static void
android_boa_stop(
JNIEnv *env, jobject thiz, jstring locale)
{
destroyBoaService();
}
static JNINativeMethod gMethods[] = {
{
"startBoaService",
"()V",
(void *)android_boa_start
},
{
"stopBoaService",
"()V",
(void *)android_boa_stop
},
};
// This function only registers the native methods, and is called from
// JNI_OnLoad in android_media_MediaPlayer.cpp
int register_android_boa(JNIEnv *env)
{
jclass clazz;
static const char* const kClassName = "com/android/jni_second/JNI";
/* look up the class */
clazz = (*env)->FindClass(env,kClassBoa);
//clazz = env->FindClass(env,kClassBoa);
if (clazz == NULL) {
LOGE("Can't find class %s\n", kClassBoa);
return -1;
}
/* register all the methods */
if ((*env)->RegisterNatives(env,clazz, gMethods, sizeof(gMethods) / sizeof(gMethods[0])) != JNI_OK)
//if (env->RegisterNatives(env,clazz, gMethods, sizeof(gMethods) / sizeof(gMethods[0])) != JNI_OK)
{
LOGE("Failed registering methods for %s\n", kClassName);
return -1;
}
/* fill out the rest of the ID cache */
return 0;
}
jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
JNIEnv* env = NULL;
jint result = -1;
if ((*vm)->GetEnv(vm,(void**) &env, JNI_VERSION_1_4) != JNI_OK) {
//if (vm->GetEnv(vm,(void**) &env, JNI_VERSION_1_4) != JNI_OK) {
LOGE("ERROR: GetEnv failed\n");
goto bail;
}
assert(env != NULL);
if (register_android_boa(env) < 0) {
LOGE("ERROR: Boa Server native registration failed");
goto bail;
}
/* success -- return valid version number */
result = JNI_VERSION_1_4;
bail:
return result;
}
编译后,终于可以通过了。
特别注意:代码中的红色部分。如果你的代码是C++的,那么就是红色部分调用。如果是C,红色部分编译就会出错。
具体可以查看android源代码中jni.h的定义
development/boa-0.94.13/src/android_boaService.c:292: error: request for member 'FindClass' in something not a structure or union
development/boa-0.94.13/src/android_boaService.c:300: error: request for member 'RegisterNatives' in something not a structure or union
development/boa-0.94.13/src/android_boaService.c: In function 'JNI_OnLoad':
development/boa-0.94.13/src/android_boaService.c:315: error: request for member 'GetEnv' in something not a structure or union