When you go to add a Firebase configuration file into your iOS project you’ll find that Google explains the most basic use case but says nothing about more advanced configurations. From their documentation they outline a two-step process:

A screenshot of Google documentation on firebase configuration. It describes a two-step process which involves moving a configuration file

Their documentation even says “if prompted, select to add the config file to all targets” which… depends. There is a blurb at the bottom which touches on the problem

If you have multiple bundle IDs in your project, you must associate each bundle ID with a registered app in the Firebase console so that each app can have its own GoogleService-Info.plist file.

Then they don’t talk at all about how you would go about doing that!

“GLHF — XO Google”


Here's what I did

Initial setup

Choose your bundle identifiers. Example: com.example.debug for your Debug configuration and com.example for your Release configuration.

Initialize your firebase projects and apps (you may only have one project and two apps, or two projects each with one app — dont get me started on that one) and download your GoogleService-Info.plist files.

Setup Xcode

Create a Firebase folder inside your target directory. Copy your GoogleService files into your new Firebase folder. The names don’t matter — but choose identifiable names that make sense to you.

An Xcode window with an example project named 'Firetruck', with its folder structure visible. There's a Firebase folder with two plist files, one named GoogleServices-Debug.plist, one named GoogleServices-Release.plist

In Xcode navigate to Build Settings for your specific target. Add a user-defined setting named GOOGLESERVICE_PATH. Configure the value for each setting as the local path to the google service info plist file.

The build settings for the example 'Firetruck' project in Xcode. There's a user-defined setting added, with multiple values. There are two values present, 'Debug', pointing at the Firetruck/Firebase/GoogleServices-Debug.plist file, and 'Release', pointing at the Firetruck/Firebase/GoogleServices-Release.plist file

Navigate to Build Phases for your target. Create a new Run Script Phase. This script will copy the referenced GoogleService resource into the compiled app. The script will

produce an error if the value of GOOGLESERVICE_PATH is empty, missing, or points to a location where the file does not exist

echo "Google Services Plist Path: ${GOOGLESERVICES_PATH}"

if [ -z "${GOOGLESERVICES_PATH}" ]; then
  echo "GOOGLESERVICES_PATH is unset or set to the empty string"
  exit 1 
fi

if [ ! -f "${GOOGLESERVICES_PATH}" ]; then
  echo "GOOGLESERVICES_PATH does not exist"
  exit 2
fi

PLIST_DESTINATION=${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app
cp "${GOOGLESERVICES_PATH}" "${PLIST_DESTINATION}"

echo "Copied ${GOOGLESERVICES_PATH} to final destination: ${PLIST_DESTINATION}"

An Xcode window listing the Build Phases of the example Firetruck project. It lists: Target Dependencies, Run Build Tool Plug-ins, Compile Sources, Link Binary With Libraries, Copy Bundle Resources, and Install Google Services Plist, which is expanded. It has the code mentioned above added as a bash script.

Access The Value From Fastlane

You can read the value of the xcode build setting (the local path to your GoogleService-Info.plist file) using xcode_build_setting

Add the fastlane plugin to your Gemfile

gem "fastlane-plugin-xcode_build_setting", git: 'https://github.com/UpBra/fastlane-plugin-xcode_build_setting.git'

Run the fastlane action provided by the plugin

xcode_build_setting(
  project: 'Firetruck.xcodeproj',  # path to your xcode project
  target: 'Firetruck',             # the name of your target
  configuration: 'Debug',          # xcode configuration
  key: 'GOOGLESERVICES_PATH'       # name of your user defined build setting key
)

# Display the result
UI.message lane_context[SharedValues::XCODE_BUILD_SETTING_VALUE]

If the other fastlane actions you want to run only require a path to your GoogleService-Info.plist file — your work is done. If you’d like to read values from inside the plist file you can use google_services_plist_values

Add the plugin to your Gemfile

gem 'fastlane-plugin-google_services', git: 'https://github.com/UpBra/fastlane-plugin-google_services.git'

Run the fastlane action provided by the plugin

google_services_plist_values(
  path: lane_context[SharedValues::XCODE_BUILD_SETTING_VALUE]
)

# Display the Google App Id
UI.message lane_context[SharedValues::GOOGLE_SERVICES_PLIST_VALUES_GOOGLE_APP_ID]

An Xcode window showing an open file named Fastfile in a fastlane directory. Underneath teh file, there's a terminal with the output of running the fastlane action printed. It shows a table with a fastlane summary.

This article was originally posted by Blair on Medium