React Native集成AR类Unity项目安卓构建报错:找不到UnityARCore等依赖
问题背景
我有一个基于react-native-unity的React Native应用,用<UnityView>组件加载导出的Unity项目。用简单Unity项目导出安卓包放入unity/builds/android/unityLibrary目录后,构建成功且能正常显示Unity内容,但用AR Foundation示例项目导出的安卓包时,安卓构建失败,提示找不到arcore_client、UnityARCore、ARPresto、unityandroidpermissions等依赖。
报错信息
FAILURE: Build failed with an exception.
What went wrong:
Could not determine the dependencies of task ':unityLibrary:compileDebugAidl'.
Could not resolve all task dependencies for configuration ':unityLibrary:debugCompileClasspath'.
Could not find :arcore_client:.
Required by:
project :unityLibrary
Could not find :UnityARCore:.
Required by:
project :unityLibrary
Could not find :ARPresto:.
Required by:
project :unityLibrary
Could not find :unityandroidpermissions:.
Required by:
project :unityLibrary
当前配置
项目根目录build.gradle
buildscript { ext { buildToolsVersion = "33.0.0" minSdkVersion = 22 compileSdkVersion = 33 targetSdkVersion = 33 // We use NDK 23 which has both M1 support and is the side-by-side NDK version from AGP. ndkVersion = "23.1.7779620" } repositories { google() mavenCentral() flatDir { dirs "${project(':unityLibrary').projectDir}/libs" } } dependencies { classpath("com.android.tools.build:gradle") classpath("com.facebook.react:react-native-gradle-plugin") } }
项目根目录settings.gradle
rootProject.name = 'projectName' apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings) include ':app' includeBuild('../node_modules/@react-native/gradle-plugin') include ':unityLibrary' project(':unityLibrary').projectDir=new File('../unity/builds/android/unityLibrary') include ':unityLibrary:xrmanifest.androidlib'
文件结构

解决方法
补充依赖仓库配置
AR Foundation项目依赖Google和Unity的专属Maven仓库,需要在项目根目录的build.gradle中添加同级的allprojects块(若原本没有),配置仓库地址:allprojects { repositories { google() mavenCentral() maven { url "https://maven.unity3d.com" } flatDir { dirs "${project(':unityLibrary').projectDir}/libs" } } }检查Unity导出的依赖文件
确认AR Foundation项目导出后,unity/builds/android/unityLibrary/libs目录下存在arcore_client.aar、UnityARCore.aar等相关依赖文件。若缺失,重新导出Unity项目时,确保已正确安装并启用AR Foundation和ARCore插件。完善子模块引入
打开Unity导出的安卓项目中的settings.gradle,把里面除unityLibrary外的子模块引入语句复制到React Native项目的settings.gradle中,示例:include ':unityLibrary:GoogleARCore' include ':unityLibrary:UnityARCore' include ':unityLibrary:ARPresto' include ':unityLibrary:unityandroidpermissions'清理并重新构建
进入安卓目录执行以下命令,清理旧构建文件后重新编译:cd android ./gradlew clean ./gradlew assembleDebug
内容的提问来源于stack exchange,提问作者Chris

