java - jni native 函数重载签名

标签 java android c++ java-native-interface

我在声明 JNI native 函数时尝试使用函数重载。

Java 方法是:

public native static void methodaaa(String type, int errorCode);
public native static void methodaaa(String type, byte[] byts);

不重载,代码如下:

JNIEXPORT void JNICALL Java_com_xxx_yyy_JavaCallCpp_methodaaa(JNIEnv* env, jobject thiz, jstring type, jint errorCode){}

这很好用。

然后我尝试添加重载:

JNIEXPORT void JNICALL Java_com_xxx_yyy_JavaCallCpp_methodaaa(JNIEnv* env, jobject thiz, jstring type, jint errorCode){}

JNIEXPORT void JNICALL Java_com_xxx_yyy_JavaCallCpp_methodaaa(JNIEnv* env, jobject thiz, jstring type, jbyteArray buffer){}

这给了我错误:

conflicting types for Java_com_xxx_yyy_JavaCallCpp_methodaaa

然后我做了一些研究,似乎我需要在我想重载的函数的末尾添加一个“__”,并附加参数 Name mangling。

所以我尝试了:

JNIEXPORT void JNICALL Java_com_xxx_yyy_JavaCallCpp_methodaaa__Ljava_lang_String_I(JNIEnv* env, jobject thiz, jstring type, jint errorCode){}

JNIEXPORT void JNICALL Java_com_xxx_yyy_JavaCallCpp_methodaaa__Ljava_lang_String_B(JNIEnv* env, jobject thiz, jstring type, jbyteArray buffer){}

但是还是不行,错误是:

No implementation found for native Lcom/xxx/yyy/JavaCallCpp;.methodaaa:(Ljava/lang/String;I)V

有人知道如何使用 jstring 作为参数编写 JNICALL 函数名吗?或者我在这里做错了什么?

任何建议将不胜感激,谢谢:)

更新:

我在这里找到了链接:

http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/design.html

然后尝试修改我的代码:

JNIEXPORT void JNICALL Java_com_xxx_yyy_JavaCallCpp_methodaaa__Ljava_lang_String_2I(JNIEnv* env, jobject thiz, jstring type, jint errorCode){}

JNIEXPORT void JNICALL Java_com_xxx_yyy_JavaCallCpp_methodaaa__Ljava_lang_String_2B(JNIEnv* env, jobject thiz, jstring type, jbyteArray buffer){}

但是,我仍然遇到同样的错误:

No implementation found for native Lcom/xxx/yyy/JavaCallCpp;.methodaaa:(Ljava/lang/String;I)V

最佳答案

不要试图自己找出 JNI 方法签名。使用 javah 的输出。它永远不会错。

关于java - jni native 函数重载签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40814563/

相关文章:

java - Activity 背景音频

java - 澄清我对testNG注释的疑惑

java - 从网页获取登录帖子请求

java - 在一个长方形内画n个正方形

android 开发 - 不能开始新的 Activity

java - 当我们有多个 Java 线程时,运行时会发生什么?

android - java.io.IOException : FIS_AUTH_ERROR in Android Firebase but DEBUG mode ok

C++ union 字段设置不正确

c++ - 多线程服务器在一个线程中处理多个客户端

c++ - 减少临时对象到就地施工的分配