Android - NDK - C ++ - can not initialize a variable of type 'int *' with a value of type 'jlong ​​*' (alias 'long long *')


I asked a similar question about char to jsstring ... but now I have a problem with an int to jlongArray, which I just can't figure out :/

I get the following error:

Error:(290, 10) error: cannot initialize a variable of type 'int *' with an rvalue of type 'jlong *' (aka 'long long *')

on this line:

JNIEXPORT void Java_de_meetspot_ndktest_MainActivity_LoadPlayerA(JNIEnv *javaEnvironment, jobject self, jstring audioPath, jlongArray offsetAndLength) {
   const char* audio = javaEnvironment->GetStringUTFChars(audioPath, JNI_FALSE);
   int* params = javaEnvironment->GetLongArrayElements(offsetAndLength, JNI_FALSE);
   example->LoadPlayerA(audio, params);

this is the declaration:

void LoadPlayerA(const char *audioPath, int *params);

can someone help me out?

long in Java is a signed 64-bit integer type. A C++ int on the other hand is only defined as being "guaranteed to have a width of at least 16 bits".

Lets's assume that your ints are 32-bit (that's a very common scenario): so now you've got a pointer to a bunch of data where each element is 64 bits, and you're trying to pass that to a function that expects a pointer to 32-bit data. That's obviously not going to work, even if the compiler had allowed it.

Some options for you:

  • Change LoadPlayerA to take a jlong* or int64_t* (and change any code in LoadPlayerA that relies on the incoming data being of type int).
  • Change the Java code to pass the data as an int[] instead of a long[], and change LoadPlayerA to take a jint* or int32_t*.
  • Allocate an array/vector of type int in your JNI function and convert the data from jlong to int before passing it to LoadPlayerA.