有些时候我们的项目底层可能是由c++来实现的,而不是c来实现,jni也对c++声明了一些接口,跟c的有点不一样
在jni.h文件中查看可以得知,jni对c++有一个wrapper,定义在struct _JNIEnv中
/*
* C++ object wrapper.
*
* This is usually overlaid on a C struct whose first element is a
* JNINativeInterface*. We rely somewhat on compiler behavior.
*/
struct _JNIEnv {
/* do not rename this; it does not seem to be entirely opaque */
const struct JNINativeInterface* functions;
#if defined(__cplusplus)
jint GetVersion()
{ return functions->GetVersion(this); }
我们可以知道JNINativeInterface、JNIEnv、_JNIEnv之间的关系如下
struct _JNIEnv;
struct _JavaVM;
typedef const struct JNINativeInterface* C_JNIEnv;
#if defined(__cplusplus)
typedef _JNIEnv JNIEnv;
typedef _JavaVM JavaVM;
#else
typedef const struct JNINativeInterface* JNIEnv;
typedef const struct JNIInvokeInterface* JavaVM;
#endif
/*
* Table of interface function pointers.
*/
struct JNINativeInterface {
void* reserved0;
void* reserved1;
void* reserved2;
void* reserved3;
jint (*GetVersion)(JNIEnv *);
先前c文件中我们的实现是
(*env)->GetVersion(env);
在 c++中就要变成
env->GetVersion();
Android.mk文件中 加载的文件后缀名做一下更改就行
LOCAL_SRC_FILES := tu_bb_ndk_cpp_MainActivity.cpp