2024年2月6日更新
使用Unity插件包方式接入(Unity方式),且必须使用EDM(ExternalDependencyManager),若已有ExternalDependencyManager文件夹(其他SDK也可能带有)则选用最新的即可。
工程Assets- External Dependency Manager - Android Resolver - Settings 必须开启如下3个选项这个GeneratedLocalRepo文件夹会在Assets- External Dependency Manager - Android Resolver - Resolve(或 Force Resolve)时动态生成,很关键,如果没有它将会报错FirebaseAppDll各种报错。
这些是Firebase导入各种插件包后的Dependencies依赖文件,EDM会根据这些文件生成mainTemplate.gradle,你可以在里面修改maven{ url 'xxx' }补充一些镜像url
例如上<repository>...</repository>,稳妥起见可以所有Dependencies.xml都补上这3行镜像url。
PlayerSettings - Player - Publishing Settings开启mainTemplate.gradle才能被EDM管理生成。
使用Assets - External Dependency Manager - Android Resolver - Force Resolve生成mainTemplate.gradle,需要手动调整内容。
成功Force Resolve后打开mainTemplate.gradle文件
// Android Resolver Repos Start ([rootProject] + (rootProject.subprojects as List)).each { project ->project.repositories {def unityProjectPath = $/file:///**DIR_UNITYPROJECT**/$.replace("\\", "/")maven {url "https://maven.google.com"}maven {url (unityProjectPath + "/Assets/GeneratedLocalRepo/Firebase/m2repository") // Assets/Firebase/Editor/AnalyticsDependencies.xml:21, Assets/Firebase/Editor/AppDependencies.xml:25, Assets/Firebase/Editor/CrashlyticsDependencies.xml:23}maven {url "https://maven.aliyun.com/repository/public" // Assets/Firebase/Editor/AnalyticsDependencies.xml:21, Assets/Firebase/Editor/AppDependencies.xml:25, Assets/Firebase/Editor/CrashlyticsDependencies.xml:23}maven {url "https://maven.aliyun.com/repository/google" // Assets/Firebase/Editor/AnalyticsDependencies.xml:21, Assets/Firebase/Editor/AppDependencies.xml:25, Assets/Firebase/Editor/CrashlyticsDependencies.xml:23}maven {url "https://maven.aliyun.com/repository/jcenter" // Assets/Firebase/Editor/AnalyticsDependencies.xml:21, Assets/Firebase/Editor/AppDependencies.xml:25, Assets/Firebase/Editor/CrashlyticsDependencies.xml:23}mavenLocal()mavenCentral()} } // Android Resolver Repos End
修改后为如下:
// Android Resolver Repos Start ([rootProject] + (rootProject.subprojects as List)).each { project ->project.repositories {def unityProjectPath = $/file:///**DIR_UNITYPROJECT**/$.replace("\\", "/")maven {url "https://maven.aliyun.com/repository/public" // Assets/Firebase/Editor/AnalyticsDependencies.xml:21, Assets/Firebase/Editor/AppDependencies.xml:25, Assets/Firebase/Editor/CrashlyticsDependencies.xml:23}maven {url "https://maven.aliyun.com/repository/google" // Assets/Firebase/Editor/AnalyticsDependencies.xml:21, Assets/Firebase/Editor/AppDependencies.xml:25, Assets/Firebase/Editor/CrashlyticsDependencies.xml:23}maven {url "https://maven.aliyun.com/repository/jcenter" // Assets/Firebase/Editor/AnalyticsDependencies.xml:21, Assets/Firebase/Editor/AppDependencies.xml:25, Assets/Firebase/Editor/CrashlyticsDependencies.xml:23}maven {url (unityProjectPath + "/Assets/GeneratedLocalRepo/Firebase/m2repository") // Assets/Firebase/Editor/AnalyticsDependencies.xml:21, Assets/Firebase/Editor/AppDependencies.xml:25, Assets/Firebase/Editor/CrashlyticsDependencies.xml:23} mavenLocal()mavenCentral()} } // Android Resolver Repos End
为什么要改动?因为EDM它默认生成的 maven {url "https://maven.google.com"} 国内是连不上这个地址的,所以直接删了它,使用镜像url,而且要调整到最前面。
镜像url可能会随着时间变更,这里只是使用阿里云的,如果打包卡死,那一定是这里镜像url问题了,卡死是连不上依赖资源地址导致的。
阿里云镜像仓库地址:
仓库服务
注意:修改mainTemplate.gradle之后就不要去操作Force Resolve(会被还原),操作Resolve是可以的。实际上最终打包时都会Force Resolve(因为还是会被EDM添加了maven.google.com)所以我只能够强制让自己的生效,即删除
// Android Resolver Repos Start 以及 // Android Resolver Repos End
修改为如下:
([rootProject] + (rootProject.subprojects as List)).each { project ->project.repositories {def unityProjectPath = $/file:///**DIR_UNITYPROJECT**/$.replace("\\", "/")maven {url "https://maven.aliyun.com/repository/public" // Assets/Firebase/Editor/AnalyticsDependencies.xml:21, Assets/Firebase/Editor/AppDependencies.xml:25, Assets/Firebase/Editor/CrashlyticsDependencies.xml:23}maven {url "https://maven.aliyun.com/repository/google" // Assets/Firebase/Editor/AnalyticsDependencies.xml:21, Assets/Firebase/Editor/AppDependencies.xml:25, Assets/Firebase/Editor/CrashlyticsDependencies.xml:23}maven {url "https://maven.aliyun.com/repository/jcenter" // Assets/Firebase/Editor/AnalyticsDependencies.xml:21, Assets/Firebase/Editor/AppDependencies.xml:25, Assets/Firebase/Editor/CrashlyticsDependencies.xml:23}maven {url (unityProjectPath + "/Assets/GeneratedLocalRepo/Firebase/m2repository") // Assets/Firebase/Editor/AnalyticsDependencies.xml:21, Assets/Firebase/Editor/AppDependencies.xml:25, Assets/Firebase/Editor/CrashlyticsDependencies.xml:23} mavenLocal()mavenCentral()} }
你可以改后去Force Resolve发现,上面我们自己的这块没被// Android Resolver Repos Start 以及 // Android Resolver Repos End包含的代码块是不会被EDM替换的,而是它会重新生成一份在我们的下面,就结果而言生效的是我们那份优先。(哎...无语了都
可打开任务管理器点开Unity观察这Java(TM) Platform SE binary 的使用进度 如果都是0%,而且几秒都没变化就说明卡死了,如果CPU%使用率>0%都要耐心等它。
C#脚本相关坑:
类似这种每一个Firebase-XXx插件的Unity文档都会如此介绍,Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>函数会反复提及,在Unity里你只能调用一次!且所有初始化Firebase方法都要在task回调里进行。
出现上面那个报错原因就是调用了多次,都挪在一起就好了,统一一个脚本管理初始化。
2024年2月5日更新
必须使用Unity方式接入Unity项目!一句话解决所有问题。(真的别玩Android方式)
大致这问题出现原因是我在Unity采用了Android方式接入Firebase,而Android接入实际上和Unity接入方式有配置上的不一样,我就是多做了几步操作如下。https://firebase.google.com/docs/android/setup?hl=zh-cn#java
根级(项目级)Gradle文件 和 模块(应用级)Gradle文件 对应Unity的 baseProjectTemplate.gradle 以及 mainTemplate.gradle,分别对应地方加上如上内容,结果就是bug不断,因为Unity以前mainTemplate.gradle就是apply plugin: 'com.android.application',现在新版已经改为‘com.android.library’,因此就变的有点复杂不过我没注意到,就这样配进去了,打包就肯定出问题,Unity的application已经放置到了launcherTemplate.gradle。
总结:不要试图在Unity使用Android接入配置方法,更深层的知识没搞懂前很难兼容处理好,最终就是彻底采用Unity方式或Android方式接入。
实际上,我想Firebase即接入Unity插件,也接入Android(aar形式接入Unity打包的)其实也可以吧?将上方的配置转移到Android配置,单独作为提供给第三方的,而Unity则是完全使用Unity插件方式接入Firebase即可。(待测试)
Warning: Mapping new ns http://schemas.android.com/repository/android/common/02 to old ns http://schemas.android.com/repository/android/common/01
Warning: Mapping new ns http://schemas.android.com/repository/android/generic/02 to old ns http://schemas.android.com/repository/android/generic/01
Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/addon2/02 to old ns http://schemas.android.com/sdk/android/repo/addon2/01
Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/repository2/02 to old ns http://schemas.android.com/sdk/android/repo/repository2/01
Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/sys-img2/02 to old ns http://schemas.android.com/sdk/android/repo/sys-img2/01
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':unityLibrary:processReleaseGoogleServices'.
> No matching client found for package name 'com.unity3d.player'
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 3s
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
UnityEngine.GUIUtility:ProcessEvent (int,intptr)
一、根本原因:google-services.json里的package_name和包签名不一致
"client": [{"client_info": {"mobilesdk_app_id": ...,"android_client_info": {"package_name": "填对包签名"}}
二、一般这样就好了,但是我是Unity版本的变种问题,Unity需要将google-services.json放到对应Assets/Plugins/Android文件夹下,然后Firebase会自动生成一个到StreamingAssets下,最终也是采用StreamingAssets下的作为配置读取。
注意:Firebase 会查找您添加到 Unity 项目的移动平台 Firebase 配置文件(GoogleService-Info.plist
或 google-services.json
)。然后,Firebase 会基于移动平台配置文件自动生成桌面平台 Firebase 配置文件 (google-services-desktop.json
)。此桌面配置文件包含您在 Firebase 控制台设置工作流中输入的 Unity 项目 ID。此文件将您的应用与 Firebase 项目关联。
如果编辑器找不到桌面配置文件,请检查 StreamingAssets 目录是否存在以及其中是否包含桌面配置文件。
也就是说你去工程里搜google-services-desktop.json,把它重新生成一遍就好了,也就是删了它,重新运行Unity就会自动生成,生成出的package_name就是你最新的包签名了。(真的是...
三、而且google-services.json依然是需要的,而且它要正确地拷贝到打包出的安卓工程,需要在mainTemplate.gradle末尾添加(一定保证末尾)
task copyJsonFile {def unityProjectPath = $/file:///**DIR_UNITYPROJECT**/$.replace("\\", "/")copy { delete("google-services.json")from(unityProjectPath + '/Assets/Plugins/Android/')into('./')include("google-services.json")}
}
preBuild.dependsOn copyJsonFile