- 首先,由于之前使用过Unity, 系统已经装好了android SDK和NDK, 所以在hello_xr文件夹下, 用local.properties文件来设置系统中二者的路径:
bash
sdk.dir=/Applications/Unity/Hub/Editor/2022.3.48f1c1/PlaybackEngines/AndroidPlayer/SDK/
# ndk.dir=/Applications/Unity/Hub/Editor/2022.3.48f1c1/PlaybackEngines/AndroidPlayer/NDK
这里由于后面编译时, 系统提示ndk.dir这种方式已经落伍了,不推荐使用,所以注释掉了, 改为在build.gradle中配置其路径, 版本号也要进行对应的修改, build.gradle中修改后的对应处如下所示:
bash
android.ndkPath = "/Applications/Unity/Hub/Editor/2022.3.48f1c1/PlaybackEngines/AndroidPlayer/NDK/"
ndkVersion "23.1.7779620"
- 编译期间,又遇到一个找不到ANDROID_NATIVE_APP_GLUE的问题, 当时报错如下:
bash
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
/Users/Documents/OpenXR-SDK-Source/src/tests/c_compile_test/ANDROID_NATIVE_APP_GLUE
used as include directory in directory /Users/Documents/OpenXR-SDK-Source/src/tests/c_compile_test
/Users/Documents/OpenXR-SDK-Source/src/tests/list_json/ANDROID_NATIVE_APP_GLUE
used as include directory in directory /Users/Documents/OpenXR-SDK-Source/src/tests/list_json
搜了一下,对应处的CMakeLists.txt是这样写的:
bash
#......
elseif(ANDROID)
set(OPENGL_INCOMPATIBLE TRUE) #不兼容OPEN GL
find_path(
ANDROID_NATIVE_APP_GLUE android_native_app_glue.h
PATHS ${ANDROID_NDK}/sources/android/native_app_glue
)
也就是说, 在给定的目录下查找android_native_app_glue.h这个文件, 找到后将其所在目录赋给ANDROID_NATIVE_APP_GLUE.
于是确认了下系统中该目录是否存在该文件, 结果发现是有的.
那么为什么查找结果是not found呢?
请教了一下ChatGPT, 列出许多种可能, 最后定位到是这个问题:
在交叉编译时,CMake 的 CMAKE_FIND_ROOT_PATH 或 CMAKE_SYSROOT 参数可能会限制搜索路径。可以在
CMake 配置文件中临时打印这些变量的值,确保没有限制搜索范围:
bash
message("CMAKE_FIND_ROOT_PATH is: ${CMAKE_FIND_ROOT_PATH}")
message("CMAKE_SYSROOT is: ${CMAKE_SYSROOT}")
如果它们包含限制,可以通过临时调整 CMAKE_FIND_ROOT_PATH_MODE_INCLUDE 和 CMAKE_FIND_ROOT_PATH_MODE_LIBRARY 的值,使其允许在本地路径中搜索:
bash
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
经过调整后,就编译成功啦