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 *)plugi开发者_如何学Cn_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?
Ahh, I feel like a fool!
The signature I was using for string was...
()Ljava/lang/String
when it should really be...
()Ljava/lang/String;
Forgot the semi-colon. Ack!
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
精彩评论