The Oculus OpenXR Mobile SDK and Oculus Mobile SDK sample projects are configured in such a way that modern versions of Android Studio (like for example Android Studio Flamingo) are not able to load correctly.
The documentation in https://developer.oculus.com/documentation/native/android/mobile-studio-setup-android/ is outdated and does not help much in the process.
I have spent 2 days to get the SDK projects working and this post is for you to save those two days. The truth is that everything related to the Android Studio build system is very frustrating because you must configure a lot of components separately, components that are often not compatible with each other, and also many times Android Studio works with cached information and you have to tell it to clear the cache to work with the changes you made, etc.. In addition, the SDK projects are built in such a way that Android Studio is not able to manage them correctly and problems appear:
As you can see, it tells us to add the path to the SDK either in an environment variable or in the (OculusSDKRoot)\local.properties file (which does not exist in the SDK). 4. Create the file (OculusSDKRoot)\local.properties with the next content (changing "Josea" with your user name).
sdk.dir=C\:\\Users\\Josea\\AppData\\Local\\Android\\Sdk
Indicating that we must install a compatible NDK, and they indicate that the default version would be 21.0.6613669. 6. In the toolbar, press the SDK Manager button (a 3D cube with a down arrow) or by selecting the menu option File/Setttings and selecting on the left side the option Appareance & Behavior/System Settings/Android SDK. 7. In the SDK Platforms tab select Android 8.0 (Oreo), as this is the Android version with API Level 26 number which is the version number for which the SDK projects are configured. 8. In the SDK Tools tab click on the Show Package Details checkbox to display the different versions of the components and open NDK (Side by side) and select version 21.0.6613669 and accept.
Click the Sync Project with Gradle Files button or select the File/Sync Project with Gradle Files menu option. Now the following error message will be displayed:
This error is caused because the version of the JDK is used by default with this version of Android Studio is not compatible with the version of the Gradle Plugin and the version of Gradle configured with the project.
Open the (OculusSDKRoot)build.gradle file. There you can locate that we have the following line:
classpath 'com.android.tools.build:gradle:4.0.0'.
The 4.0.0.0 is the version of the Graddle Plugin that the program has configured. This plugin must run with a certain version of Gradle, and in turn, this version of Gradle must run with a certain version of Java. If you go to https://developer.android.com/build/releases/gradle-plugin#updating-gradle and click on the Older versions button you will see that version 4.0.0.0 of the plugin requires a version of Gradle 6.1.1, and this version of Gradle we would have to look for which version of Java corresponds to it. As it is an incredibly outdated version, we are going to update everything, so modify the file (OculusSDKRoot)build.gradle and change that line to version 7.0.2. If you look at that table (https://developer.android.com/build/releases/gradle-plugin#updating-gradle), the Gradle version required for the plugin version 7.0 is 7.0. Now we only need to know the Java version on which Gradle 7.0 must run. If you go to https://developer.android.com/build/releases/gradle-plugin#updating-gradle you can check that it is version 16. 12. Modify the file (OculusSDKRoot)build.gradle and change the Gradle Plugin version to 7.0.2:
classpath "com.android.tools.build:gradle:7.0.2"
Open this file and replace the value that appears in the distributionUrl variable with the following:
distributionUrl=https://services.gradle.org/distributions/gradle-7.0.2-all.zip
I have analyzed the files that Android Studio opens with Process Monitor and I have seen that it really tries to open the file in the path: (OculusSDKRoot)XrSamples\XrSpatialAnchor\Projects\Android\gradle\wrapper\gradle-wrapper.properties, and not in the path (OculusSDKRoot)gradle\wrapper\gradle-wrapper.properties, so we just must copy the folder (OculusSDKRoot)gradle inside (OculusSDKRoot)XrSamples\XrSpatialAnchorjects\Android.
And you're done! Remember that you must copy in each project (XrBodyFaceEyeSocial, XrBodyTrackingFB, ....) in their Projects/Android directories the gradle directory that you have in the SDK root directory.