AndroidStudio之NDK常見編譯錯誤

lvxiangan發表於2018-11-04

1、執行ndk-build 提示error: request for member 'FindClass' in something not a structure or union
/Users/lvxiangan/Workspace/Android_Studio/NDK/app/src/main/jni/test.c:33:30: error: member reference base type 'JNIEnv' (aka 'const struct JNINativeInterface *') is not a structure or union
    jclass native_class = env->GetObjectClass(contextObject);
                          ~~~^ ~~~~~~~~~~~~~~
/Users/lvxiangan/Workspace/Android_Studio/NDK/app/src/main/jni/test.c:34:26: error: member reference base type 'JNIEnv' (aka 'const struct JNINativeInterface *') is not a
      structure or union
    jmethodID pm_id = env->GetMethodID(native_class, "getPackageManager", "()Landroid/content/pm/PackageManager;");
                      ~~~^ ~~~~~~~~~~~
/Users/lvxiangan/Workspace/Android_Studio/NDK/app/src/main/jni/test.c:35:25: error: member reference base type 'JNIEnv' (aka 'const struct JNINativeInterface *') is not a
      structure or union
    jobject pm_obj = env->CallObjectMethod(contextObject, pm_id);
                     ~~~^ ~~~~~~~~~~~~~~~~
/Users/lvxiangan/Workspace/Android_Studio/NDK/app/src/main/jni/test.c:36:26: error: member reference base type 'JNIEnv' (aka 'const struct JNINativeInterface *') is not a
      structure or union

錯誤原因:env變數在C和C++ 語法表達不一致引起。
C++   :env->FindClass("java/lang/String")
C語言:(*env)->FindClass(env, "java/lang/String")



2、No rule to make target needed by *.o
make: *** No rule to make target `x x x/xxxx/xxx/xx.c', needed by `x x x/xxxx/xxx/xx.c.o'.  Stop.
錯誤原因:Android.mk沒有修改檔案字尾名,如原來是LOCAL_SRC_FILES := test.c,修改成

step1)LOCAL_SRC_FILES := test.cpp
step2)刪除/
app/src/main/obj/local 資料夾

 

3、返回一個字串報錯: error: too many arguments to function call, expected single argument 'bytes', have 2 arguments
      return env->NewStringUTF(env,"This is my first NDK Application");
             ~~~~~~~~~~~~~~~~~     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/lvxiangan/ProgramFiles/AndroidStudio/Sdk/ndk-bundle/sysroot/usr/include/jni.h:840:5: note: 'NewStringUTF' declared here
    jstring NewStringUTF(const char* bytes)
    ^
1 error generated.

 

 

 

 

相關文章