PN532库与Android应用同步失败:CMake配置报错求助
问题描述
在Android项目中集成libnfc库(用于PN532读卡器),编写了CMakeLists.txt和native-lib.cpp,但同步Gradle时出现错误:
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
我的CMakeLists.txt
# Minimum version of CMake required cmake_minimum_required(VERSION 3.18.1) # Project name project(nfc_app) # Specify the library to be built as a shared library add_library( # Name of the library to be built (native JNI library) nfc_native_lib # This should be a logical name for the library # Build as a shared library SHARED # Add your native source files here src/main/cpp/native-lib.cpp # Ensure this is the correct path ) # Set the path to the libnfc header files (should point to headers, not .so) include_directories( ${CMAKE_SOURCE_DIR}/src/main/jniLibs/libnfc.so/libnfc/pn532.h # Path to libnfc headers (.h files) ) # Find the libnfc library find_library( libnfc-lib # Variable to store the result of the find nfc # Name of the library (libnfc.so is what will be searched) PATHS ${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi-v7a # Path where .so file is located ) # Set path to additional libraries if required (for example, if libnfc is in a custom directory) # You can adjust this path as per your project structure link_directories(${CMAKE_SOURCE_DIR}/src/main/jniLibs/arm64-v8a) # Link the native library with libnfc and any other necessary libraries (e.g., log for logging) target_link_libraries( nfc_native_lib # The JNI library you're building (same name as in add_library) ${libnfc-lib} # Link libnfc (found using find_library) log # Optional: Android NDK logging library )
我的native-lib.cpp
#include <jni.h> #include <nfc/nfc.h> // Include libnfc headers JNIEXPORT jstring extern "C" JNICALL Java_com_example_nfc_app_MainActivity_readNFC(JNIEnv *env, jobject thiz) // Use libnfc functions here nfc_device *device = NULL; // Declare the NFC device pointe // Initialize libnfc, interact with the NFC reader (PN532), etc. nfc_context *context; nfc_init(&context); if (context == NULL) { return env->NewStringUTF("Failed to initialize libnfc"); } // Open the NFC device device = nfc_open(context, NULL); // NULL means using the first available device if (device == NULL) { nfc_exit(context); return env->NewStringUTF("Failed to open NFC device"); }
错误原因与修复方案
CMakeLists.txt的问题与修复
- include_directories路径错误:该指令需要指向包含头文件的目录,而非单个头文件。假设
nfc.h、pn532.h等头文件都在src/main/jniLibs/include/libnfc/目录下,修改为:
include_directories( ${CMAKE_SOURCE_DIR}/src/main/jniLibs/include/ )
- 架构路径不一致:find_library指定了
armeabi-v7a,但link_directories用了arm64-v8a,导致CMake无法匹配对应架构的库。建议用${ANDROID_ABI}自动适配当前编译架构:
# 统一架构路径 set(LIBNFC_PATH ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}) # 查找libnfc库 find_library( libnfc-lib nfc PATHS ${LIBNFC_PATH} NO_DEFAULT_PATH # 仅在指定路径查找,避免系统库干扰 ) # 添加链接目录 link_directories(${LIBNFC_PATH})
- 校验库文件存在性:确认
src/main/jniLibs/${ANDROID_ABI}/目录下存在libnfc.so文件(find_library查找的nfc对应文件名libnfc.so)。
修复后的完整CMakeLists.txt:
cmake_minimum_required(VERSION 3.18.1) project(nfc_app) add_library( nfc_native_lib SHARED src/main/cpp/native-lib.cpp ) # 指向libnfc头文件所在目录(根据实际路径调整) include_directories( ${CMAKE_SOURCE_DIR}/src/main/jniLibs/include/ ) # 适配当前编译架构 set(LIBNFC_PATH ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}) find_library( libnfc-lib nfc PATHS ${LIBNFC_PATH} NO_DEFAULT_PATH ) link_directories(${LIBNFC_PATH}) target_link_libraries( nfc_native_lib ${libnfc-lib} log )
native-lib.cpp的问题与修复
- 函数体缺少大括号:
Java_com_example_nfc_app_MainActivity_readNFC函数未用大包裹代码块,导致语法错误。 - 变量拼写错误:
NFC device pointe应为NFC device pointer。 - 缺少返回值与资源清理:函数结尾需添加返回语句,以及打开设备成功后的资源释放逻辑。
修复后的native-lib.cpp:
#include <jni.h> #include <nfc/nfc.h> extern "C" JNIEXPORT jstring JNICALL Java_com_example_nfc_app_MainActivity_readNFC(JNIEnv *env, jobject thiz) { nfc_device *device = NULL; // Declare the NFC device pointer nfc_context *context; nfc_init(&context); if (context == NULL) { return env->NewStringUTF("Failed to initialize libnfc"); } device = nfc_open(context, NULL); if (device == NULL) { nfc_exit(context); return env->NewStringUTF("Failed to open NFC device"); } // 此处可添加NFC标签读取逻辑 // 清理资源 nfc_close(device); nfc_exit(context); return env->NewStringUTF("NFC device opened successfully"); }
最后检查
- 确认libnfc的头文件和.so文件路径与CMakeLists.txt配置一致。
- 在app模块的build.gradle中指定支持的ABIs:
android { defaultConfig { ndk { abiFilters 'armeabi-v7a', 'arm64-v8a' } } }
内容的提问来源于stack exchange,提问作者Wan Ammar
相关产品推荐
相关产品推荐

