You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity调用CloudXR Android插件触发DllNotFoundException如何解决

Unity调用Android原生插件触发DLLNotFoundException问题排查

我正在开发供Unity调用的Android插件,从Unity侧发起插件调用时出现DLLNotFoundException报错,相关操作步骤与问题现象如下:

Android Studio侧配置

  • 基于搭载CloudXR version 3.2的hello_cloudxr_c安卓示例项目生成AAR插件。为生成AAR做的修改仅包括:
    1. 在build.gradle(:app)文件中将首行修改为 apply plugin: 'com.android.library'
    2. 注释掉defaultConfig块中的 applicationId "com.nvidia.ar.hellocloudxr" 配置
  • 调整头文件引用路径:
    • 将-DARCORE_INCLUDE的路径从${project.rootDir}/../../libraries/include"修改为${project.rootDir}/libraries/include"
    • 将-DGLM_INCLUDE的路径从${project.rootDir}/../../libraries/glm"修改为${project.rootDir}/libraries/glm"
  • 项目基础版本配置:minSdkVersion设为25,targetSDKVersion设为28
  • 在jni_interface.cc文件中新增了返回整数的测试函数getSomeNumber()用于Unity侧连通性测试,代码如下:
extern "C" {
  int getSomeNumber() {
      return 2345;
  }
}
  • 执行clean build后,在app/build/outputs/aar路径下生成app-debug.aar文件。将后缀改为.zip解压后查看app-debug/jni/arm64-v8a目录,发现仅包含libc++_shared.so、libhello_cloudxr_native.so、liboboe.so三个文件,缺失CloudXR相关依赖库。手动将libCloudXRClient.so、libgrid.so、libGsAudioWebRTC.so、libPoco.so复制到Android Studio的jniLibs目录,重新构建后AAR包内已包含全部所需.so文件。

Unity侧配置

  • 创建包含单个按钮的Unity场景,将生成的AAR文件复制到Plugins目录下,编写名为CXRConnect.cs的脚本,在按钮点击时引用插件并调用getSomeNumber()函数,代码如下:
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();
    }
}
  • Unity项目参数设置:
    • Minimum API Level设为Android 10.0(API level 29)
    • Target API Level设为Automatic(highest installed)
    • Scripting Backend为IL2CPP
    • Api Compatibility Level为.NET Standard 2.0
    • Target Architectures中勾选ARMv7和ARM64
  • 在三星Galaxy Note20 Ultra设备上运行构建后的应用,点击Unity场景中的按钮时,Android Studio的Logcat中抛出如下错误:
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

已检查插件包内容,确认所有依赖库均已包含,需要定位该问题的可能成因与对应的修复方案。


问题成因与修复方案

按优先级逐一排查以下问题即可解决:

  1. 库名引用错误
    Unity在Android平台加载原生库时,会自动补全lib前缀和.so后缀,代码里写的LIBRARY_NAME = "libhello_cloudxr_native"会被系统优先解析为查找liblibhello_cloudxr_native.so,自然匹配失败。直接把常量值改成"hello_cloudxr_native"即可。
  2. CPU架构不匹配
    之前解压AAR仅检查了arm64-v8a目录下的so文件,但Unity打包时勾选了ARMv7和ARM64两个架构。如果AAR里没有armeabi-v7a目录对应的全套so文件,系统在ARMv7环境下加载会直接失败;哪怕是在ARM64设备上运行,如果Unity判定需要兼容32位、安装的是32位安装包,同样会报找不到库。
    修复方式:要么在Unity Player Settings里取消ARMv7的勾选,仅保留ARM64架构;要么在Android Studio构建时同步编译ARMv7版本的原生库,把对应架构的所有依赖so都放到AAR对应的jni/armeabi-v7a目录下。
  3. AAR导入配置错误
    把AAR放到Unity的Plugins目录后,需要在Inspector面板中选中AAR,确认平台勾选了Android,CPU架构选项和打包选择的架构匹配,不要勾选“Optional”选项。
  4. 依赖库加载顺序问题
    Android加载原生库时不会自动递归加载依赖项,如果libhello_cloudxr_native.so依赖的CloudXR相关库没有提前加载,也会触发主库加载失败报DLLNotFound。
    修复方式:在C#侧调用getSomeNumber()之前,先手动按依赖顺序加载所有依赖库,代码示例:
    [DllImport("__Internal")]
    private static extern IntPtr dlopen(string path, int flags);
    
    // 调用业务函数前先执行加载
    void InitNativeLibs()
    {
        dlopen("libPoco.so", 2);
        dlopen("libGsAudioWebRTC.so", 2);
        dlopen("libgrid.so", 2);
        dlopen("libCloudXRClient.so", 2);
        dlopen("libhello_cloudxr_native.so", 2);
    }
    
  5. minSdkVersion冲突
    Android侧插件配置的minSdkVersion是25,但Unity项目设置的Minimum API Level是29(Android 10)。如果构建时没有做版本对齐,高版本系统下可能出现库加载异常,把Android Studio项目的minSdkVersion改成和Unity一致的29重新构建AAR即可。

内容的提问来源于stack exchange,提问作者Parms

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 16:39:35