I got the sample HelloJni project working, which uses a C file for the native code. I have not been able to get any simple examples working with C++. Take the following JNI code:
#include <jni.h>
#include <string.h>
JNIEXPORT void JNICALL Java_com_test_testActivity_doSomething(JNIEnv * env, jobject obj)
{
}
If I have the code in a .c file, it works fine. If I change the extension to .cpp it compiles fine, but blows up when running (force closes). Since the library and method signature is the same either way, I don't suspect the problem being on the Java side.
You might need to surround your code with an extern "C"
block:
extern "C" {
JNIEXPORT ...
}
You should be able to make a version that will work in both C and C++ by wrapping the extern block in #if
:
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT ...
#ifdef __cplusplus
}
#endif
Java_com_test_testActivity_doSomething will need to be extern "C".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With