Gradle has exited unexpectedly when building OVR apk file

Hello Unity Community,
I am trying to build an .apk file for my Meta Quest 2 and keep seeing this error every time I try to do an OVR build apk:

Gradle has exited unexpectedly.
UnityEngine.Debug:LogError (object)
OculusBuildApp:BuildGradleProject () (at ./Library/PackageCache/com.meta.xr.sdk.core/Editor/OVRBuild.cs:644)
OculusBuildApp:OVRBuildRun () (at ./Library/PackageCache/com.meta.xr.sdk.core/Editor/OVRBuild.cs:544)
OculusBuildApp/<>c:<StartBuild>b__42_0 () (at ./Library/PackageCache/com.meta.xr.sdk.core/Editor/OVRBuild.cs:454)
System.Threading.ThreadHelper:ThreadStart ()

Not sure what to do since I am a beginner at Unity. My version is 6000.0.25f1 with all of the recommended external tools for Android. I think this is related to a Gradle timeout error but not 100% sure. Any help with this issue is appreciated and I can send any additional information needed. Thanks in advance!

Sometimes the API level is not updated correctly.
Change your API level in “Edit->Project Settings->Player” to the latest (Level 35 or 36).
Try to generate a build, Unity should updates its Android SDK automatically.
Then go back to your previous API level.

Also if you are using XR Plug-in Management go to “Edit->Project Settings->Project Validation” and fix what you can.

Currently I am not use XR Plug-in Management, should I be? Also I tried the mentioned fixed by changing the API version to level 35 and the error is still happening. Thank you for the suggestion.

Depending of what you want to try you can:

  • Try to switch from il2cpp to mono (Project Settings → Player → Scripting Backend) then build and switch back.
  • Try to reinstall in the Unity Hub the android modules of your Unity version

  • Old method: Download Android Studio to manualy update your Gradle and SDK. Then set them in Unity (Preference → External Tools)

It seems that this guy solve the same error than you: Build Failed after charging XR Oculus plug-in : Gradle build failed.

My best advice if you just started your project is to use the OpenXR workflow instead (XR Interaction Toolkit and XR Plugin Management), it is well documented, setup most of the project settings for you and is less confusing than the MetaXR plugin.

I have tried all of these fixes and it still will not build. Not sure what I am doing wrong here. Thank you for the amazing suggestions again. I am also running into this error every time I start the editor:

TypeLoadException: VTable setup of type Meta.WitAi.Data.Configuration.WitConfiguration failed
System.RuntimeType.GetMethodsByName (System.String name, System.Reflection.BindingFlags bindingAttr, System.RuntimeType+MemberListType listType, System.RuntimeType reflectedType) (at <321eb2db7c6d43ea8fc39b54eaca3452>:0)
System.RuntimeType.GetMethodCandidates (System.String name, System.Reflection.BindingFlags bindingAttr, System.Reflection.CallingConventions callConv, System.Type[] types, System.Int32 genericParamCount, System.Boolean allowPrefixLookup) (at <321eb2db7c6d43ea8fc39b54eaca3452>:0)
System.RuntimeType.GetMembers (System.Reflection.BindingFlags bindingAttr) (at <321eb2db7c6d43ea8fc39b54eaca3452>:0)
Meta.XR.ImmersiveDebugger.Editor.CompilationProcessor+<>c.<ProcessCompiledAssemblies>b__5_0 (System.Type t) (at ./Library/PackageCache/com.meta.xr.sdk.core/Editor/ImmersiveDebugger/CompilationProcessor.cs:93)
System.Linq.Enumerable+WhereArrayIterator`1[TSource].ToList () (at <378523c1e6ba4db992c5a1e98a5541ae>:0)
System.Linq.Enumerable.ToList[TSource] (System.Collections.Generic.IEnumerable`1[T] source) (at <378523c1e6ba4db992c5a1e98a5541ae>:0)
Meta.XR.ImmersiveDebugger.Editor.CompilationProcessor.ProcessCompiledAssemblies () (at ./Library/PackageCache/com.meta.xr.sdk.core/Editor/ImmersiveDebugger/CompilationProcessor.cs:95)
Meta.XR.Editor.Callbacks.InitializeOnLoad.OnAllAssetsPostProcessed () (at ./Library/PackageCache/com.meta.xr.sdk.core/Editor/Utils/EditorCallbacks/InitializeOnLoad.cs:46)
Meta.XR.Editor.Callbacks.AssetPostProcessor.OnPostprocessAllAssets (System.String[] importedAssets, System.String[] deletedAssets, System.String[] movedAssets, System.String[] movedFromAssetPaths, System.Boolean didDomainReload) (at ./Library/PackageCache/com.meta.xr.sdk.core/Editor/Utils/EditorCallbacks/AssetPostProcessor.cs:32)
System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <321eb2db7c6d43ea8fc39b54eaca3452>:0)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <321eb2db7c6d43ea8fc39b54eaca3452>:0)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at <321eb2db7c6d43ea8fc39b54eaca3452>:0)
UnityEditor.AssetPostprocessingInternal.InvokeMethod (System.Reflection.MethodInfo method, System.Object[] args) (at <9e3a60dbcacc4e77a9c6e1c14064183a>:0)
UnityEditor.AssetPostprocessingInternal.PostprocessAllAssets (System.String[] importedAssets, System.String[] addedAssets, System.String[] deletedAssets, System.String[] movedAssets, System.String[] movedFromPathAssets, System.Boolean didDomainReload) (at <9e3a60dbcacc4e77a9c6e1c14064183a>:0)
UnityEditor.AssetPostprocessingInternal:PostprocessAllAssets(String[], String[], String[], String[], String[], Boolean)

Not sure if it is actually causing my problem or not but felt I should share it. I will also be trying to implement the OpenXR workflow soon.

Yeah you should fixed all the errors you can to ensure your build will work.
There can be a version mismatch between different libraries and possibly some .dll duplicated.

  • Close Unity and delete the Library and Temp folders of your project (Unity will regenerate these on launch).
  • Dupplicated .dll can be caused by 3rd party scripts or libraries (often in Assets and Library). Check if you installed one yourself, otherwise it may be an installation failure and the previous solution should have fixed it.
  • Update or reinstall Meta XR SDK

If that doesn’t work these guys seems to have found a solution:

I got it to Work! For anyone else trying to fix this problem my fix was the delete the library folder, connect quest to my computer via usb, do a ovr build and run, then build the project normally.