Android NDK function returns wrong values ββfor 64 bit variables (double, long int)
I am trying to deal with android NDK in android studio, I created a demo app with a jni folder with the required files, it seems to work as expected when I call a function that returns an int or const char *, but when it comes to doubles, he just doesn't behave.
I tried to return the return value in jdouble and also tried to declare functions as returning a long double result with the same results. The application works on bundle 5, and I don't know what's wrong.
As requested, here are some attempts, on the left is a return statement in c, on the right is the value obtained in java
return 0 -> 0
return 1 -> 0
return 1.555 -> -1.374389535E9
return 10 -> 0
return 10.555 -> -1.71798692E8
return 2000 -> 0
return 2000.123 -> -2.0615843E8
Ok, seems like integer values ββare being returned as 0 ...?
EDIT:
I added a new function getMyLong as I did with other functions
long int getMyLong(){ return 0x1122334455667788;}
Interestingly, if a bridge function is defined in main.c that calls a function from func.c, it only returns what appears to be the 32-bit part, and if I return my value directly from the bridge function, it works and so it does doubles
JNIEXPORT jlong JNICALL Java_com_lelloman_jni_1test_MainActivity_getMyLong
(JNIEnv * env, jobject obj){
return getMyLong(); // this one will return 1432778632 in java
//return 0x1122334455667788; this one returns 1234605616436508552
}
Edit2 :
about build configuration and ndk, I barely know what I am doing, but I'll do my best to point out what I know:
- in local.properties I added this line ndk.dir=/usr/share/android-studio/data/ndk
- in ndk / RELEASE.TXT says r10c (64-bit)
- In build. gradle I added this inside defaultConfig (I also tried without cFlags, it is the same)
ndk {
moduleName "ndkTest"
cFlags "-std=c99"
}
- javah generated a file named com_lelloman_jni_test_MainActivity.h
, the only one is #include <jni.h>
- I wrote main.c and the only include is the file created with javah
main.c
#include "com_lelloman_jni_test_MainActivity.h"
JNIEXPORT jint JNICALL Java_com_lelloman_jni_1test_MainActivity_getMyInt
(JNIEnv * env, jobject obj, jint b, jint h){
return getMyInt( b, h);
}
JNIEXPORT jdouble JNICALL Java_com_lelloman_jni_1test_MainActivity_getMyDouble
(JNIEnv *env, jobject obj, jdouble r){
return getMyDouble(r);
}
func.h
#ifndef FUNC_H_INCLUDED
#define FUNC_H_INCLUDED
int getMyInt(int b,int h);
double getMyDouble(double r);
const char* getMyString(int q);
#endif // FUNC_H_INCLUDED
func.c
#include "func.h"
#ifndef PI
#define PI 3.14159265359
#endif // PI
int getMyInt(int b,int h){
return b*h;
}
double getMyDouble(double r){
//return r*PI*2;
//return 5;
return 1*2.23;
}
source to share