解决:Could not get GOOGLE_APP_ID in Google Services file...
in 技术 with 2 comments

解决:Could not get GOOGLE_APP_ID in Google Services file...

in 技术 with 2 comments

前言

上述问题出现在当我要集成 Firebase 的崩溃日志收集功能到我的工程的时候,我按照官方文档根据 pod 集成:

pod 'Firebase/Crashlytics'

并且在 Build Phases 中新增了自动上传符号文件的脚本:

"${PODS_ROOT}/FirebaseCrashlytics/run"

运行的时候报如下错误:

Could not get GOOGLE_APP_ID in Google Services file from build environment

问题

点击报错,会跳转到构建日志,会看到在报错前最后执行的命令是:

Run custom shell script 'Run Script'

所以问题出在自动上传符号文件的脚本这里。

由于

我们的项目为了区分开发环境和线上环境,在 Firebase 控制台新建了两个项目,所以项目中也会有两个配置文件:

崩溃日志收集的配置,我们可以通过代码指定配置文件:

if (线上){
 fileName = @"GoogleService-Info-RELEASE.plist";
}else{
 fileName = @"GoogleService-Info-DEBUG.plist";
}
NSString *googleServicePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"];
FIROptions *firOptions = [[FIROptions alloc] initWithContentsOfFile:googleServicePath];
[FIRApp configureWithOptions:firOptions];

但是

自动上传符号文件的脚本 run 却无法用代码控制,它默认读取的是标准配置文件命名 GoogleService-Info.plist。所以才会有上面的报错:找不到 GOOGLE_APP_ID。幸运的是,查看脚本的帮助菜单:

sh run --help

发现可以通过 -gsp 参数指定:

-gsp, --google-service-plist <path to Google service plist> The path to your app's GoogleService-Info.plist

解决

我们修改自动上传符号文件的脚本为(<project name> 替换为自己的项目名称):

if [ "${CONFIGURATION}" = "Release" ]; then
    "${PODS_ROOT}/FirebaseCrashlytics/run" -gsp <project name>/GoogleService-Info-RELEASE.plist
else
    "${PODS_ROOT}/FirebaseCrashlytics/run" -gsp <project name>/GoogleService-Info-DEBUG.plist
fi

当然

实际上项目在控制环境切换的时候远比这个要复杂得多,不仅仅只通过 Build Configuration 来控制,因为 Release 可能只是 Adhoc 的测试包,并不是线上环境。有的甚至可以在 APP 里面动态切换。那脚本中如何去识别代码里面的配置仍然没有好的办法。

其它思路

例如 stack overflow 里面的这种想法,把对应环境的 GoogleService-Info.plist 配置文件通过 Target Membership 关联到对应的 target 上,前提是你需要根据不同的环境新建多个 target。

Responses
  1. zhenghongshe

    说下我的问题:
    【Build Phases】-【Run Script】顺序要调整一下,保证在【Copy Bundle Resources】后执行。否则会找不到GooleService-Info.plist,进而报错。

    Reply
    1. @zhenghongshe

      是的,Firebase 也建议把 Run Script 放到最后执行。

      Reply