简体   繁体   English

NDK JNI函数调用

[英]NDK JNI functions call

I just started using NDK, since my Android SDK code did not give satisfactory results, though I have never tried to code in C/C++. 尽管我从未尝试过使用C / C ++进行编码,但由于我的Android SDK代码无法给出令人满意的结果,所以我才开始使用NDK。 Until now, I generated the headers of my two native functions, fft_transform() and convolve() . 到目前为止,我生成了我的两个本地函数的头文件fft_transform()fft_transform() convolve()

I want to have use of the code written here . 我想使用这里编写的代码。 The problem is that fft_transform calls other C functions, and being called itself in another C function. 问题是fft_transform调用其他C函数,并在另一个C函数中本身被调用。

JNIEXPORT void JNICALL Java_com_example_ffttest_FFTActivity_transform
(JNIEnv *env, jobject obj, jdoubleArray real, jdoubleArray imag, jint n)
{
if (n == 0)
    return 1;
else if ((n & (n - 1)) == 0)  // Is power of 2
    return transform_radix2(real, imag, n);
else  // More complicated algorithm for arbitrary sizes
    return transform_bluestein(real, imag, n);
}

Should I declare the non-JNI functions in the header as they are, or change them to JNI functions? 我应该在标头中直接声明非JNI函数,还是将其更改为JNI函数? How to add JNI *env and jobj obj variables in the non-JNI functions? 如何在非JNI函数中添加JNI *envjobj obj变量?

For example: 例如:

int inverse_transform(double real[], double imag[], size_t n) {
    return Java_com_example_ffttest_FFTActivity_transform(env, obj, imag, real, n);
}

Please consider that I never used C/C++ neither NDK before starting to vote down. 请考虑我在开始投票之前从未使用过C / C ++和NDK。

  1. The Java method FFTActivity.transform(..) seems to have no return type. Java方法FFTActivity.transform(..)似乎没有返回类型。 Therefore you can not return something in it's JNI implementation like you do: if (n == 0) return 1; 因此,您不能像在JNI实现中那样返回任何内容: if (n == 0) return 1; Change the Java method and re-create/update your JNI headers accordingly. 更改Java方法,并相应地重新创建/更新您的JNI标头。

  2. Use Java_com_example_ffttest_FFTActivity_transform to convert from Java JNI types into C/C++ types used in the C/C++ implementation you want to use. 使用Java_com_example_ffttest_FFTActivity_transform将Java JNI类型转换为要使用的C / C ++实现中使用的C / C ++类型。 Check JNI documentation for the available functions for doing so (see eg GetDoubleArrayElements method). 检查JNI文档以了解可用的功能(例如,参见GetDoubleArrayElements方法)。

  3. You should not mix up regular C/C++ functions with JNI C/C++ function. 您不应将常规C / C ++函数与JNI C / C ++函数混淆。 In general this possible but a bit complicated. 一般来说,这可能但有点复杂。 Therefore you should not call the JNI function anywhere in your c/C++ code. 因此,不应在c / C ++代码中的任何地方调用JNI函数。 This method is only called from the Java side of your application. 仅从应用程序的Java端调用此方法。

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

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