test.c
#include <string.h>
#include <jni.h>
#include <android/log.h>
#include <stdio.h>
#include <stdlib.h>
jstring Java_com_test_b_hello_hellostr( JNIEnv* env,jobject thiz )
{
return (*env)->NewStringUTF(env, "Hello from JNI !");
}
This compilation is OK. But compile has the error when I change to test.cpp.
libb/jtest.cpp: In function ‘_jstring* Java_com_test_b_hello_hellostr(JNIEnv*, _jobject*)’:
jtest.cpp:108: error: base operand of ‘->’ has non-pointer type ‘_JNIEnv’
make[1]: * [out/…/obj/SHARED_LIBRARIES/libdrvb_intermediates/jtest.o] Error 1
Why it like this? It has the difference between app and c?
I check the system jni.h file: alps\dalvik\libnativehelper\include\nativehelper\jni.h
.
.
void (*ReleaseStringChars)(JNIEnv*, jstring, const jchar*);
jstring (*NewStringUTF)(JNIEnv*, const char*);
jsize (*GetStringUTFLength)(JNIEnv*, jstring);
...
jstring NewStringUTF(const char* bytes)
{ return functions->NewStringUTF(this, bytes); }
.....
JNI for C++ is slightly different from JNI for plain C.
In plain C it is correct to use:
(*env)->SomeFunction(env, arg, arg, ...)Note that you must dereference
envand that the first argument to the function is alwaysenv.In C++, it’s different. You use:
env->SomeFunction(arg, arg, ...)You don’t need to dereference
envand you don’t passenvas the first argument.The actual calls into Java will be the same. Java doesn’t care whether you use plain C or C++ to do the JNI stuff.
Here’s a quick introduction to using C++ for JNI.
http://journals.ecs.soton.ac.uk/java/tutorial/native1.1/implementing/cpp.html