標籤:
Android NDK帶的jni例子都是使用C定義JNI介面,但是在項目中,因為Native代碼是用C++編寫的,所以我就使用C++定義JNI介面,但是初學者總會遇到很多問題:
jni中的常見問題:
1、base operand of ‘->‘ has non-pointer type ‘JNIEnv {aka _JNIEnv}‘和Method ‘GetStringUTFChars‘ could not be resolved
其中這個問題是跟你jni配置時到底是.cpp還是.c檔案及:
和
解決問題的辦法:
1》使用c++來寫代碼,檔案名稱就必須【cpp】尾碼: C++ code must have .cpp extension.,必須cpp尾碼,c尾碼不行;2》使用c來寫代碼,檔案名稱就必須【c】尾碼;
2》調用的代碼這麼來寫://return (*env)->NewStringUTF(env, "Hello from JNI !");//如果是用C語言格式就用這種方式
//return env->NewStringUTF((char *)"Hello from JNI !");//C++用這種格式
錯誤的辦法:
網上有一種方法是如下,但這方方法明顯是騙小孩子的,哪有用忽略這種東西的,不推薦
正確的辦法:
問題是就是上面第二點,因此
這是如果想深究原因可以查看<jni.h>
1、用.cpp檔案定義的
const char* GetStringUTFChars(jstring string, jboolean* isCopy)
{ return functions->GetStringUTFChars(this, string, isCopy); }
2、用.c檔案定義
const char* (*GetStringUTFChars)(JNIEnv*, jstring, jboolean*);
base operand of '->' has non-pointer type 'JNIEnv {aka _JNIEnv}'和Method 'GetStringUTFChars' could no