在 Android NDK 中编写本机方法签名的正确方法是什么? [英] What is the correct way to write native method signatures in Android NDK?

查看:39
本文介绍了在 Android NDK 中编写本机方法签名的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NDK 库和相应的 Java 类的工作实现.但我无法向该类添加重载方法.目前我的课程包含:

I have a working implementation of NDK library and corresponding Java-class. But I am not able to add overloaded method to that class. Currently my class contains:

package com.package;

public class MyClass
{
  public static native String getFileName();
  static
  {
    System.loadLibrary("mylib");
  }
}

我的 jniwrappers.cpp 文件有以下声明:

My jniwrappers.cpp file has the following declaration:

JNIEXPORT jstring JNICALL
Java_com_package_MyClass_getFileName(_JNIEnv* env, jobject thiz);

到目前为止,一切正常.但接下来我修改我的课程:

Up to this point everything is working fine. But next I modify my class:

package com.package;

public class MyClass
{
  public static native String getFileName();
  public static native String getFileName(int index);
  ...
}

并在 jniwrappers.cpp 中添加另一个声明:

And add to jniwrappers.cpp another declaration:

JNIEXPORT jstring JNICALL
Java_com_package_MyClass_getFileName__I(_JNIEnv* env, jobject thiz, jint index);

它编译良好,Android 应用程序启动,没有得到 UnsatisfiedLinkError 但是当它使用参数调用第二个方法时,第一个 C++ 函数被调用,但不是第二个.我在该类中有其他带有参数的方法,但它们都没有被重载,因此它们各自的 JNI 签名不包含参数.

It compiles fine, Android application starts, does not get UnsatisfiedLinkError but when it calls the second method with the argument the first C++ function is being called but not the second. I have other methods with arguments in that class but none of them are overloaded so their respective JNI signatures do not contain arguments.

那么,我做错了什么?

推荐答案

您必须在原始 getFileName 函数的末尾添加一个 __ ,因为它已重载.您的 2 C 函数原型现在应该如下所示:

You have to add a __ onto the end of the original getFileName function now that it is overloaded. Your 2 C function prototypes should now look like this:

JNIEXPORT jstring JNICALL Java_com_package_MyClass_getFileName__
  (JNIEnv *, jclass);

JNIEXPORT jstring JNICALL Java_com_package_MyClass_getFileName__I
  (JNIEnv *, jclass, jint);

这篇关于在 Android NDK 中编写本机方法签名的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆