Hi guys,
Like some of you here, I am creating an Android plugin to be used by Unity. However, I get a DLLNotFoundException when I try calling the plugin from Unity. Here are the steps I took:
[Android Studio]
I have generated an AAR plugin from the hello_cloudxr_c sample Android project which uses CloudXR version 3.2. The only modifications I did to this to generate the AAR were in build.gradle (:app) where I changed the first line to apply plugin: ‘com.android.library’ and comment out applicationId “com.nvidia.ar.hellocloudxr” in the defaultConfig block.
I changed the path of -DARCORE_INCLUDE from ${project.rootDir}/…/…/libraries/include" to ${project.rootDir}/libraries/include"
I changed the path of -DGLM_INCLUDE from ${project.rootDir}/…/…/libraries/glm" to ${project.rootDir}/libraries/glm"
minSdkVersion is set to 25 and targetSDKVersion is 28.
In jni_interface.cc, I added a small function called getSomeNumber() which returns an integer.
extern "C" {
int getSomeNumber() {
return 2345;
}
}
I will call this from Unity as a simple test.
After doing a clean build, app-debug.aar is created in app/build/outputs/aar. If I change the file extension to .zip, uncompress it and look in app-debug/jni/arm64-v8a, I see that only libc++_shared.so, libhello_cloudxr_native.so and liboboe.so are present. The CloudXR library isn’t there. I manually copy libCloudXRClient.so, libgrid.so, libGsAudioWebRTC.so and libPoco.so into the jniLibs folder in Android Studio. When I build again, all the required .so files are present in the AAR.
[Unity]
I have created a Unity project which has a scene containing a single button. The AAR file is copied into the Plugins folder. I have written a script called CXRConnect.cs which references the plugin and calls the getSomeNumber() function when the button is pressed.
public class CXRConnect : MonoBehaviour
{
public Button button;
public Text textObject;
private const string LIBRARY_NAME = "libhello_cloudxr_native";
[DllImport(LIBRARY_NAME)]
private static extern int getSomeNumber();
void Start()
{
Button btn = button.GetComponent<Button>();
btn.onClick.AddListener(CallCPPLibrary);
}
void CallCPPLibrary()
{
int num = getSomeNumber();
Debug.Log("CXR Some Num: "+num);
textObject.text = num.ToString();
}
}
In the Unity project settings:
Minimum API Level is set to Android 10.0 (API level 29)
Target API Level is set to Automatic (highest installed)
Scripting Backend is IL2CPP
Api Compatibility Level is .NET Standard 2.0
ARMv7 and ARM64 are ticked in Target Architectures
I am running the build on a Samsung Galaxy Note20 Ultra. When the app is running on the device and I press the button in the Unity scene, there is an error in Logcat in Android Studio which reads:
2022-07-06 10:59:42.240 3203-18112/? E/Unity: DllNotFoundException: Unable to load DLL 'hello_cloudxr_native': The specified module could not be found.
at CXRConnect.getSomeNumber () [0x00000] in <00000000000000000000000000000000>:0
at CXRConnect.CallCPPLibrary () [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.Events.UnityAction.Invoke () [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.Events.UnityEvent.Invoke () [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1].Invoke (T1 handler, UnityEngine.EventSystems.BaseEventData eventData) [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.EventSystems.StandaloneInputModule.ProcessTouchPress (UnityEngine.EventSystems.PointerEventData p
Have any of you encountered this issue? What could be the possible cause and how should I fix it? I have checked the contents of the plugin and all the required libraries are in there.