AndroidStudio之NDK常见编译错误

时间:2024-05-22 17:07:22

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 文件夹
AndroidStudio之NDK常见编译错误

 

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.

AndroidStudio之NDK常见编译错误