Integrating Firebase Crashlytics for Multiple Targets in iOS — Part II

Manoj Aher
The Startup
Published in
5 min readJan 31, 2021

In this tutorial, we will be looking at how to handle multiple targets and environments. If you have not integrated Crashlytics in your app I will suggest you have a quick look at Part I of this tutorial.
All set, let's go ahead and handle crashlytics for your app.

Requirement

We had criteria that all the crashes encountered during development and QA/Automation testing should be fixed before the app is released on the store. This ensures a stable app and happy customers.
There are times (a lot sometimes) where the Production user encounters a crash that must be reported and should be fixed on priority (based on the occurrence) and merged in the next upcoming release cycle.

Problem

Having one GoogleService-Info.plist which reports all the crashes in one project

Having just one Project on Firebase and only one app for all the targets was getting messy as it was getting difficult to identify for which target the crash was happening.
I had to integrate Crashlytics for 3 environments and 3 targets where any target may point to any environment. This became difficult for me to manage multiple GoogleService-Info.plist and more difficult for DevOps to release the daily builds.
Here is the list of targets and environments we had to deal with and will be used as an example

Targets
LittleBoy, Master, and Dame
Environment
Development, QA, and Release.

Solution:

I have created 3 projects and for each project, we added 3 application Dev, QA, and release

This goes without saying:
I have used different bundle id’s for all the applications created in the 3 projects

That makes a total of 9 GoogleService-Info.plist.
Download all the plist and put them in the respective folder.
for eg. The plist created for the project Dame for development will reside in Dame --> development.
The arrangement looks something like this

Go to your Project folder and add the Crashlytics to it.

This part is completely optional
Open your Xcode and add the folder Crashlytics to it. But while adding ensure that:
1. copy items if needed is unchecked
2. All Add to targets should be unchecked

These checks ensure that we do not end up copying all the plist into the bundle.

How do ensure that the correct plist is added to the project and we will get crashes for it?

For that, we will be writing a script that takes care of copying the correct Plist for a given Target and Environment to the bundle. I will add the script and will try to explain how it works.

Get the source path

For selecting the correct we need the following things:
1. App Target Name - Dame, LittleBoy, Master
2. Bundle Identifier - We have added all the bundle id’s that would be used
3. Environment - Dev, QA, Release

Based on Bundle identifier we figure out the Environment.
PLIST_LOCATION is created by appending these with the plist name.
Here is an example
TargetName/Environment/GoogleService-info.plist
Dame/Development/GoogleService-info.plist is your source path.

Get the destination path

The destination is simple to create; all we need is BUILT_PRODUCTS_DIR and PRODUCT_NAME. PLIST_DESTINATION is created by appending these paths which is nothing but your bundle path. Once the script is executed you can open the path to check if the correct plist is copied.

I suggest you save this shell-script in the crashlytics folder where you can find all resources for crashlytics. I have named my shell-script file as firebase.sh

Integration with Xcode

Open your Xcode and a new script.

Add Crashlytics/firebase.sh to the script.
This will run the custom shell script after building the target.

Issue:
If your build fails with below error with below message. You need to provide permission to run the shell-script.

/Users/manoj/Library/Developer/Xcode/DerivedData/BaaBlackSheep-fahuktrzsgvbkwehotjoxvfuniuv/Build/Intermediates.noindex/BaaBlackSheep.build/Debug-iphonesimulator/BaaBlackSheep.build/Script-F52C3A13254FC0F000AEA931.sh: line 2: Crashlytics/firebase.sh: Permission denied

Fix:
Run following command on terminal
chmod 755 Path_to_Crashlytics_folder/firebase.sh

Now let’s run and build the app.

In case you encounter errors while building apps, we have added logs at appropriate places. Like when the bundle-id does not match with the bundle-id specified in crashlytics.

Error message when bundle identifier is not valid

Or when the crashlytics GoogleService-info.plist is not present at a valid path or the path is incorrect. Fix these before moving ahead.

Error message when Plist destination path is not valid

If everything works correctly you will see the correct GoogleService-info.plist at the location PLIST_DESTINATION. You can check the build status in the Project navigator to get the path of the installed app.

Success at last!

Let’s verify if the correct plist was copied to the bundle.
You may open the Dame.app from the project and see the Package contents to check if the GoogleService-Info.plist is present.

Verify if the plist is present in the bundle

And that’s how we are done with crashlytics setup for your app for multiple targets.
Yes, it’s easy to add crashlytics to the app. If you still face issues while adding crashlytics let me know down in the comments, I will be more than happy to help you.

Till then, keep learning.

--

--