JNI_OnLoad 错误:无法找到具有签名 ()Ljava/lang/String 的本机函数的 decl
我目前正在使用 SWIG/jni 从 Android 应用程序的 java 调用 C++ 函数。但是,每当函数返回 jstring 时我都会遇到困难。应用程序启动时,我在 LogCat 中收到以下错误...
错误:无法找到本机 Lcom/example/swigJNI 的 decl;.plugin_name:L()java/lang/String
错误< /strong>: 无法找到本机 Lcom/example/swigJNI 的 decl;.plugin_description:L()java/lang/String
以下是一些可能有助于检查的代码...
SWIG 生成的包装器代码:
SWIGEXPORT jstring JNICALL Java_swigJNI_1plugin_1name(JNIEnv *jenv, jclass jcls) {
jstring jresult = 0 ;
char *result = 0 ;
(void)jenv;
(void)jcls;
result = (char *)plugin_name();
if (result) jresult = jenv->NewStringUTF((const char *)result);
return jresult;
}
SWIGEXPORT jstring JNICALL Java_swigJNI_1plugin_1description(JNIEnv *jenv, jclass jcls) {
jstring jresult = 0 ;
char *result = 0 ;
(void)jenv;
(void)jcls;
result = (char *)plugin_description();
if (result) jresult = jenv->NewStringUTF((const char *)result);
return jresult;
}
声明JNI Native 方法:
static const JNINativeMethod methods[] = {
{"plugin_name", "()Ljava/lang/String", (void*) Java_swigJNI_1plugin_1name},
{"plugin_description", "()Ljava/lang/String", (void*) Java_swigJNI_1plugin_1description}
};
当函数返回 int 时,我已经成功执行 JNI_onLoad() 和 RegisterNatives(),但是字符串对我来说是相当有问题的。我不太明白为什么找不到这些功能。我缺少什么吗?
I'm currently using SWIG/jni to call C++ functions from java for an Android app. However, I'm having difficulty whenever the function returns a jstring. I get the following errors in LogCat upon application launch...
ERROR: Unable to find decl for native Lcom/example/swigJNI;.plugin_name:L()java/lang/String
ERROR: Unable to find decl for native Lcom/example/swigJNI;.plugin_description:L()java/lang/String
Here is some code that might be useful to examine...
SWIG generated wrapper code:
SWIGEXPORT jstring JNICALL Java_swigJNI_1plugin_1name(JNIEnv *jenv, jclass jcls) {
jstring jresult = 0 ;
char *result = 0 ;
(void)jenv;
(void)jcls;
result = (char *)plugin_name();
if (result) jresult = jenv->NewStringUTF((const char *)result);
return jresult;
}
SWIGEXPORT jstring JNICALL Java_swigJNI_1plugin_1description(JNIEnv *jenv, jclass jcls) {
jstring jresult = 0 ;
char *result = 0 ;
(void)jenv;
(void)jcls;
result = (char *)plugin_description();
if (result) jresult = jenv->NewStringUTF((const char *)result);
return jresult;
}
Declaration of JNI Native methods:
static const JNINativeMethod methods[] = {
{"plugin_name", "()Ljava/lang/String", (void*) Java_swigJNI_1plugin_1name},
{"plugin_description", "()Ljava/lang/String", (void*) Java_swigJNI_1plugin_1description}
};
I've been successful in executing JNI_onLoad() and RegisterNatives() when the functions return int's, however strings have been quite problematic for me. I don't quite understand how these functions aren't being found. Is there something that I'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
啊啊,我感觉自己像个傻子!
我用于字符串的签名是...
实际上应该是...
忘记了分号。确认!
Ahh, I feel like a fool!
The signature I was using for string was...
when it should really be...
Forgot the semi-colon. Ack!
您是否将 java 类的头文件(生成的 c 头文件)放入定义了本机方法的 c++ 代码中。
so,生成一个C头文件,包含native方法实现的函数原型
Are you putting the header file of your java class(generated c header file) in c++ code, where the native method is defined.
so, generate a C header file, containing the function prototype for the native method implementation