androidc++cmakeopengl-esopenxr

How to add OpenXR to existing Android OpenGLES native C++ project?


I'm trying to add OpenXR to a game I've developed (custom OpenGL C++ game engine) but I can't seem to figure out how to build and link the OpenXR library for Android Studio. I've tried everything I could think of, but admittedly I'm no expert with CMake and can't find adequate instructions anywhere, and the example OpenXR projects I've found online are outdated and don't compile.

This is what I've attempted:


I first added the following to my app build.gradle file:

implementation 'org.khronos.openxr:openxr_loader_for_android:1.1.41'

This appeared to download something but I have no idea what or where and this doesn't seem to automatically add the necessary include path for the C++ code. I added the following include directives to my code (copied from an example found online) but it fails to find the files:

#include <openxr/openxr.h>
#include <openxr/openxr_platform.h>
#include <openxr/openxr_reflection.h>

Since that didn't work, I removed the 'implementation' line from the build.gradle file and instead tried going direct to the source:

I cloned the OpenXR SDK source from here: https://github.com/KhronosGroup/OpenXR-SDK and put this in the 'lib/openxr' folder within my project.

I then added the following to my CMakeLists.txt file for the C++ portion of my project:

add_subdirectory(lib/openxr lib/build/lib/android/openxr)
include_directories(lib/openxr/include)
target_link_libraries(
  [project name]
  openxr-gfxwrapper
  [...]
)

As far as I could tell, 'openxr-gfxwrapper' is the name of the library but it says it can't be found. I've also tried several other names which I found within the CMakeLists.txt files but none of them work. I've also tried replacing 'add_subdirectory(lib/openxr [...])' with 'add_subdirectory(lib/openxr/src [...])' but that didn't work either.


I've spent hours searching online for how to build this for Android but couldn't find anything that works. Any help would be appreciated, thanks! :)


Solution

  • You need to enable prefab by editing your build.gradle(.kts) like so:

    android {
      buildFeatures {
        prefab = true
      }
    }
    

    In CMakeLists.txt, you can now do:

    find_package(OpenXR REQUIRED CONFIG)
    target_link_libraries(${CMAKE_PROJECT_NAME}
            OpenXR::openxr_loader)