Reputation: 8345
I've made two test projects, both android applications using JNI.
For the first one i did not use Android Tools / Add native support. The native method is defined in a .c file using the C syntax of JNI, and it works fine, the method is successfully found and called.
On the second project i did add the native support and I've written the native code in a .cpp file using the C++ syntax of JNI. In this case it doesn't work, the native method is not found when calling it.
I have checked the name of the method 1000 times, it's perfectly spelled. The project compiles properly, the .so libary is also loaded successfully, but the method can not be found.
Here is my Cpp file :
#include <jni.h>
JNIEXPORT void JNICALL Java_xxxpackagenamexxx_xxxclassnamexxx_NativeShowMsgBox( JNIEnv * env, jobject jobj, jstring oStr )
{
// some stuff
}
What could this be due to ? Why isn't it working when using C++ while it works when using C ? What can I be missing here ? Thank you.
Upvotes: 1
Views: 807
Reputation: 89
extern "C" {
JNIEXPORT void JNICALL Java_xxxpackagenamexxx_xxxclassnamexxx_NativeShowMsgBox(...)
{
// some stuff
}
}
Upvotes: 4
Reputation: 303
I experienced this problem when the parameter types I specified in my Java class native function declaration didn't match the types I declared in my c++ method declaration. Javah would generate what looked to be a proper entry in the .h file, but the c++ compiler always mangled the entry point name. It wasn't mangling names of my other methods that were written properly.
In my case, I was passing in a long[] in my Java method declaration but was using a jobjectarray in my c++ method declaration. Changing the c++ declaration to jlongarray solved the problem and the function was no longer mangled.
Upvotes: -1
Reputation: 311047
#include <jni.h>
You must also #include the generated .h file. Makes the function extern "C"
and also provides a check that you got the name right.
Upvotes: 2
Reputation: 12167
Internally, C++ compilers ''mangle'' the names of C++ functions (e.g., for typing purposes), and they need to be told that a given function should be called as a C function (and thus, not have its name mangled).
For more information about using CPP in shared library, refer here.
Upvotes: 0