I have been given a Unity project, that is an AR game made to run on android. The app runs well with Build and Run directly from Unity, and if I export it as an android library and build using Android Studio, it also runs well. Unfortunately, the format of the exported library does not work for our intended use case; it creates a module named unityLibrary containing source and dependencies, but I want to provide unityLibrary as a package through artifactory, so I need to convert the unityLibrary module into an AAR. I can do this with the usual gradlew assemble
after a few minor modifications to the unityLibrary Manifest, and I can then include that AAR locally into a project by dropping it into the the project's libs
folder and then setting it as a project dependency using implementation(name: "unityLibrary-debug" ext: "aar")
.
The app mostly works at this point, but the AR parts of the game do not work, the camera feed is a completely black screen. In the adb logs there is the following error:
2020-05-27 01:53:55.667 8661-8744/com.mokriya.aarapp E/Unity: DllNotFoundException: Unable to load DLL 'UnityARCore': The specified module could not be found.
at UnityEngine.XR.ARCore.ARCoreSessionSubsystem+NativeApi.UnityARCore_session_construct (UnityEngine.XR.ARCore.ARCoreSessionSubsystem+NativeApi+CameraPermissionRequestProviderDelegate cameraPermissionRequestProvider) [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.XR.ARCore.ARCoreSessionSubsystem+Provider..ctor (UnityEngine.XR.ARCore.ARCoreSessionSubsystem subsystem) [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.XR.ARCore.ARCoreSessionSubsystem.CreateProvider () [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.XR.ARSubsystems.XRSessionSubsystem..ctor () [0x00000] in <00000000000000000000000000000000>:0
at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00000] in <00000000000000000000000000000000>:0
at System.Activator.CreateInstance (System.Type type, System.Boolean nonPublic) [0x00000] in <0000000
2020-05-27 01:53:55.685 8661-8744/com.mokriya.aarapp E/Unity: DllNotFoundException: Unable to load DLL 'UnityARCore': The specified module could not be found.
at UnityEngine.XR.ARCore.ARCoreCameraSubsystem+NativeApi.UnityARCore_Camera_Construct (System.Int32 mainTexPropertyNameId) [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.XR.ARCore.ARCoreCameraSubsystem.CreateProvider () [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.XR.ARSubsystems.XRCameraSubsystem..ctor () [0x00000] in <00000000000000000000000000000000>:0
at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00000] in <00000000000000000000000000000000>:0
at System.Activator.CreateInstance (System.Type type, System.Boolean nonPublic) [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.SubsystemDescriptor`1[TSubsystem].Create () [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.XR.ARFoundation.SubsystemLifecycleManager`2[TSubsystem,TSubsystemDescriptor].CreateSubsystemIfNecessary () [0x
2020-05-27 01:53:55.759 8661-8744/com.mokriya.aarapp E/Unity: Unable to find FirebaseCppApp-6_12_0
2020-05-27 01:53:55.775 8661-8744/com.mokriya.aarapp E/Unity: DllNotFoundException: Unable to load DLL 'FirebaseCppApp-6_12_0': The specified module could not be found.
at Firebase.AppUtilPINVOKE+SWIGExceptionHelper.SWIGRegisterExceptionCallbacks_AppUtil (Firebase.AppUtilPINVOKE+SWIGExceptionHelper+ExceptionDelegate applicationDelegate, Firebase.AppUtilPINVOKE+SWIGExceptionHelper+ExceptionDelegate arithmeticDelegate, Firebase.AppUtilPINVOKE+SWIGExceptionHelper+ExceptionDelegate divideByZeroDelegate, Firebase.AppUtilPINVOKE+SWIGExceptionHelper+ExceptionDelegate indexOutOfRangeDelegate, Firebase.AppUtilPINVOKE+SWIGExceptionHelper+ExceptionDelegate invalidCastDelegate, Firebase.AppUtilPINVOKE+SWIGExceptionHelper+ExceptionDelegate invalidOperationDelegate, Firebase.AppUtilPINVOKE+SWIGExceptionHelper+ExceptionDelegate ioDelegate, Firebase.AppUtilPINVOKE+SWIGExceptionHelper+ExceptionDelegate nullReferenceDelegate, Firebase.AppUtilPINVOKE+SWIGExceptionHelper+ExceptionDelegate outOfMemoryDelegate, Firebase.AppUtilPINVOKE+SWIGExceptionHelper+ExceptionDelegate overflowDelegate
So, I don't understand why there should be any dependencies that aren't found at runtime when using an AAR, when those same dependencies are found with the library when it is given as a library module with source and dependencies. Does anyone have any ideas on why that might be? Or does anyone have tips on how I can troubleshoot this further?
I had exactly the same issue and I can answer some of your questions.
So, I don't understand why there should be any dependencies that aren't found at runtime when using an AAR, when those same dependencies are found with the library when it is given as a library module with source and dependencies
What's happening is that when you make an aar, the dependencies of the library are themselves excluded. It is down to the user of the aar to ensure that the dependencies required by the aar are provided.
What this means in practice is that the project which uses your Unity aar (your user project) also needs to add dependencies on the jars and aars from unityLibrary/libs
.
In your user project, for example:
// The unity game itself
implementation 'com.example:myunitygame:1.0.0'
// The dependencies for the unity game
implementation 'com.unity:unityarcore:1.0.0'
implementation 'com.example:whatever:1.0.0'
... // etc. Make sure you include all the jars and aars from `unityLibrary/libs`
Another option is to use a 'fat-aar' - this is an alternative where your aar does include all its own dependencies. It is the same idea as a 'fat jar', which you can read about here: What is a fat JAR?
Fat aars are not natively supported, but you can try a plugin like https://github.com/kezong/fat-aar-android
I have had success with this fat aar plugin - my user project has a dependency on only the single fat aar for the exported Unity project.