Search code examples
android-studiosdkoculusquest

Unable to build any of the projects of Oculus OpenXR Mobile SDK and Oculus Mobile SDK


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.


Solution

  • 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:  

    1. When selecting File/Project Structure, the Android Gradle Plugin Version and Gradle Version fields always appear empty and even if you enter some value, it is useless: no file is modified and if you open the dialog box again, they are empty again.
    2. If it detects that there is a newer version of the Gradle plugin, the wizard is not able to locate the active version (although curiously it has detected that there is a newer version so that in theory it knows which version you are in).
    3. Android Studio is unable to find the file gradle-wrapper.properties. In File/Settings/Build, Execution, Deployment/Gradle if you select Use Gradle from: gradle-wrapper.properties file, it will show you an error indicating that it does not find the file gradle-wrapper.properties.   This tutorial is to get the projects compiled in Android Studio Flamingo | 2022.2.1 Patch 2 which is the latest version as of today (May 5, 2023) and in it, we will also update the Gradle plugin to version 7.0.2 (because I have not been able to make it work in 7.4.2).    I've run everything on Windows and the truth is that I'm too lazy to try everything on Mac or Linux, so I hope that even if your OS is not Windows, at least these steps will help you. I'm going to do the steps on a virtual machine with clean Windows, a machine that has never had Android Studio or Java, or anything else installed and I'll do everything without creating any environment variable.  
    4. Download and install Android Studio.
    5. Download the zip with the SDK, I downloaded ovr_openxr_mobile_sdk_53.0.zip. Unzip it.
    6. Open with Android Studio any of the projects, I have opened the (OculusSDKRoot)XrSamples\XrSpatialAnchor\Projects\Android folder.   Android Studio will start downloading and executing libraries and after a while, it will show you an error like the following:

    enter image description here

    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
    
    1. 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: 

    enter image description here

    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.

    1. 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:  enter image description here

    2. 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.

    3. 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"
    
    1. Do the same with the file (OculusSDKRoot)VrApp.gradle
    2. Now we must specify the version of Gradle that corresponds with this version of the plugin. The version is specified in the gradle-wrapper.properties that in the Oculus SDK appears in the path: (OculusSDKRoot)gradle-wrapper.properties.

    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
    
    1. In theory we have correctly configured the Gradle Plugin and the Gradle versions to use, but if you press the Sync Projects with Gradle Files button you will see that it does not work.   The problem is that Android Studio is not locating the gradle-wrapper.properties file that we just modified. Let's check it...
    2. Select File/Settings/Build, Execution, Deployment/Build Tools/Gradle: If you notice, an error appears to the right of Use Gradle from: 'gradle-wrapper.properfies' file

    enter image description here   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

    1. Select File/Settings/Build, Execution, Deployment/Build Tools/Gradle again. Now the error message is no longer displayed because Android Studio does locate the gradle-wrapper.properfies file. All that remains is to specify the Java version with which Gradle should be run. 18. In Gradle JDK press the arrow button and select in the context menu the option Download JDK....
    2. In the Download JDK dialog box select Version 16 (which I remind you was the Java version compatible with Gradle 7.0). You will see that Amazon Corretto 16.0.2 is automatically selected in Vendor. Click Download and OK in the Settings dialog box.
    3. Now we must clear caches. Select the File/Invalidate caches... menu option and in the dialog box, check all the checkboxes and click OK to restart Android Studio.  

    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.