简体   繁体   English

JNI_OnLoad错误:无法使用signature()Ljava / lang / String查找本机函数的decl

[英]JNI_OnLoad error: Unable to find decl for native function with signature ()Ljava/lang/String

I'm currently using SWIG/jni to call C++ functions from java for an Android app. 我目前正在使用SWIG / jni从Java为Android应用程序调用C ++函数。 However, I'm having difficulty whenever the function returns a jstring. 但是,只要函数返回jstring,我就会遇到困难。 I get the following errors in LogCat upon application launch... 在应用程序启动时,我在LogCat中收到以下错误...

ERROR : Unable to find decl for native Lcom/example/swigJNI;.plugin_name:L()java/lang/String 错误 :无法为本机Lcom / example / swigJNI找到decl; .plugin_name:L()java / lang / String

ERROR : Unable to find decl for native Lcom/example/swigJNI;.plugin_description:L()java/lang/String 错误 :无法为本机Lcom / example / swigJNI找到decl; .plugin_description:L()java / lang / String

Here is some code that might be useful to examine... 以下是一些可能有用的代码来检查......

SWIG generated wrapper code: 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;
}

Declaration of JNI Native methods: JNI原生方法声明:

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. 当函数返回int时,我已成功执行JNI_onLoad()和RegisterNatives(),但字符串对我来说是个问题。 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. 您是否将java类的头文件(生成的c头文件)放在c ++代码中,其中定义了本机方法。

so, generate a C header file, containing the function prototype for the native method implementation 因此,生成一个C头文件,其中包含本机方法实现的函数原型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM