If you are using Unity to build your iOS projects and use SDK’s like Firebase, Facebook etc. which require CocoaPods, then these steps enlisted below should help you run a smooth and successful build.
Even if you are not using Unity and just using Xcode project, these steps should ensure that the pods get installed in your project successfully.
Once everything is setup, the builds usually almost always run successfully and rarely give problems. It is mostly during the first build or sometimes after an OS update or after a hard disk change or moving to a new machine, that problems creep up and the Unity build fails with the message – “iOS framework addition failed due to a Cocoapods installation failure. This will will likely result in an non–functional Xcode project.“. It is a long error message in the Console which would probably state how even the “pod repo update” was attempted and it failed as well.
For the people who aren’t aware how Pods work and not familiar with the Xcode CocoaPods workflow, this error is very hard to decipher and fix.
If you have encountered this error, follow the steps below to fix it and setup CocoaPods correctly.
The setup used for the purpose of this writeup:
MacOS Catalina (10.15.6)
Unity 2018.4.22f1
Xcode 11.4.1
CocoaPods 1.9.1
1) Install Xcode.
Pretty self explanatory and in all probability you should have done this step already. Xcode can be installed from the Mac AppStore or by downloading a xip file from the apple developer site.
2) Sudo xcode-select
Now we have to ensure that our system knows where our Xcode is and specifically where the developer tools which come bundled with it are installed.
Open Terminal on your Mac and run the following command.
sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer
Please note that the path to Xcode on your machine may vary. The path listed above is the default location of Xcode. You may have installed or moved Xcode to a new location. In that case, please run the command with the appropriate path.
Sometimes, just this magic command can make your build run fine. If it doesn’t, follow the next steps to install and correctly setup CocoaPods on your system.
3) CocoaPods installation and setup.
If your build is failing, the most likely cause is that CocoaPods is not installed and setup properly. So follow the steps below and by the end of it, CocoaPods would be installed successfully on your system.
First lets remove any previous installations so that we can make a fresh start. Open a Terminal and run the command below. If there was a previous installation (or more than one installations) this will remove them. Type “y” and press “Enter/Return” on any prompts that this command makes.
sudo gem uninstall cocoapods
Once you have uninstalled the previous version (or if no previous version exists) proceed to install cocoapods. Run the following command on the Terminal.
sudo gem install cocoapods
Let the process complete successfully. This should install the latest version of CocoaPods on your system.
But the installation process isn’t complete. CocoaPods is basically a repository of frameworks and their dependencies. You just have to specify which “pods” you want in your project in a podfile and it installs them. But in order for it to work, the entire repository needs to be on your system in a specific folder. There are several ways to do that. The first time you run a pod install, it will download the repository before proceeding. In older versions of CocoaPods, you can run pod setup and it should do the job (since version 1.8.0, pod setup does nothing!). Or in some cases pod repo update might work.
But I am going to list the method that I use which works for me every time.
First we need to create a folder. Go to your user folder using Finder Explorer. Look for a folder name .cocoapods. (It is a hidden folder since the name starts with a dot). If you can’t see the folder, press Command+Shift+. to reveal the hidden folders. If the folder doesn’t exist, then create one with the name. Open the folder and look for a folder named repos. If the folder doesn’t exist, create one by that name. Open the repos folder. If there is a master folder inside it, then delete it. We are going to recreate it. Your folder structure should look like this :
Once you have got this open the terminal and run the below commands one by one.
cd ~/.cocoapods/repos
git clone --depth 1 https://github.com/CocoaPods/Specs.git master
pod repo update
The git command should take a few minutes to complete as it downloads the repository to your system. Ensure all the commands listed above are completed successfully. Your folder structure should look like this.
Voila! Now you have completed the installation correctly. Go back to Unity and build for iOS again. And this time the build should succeed without any errors!
4) Use .xcworkspace file instead of .xcodeproj
If you are new to using Pods, then you probably won’t even be aware of this. If you are experienced, there is still a chance that you might forget this.
Always, use the .xcworkspace file to open your project in Xcode and not the .xcodeproj which you are probably used to.
Once the unity build process completes and the pods are installed successfully, notice that the generated Xcode project now has a .xcworkspace file alongside the usual .xcodeproj file. Use the .xcworkspace file to open you project in Xcode. Using the .xcodeproj file to open the project will lead to errors when building the Xcode project.
4a) There is no .xcworkspace file in my Xcode project!
Relax. This may not be a problem. Unity also has the ability to load pods from .xcodeproj file as well. The External Dependency Manager used by Unity to manage pods has the option to add pods to either to the workspace or the project file as shown in the image below. Check which setting you are using by going to the following location in Unity.
Assets -> External Dependency Manager -> iOS Resolver -> Settings
If there is no workspace file in your project, try building the project using the .xcodeproj file. If it doesn’t work, try changing the iOS Resolver Setting and generate a .xcworkspace file.
5) Keep CocoaPods updated.
Everything might work correctly for some time and then suddenly you might face the pod installation error again. This might be because the repo could not be updated due to some error. Try running the ‘pod repo update’ command manually again and see if it helps.
Also it might be possible that your CocoaPods version is outdated and one or more of your pods require a higher version of CocoaPods. In that case, just run the below command (same command which was used for installing) and it will update you CocoaPods to the latest version.
sudo gem install cocoapods
There is a strange bug currently if you are using Unity + Firebase where the Xcode build will fail with error –
Unable to read Google Service plist at path….
This error is surprisingly being caused because of spaces in build folder name! To fix this error, just rename your build folder without any spaces and then run the build again. It should run successfully now.
Refer to below links:
https://github.com/firebase/quickstart-unity/issues/557
https://github.com/firebase/quickstart-unity/issues/714
Some parting info on CocoaPods
There’s a lot of information on CocoaPods out on the internet, some old some new. A lot of questions being answered on StackOverflow. If you have been going through all the material, it can cause quite a lot of confusion. Especially with the new versions of CocoaPods not requiring the master repository on your system. Let me help you out with that.
As of CocoaPods version 1.8.0, there is no mandatory requirement for the master repository on your system. However, this also requires a change in the projects podfile to change the source of download. When testing with Unity 2018.4.22f1, I found the podfile still tries to download from the master repository and hence the master repo is required on the system. If the repo is not present, the Unity build process will try to download and install the repo which may fail due to network issues or some other error and will cause the build to fail. So it is better to have the master repository for now. In the future, when the new methodology is stable and becomes the norm, we probably won’t need the master repository.
When running into issues, a lot of old answers on internet can point you to “pod setup” to solve the problem. Let me reiterate that as of version 1.8.0, this command does nothing and just prints a completed message. Hence don’t rely on it.
“pod repo update” command can be run to keep the master repo updated but it requires the master repository correctly configured to work. It especially requires the .git folder inside the master folder to be configured correctly. This command can be run on Terminal from any folder.
“pod install” command is used to install the pods in your project. It will also download and install the master repository if it is not present. This command can only be run from a folder which has a valid podfile.
Hopefully you won’t run into any issues if you follow the above mentioned steps correctly and won’t need any additional help.
I am no expert on CocoaPods. But I have had to fiddle around to get my project to build with Google Firebase SDK and Facebook SDK. Hopefully this writeup will help any one else coming across the same problems.
Did all of this and I still get the same error message, and no .xcworkspace file is built.
Oh! That’s not good. Have you confirmed that the cocoapod is installed perfectly and the folder structure matches the one in the pic. Also, what’s the error message that you get in the console? Maybe there is some other error which is preventing the pod installation. Check your Xcode location as well and if it isn’t in the default location, run the xcode-select command from above with the right path on your machine.
@Shobhit Samaria – Thanks for the post. Have you by chance gotten Firebase to work on 2019.3+? It seems Unity changed some things and we ran into some troubles.
@Mathias Grønstad – Have you tried looking in iOS Resolver -> Settings to see if you are set to generate an .xcworkspace?
Hi Joey
I primarily work with Unity 2018 but I just tried 2019.4.3 (latest LTS release) and I was able to successfully build my project and run it via XCode. I did not face any issues. Are you facing any build related issues? Or is it a specific Firebase related issue? Also, I was using Firebase through the Package Manager, if that helps.
Hi Shobhit,
Here’s what we were running into: https://forum.unity.com/threads/unityframework-load-in-2019-3-11-causes-watchdog-transgression-crash-0x8badf00d.878620/
We’ve downgraded to Unity 2019.2 in the meantime. 🙂 Thanks again for the post.
That seems like a nasty bug. I guess there is little that you can do except to downgrade Unity. Did you try to upgrade Unity to the LTS version? the 2019.4.4 came out yesterday. Maybe they have fixed the issue in one of the new releases. Anyways, I think I will stick to Unity 2018 for a little while longer. 🙂 Thanks for making me aware of this issue.
Hi Shobhit,
Thank you for the information. I got as far as: git clone –depth 1 https://github.com/CocoaPods/Specs.git master
Unfortunately it fails as about 40% on Receiving objects with the message: error: RPC failed; curl 18 transfer closed with outstanding read data remaining
I have tried multiple times over the few hours, but no luck. In desperation I tried downloading straight from the github page ready to copy into position, but the .git folder and subfolders are missing.
Do you have any suggestions please?
Andy
Hi Andy
I haven’t faced this issue before but I googled your problem and it seems it has to do something with internet connectivity. You can try steps mentioned here – https://stackoverflow.com/questions/38618885/error-rpc-failed-curl-transfer-closed-with-outstanding-read-data-remaining/38703069#38703069 to fix your problem (using ssh instead of https).
Alternatively you can try to get the .git folder by using the following command –
git clone –depth 1 –no-checkout https://github.com/CocoaPods/Specs.git master
This should create the .git folder. Then you can download the rest of the repository as a zip file (which you have probably done) and paste the contents in the master folder. The finally run “pod repo update” and see if everything works.
I haven’t tried these methods myself as I haven’t run into this issue but I do hope it works for you.
Shobhit
Thank you Shobhit,
I tried the alternate command as it seemed the easier option. It failed in the same way. The stackoverflow option didn’t work, but maybe I did something wrong. The operation was timing out and then asking to make sure I had the correct access rights.
However, you mentioning internet connectivity got me to try tethering my phone for internet connectivity instead of broadband. That worked and so I could use your original instructions to solve the issue.
Now I have another issue with my Xcode build, but that doesn’t appeared related to this, and I feel I have made a lot of progress today.
Thank you for your help.
Andy
Hi Andy
Good to hear that your problem is resolved. Hope your Xcode build works as well. I have faced some silly bugs while doing Xcode build including the build failing because of spaces in the folder name (https://github.com/firebase/quickstart-unity/issues/557)! I hope it is not the same for you.
Anyways good luck with your project.
Shobhit
Hi!
Thanks you for the info!
I am trying to get unity to generate workspace from batch (not cloudbuild, just to build from command line on my mac)
I have firebase in my project, when I build normally, I get the workspace, but when I build from command line, I only get the project file…
Do you know why?
the command I use to build:
/Applications/Unity/Hub/Editor/2020.1.13f1/Unity.app/Contents/MacOS/Unity -batchmode -quit -projectPath {PATH-TO-PROJECT} -executeMethod ExportTool.ExportXcodeProject logFile “export.log” -username {USERNAME} -password {PASSWORD} -serial {UNITY_SERIAL}
My export method:
class ExportTool
{
static void ExportXcodeProject()
{
EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTargetGroup.iOS, BuildTarget.iOS);
EditorUserBuildSettings.symlinkLibraries = false;
EditorUserBuildSettings.development = false;
EditorUserBuildSettings.allowDebugging = false;
List scenes = new List();
foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes)
{
if (!scene.enabled)
continue;
scenes.Add(scene.path);
}
BuildPipeline.BuildPlayer(scenes.ToArray(), “{PATH_TO_PROJECT}”, BuildTarget.iOS, BuildOptions.None);
}
}
Hi
It does seem you are doing everything correct but unfortunately I do not have sufficient knowledge of command line tools to offer you a solution for your problem. I suggest posting on the unity forums and maybe the unity folks will have an answer. Try playing around with the iOS resolver settings or check if there is something in the logs. I am really sorry that I could not be of much help. Please do let me also know once you figure out a solution so that it can be helpful to me and others in the future. Good luck friend!
Regards
Shobhit
Hi Shobhit,
Thanks for the info you have provided. When I am running the command to install cocoa pods – I get the following error (can you help?):
% sudo gem install cocoapods
Building native extensions. This could take a while…
ERROR: Error installing cocoapods:
ERROR: Failed to build gem native extension.
current directory: /Library/Ruby/Gems/2.6.0/gems/ffi-1.15.0/ext/ffi_c
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby -I /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0 -r ./siteconf20210326-652-nsc43l.rb extconf.rb
checking for ffi.h… *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.
this is followed by some items and then…
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:467:in `try_do’: The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:585:in `block in try_compile’
Hi
While I haven’t run into this error, but it does say “You have to install development tools first.” at the end. It might be that your Xcode development tools have not been installed. Maybe you don’t have Xcode installed or it is not in the default location (use the sudo xcode-select command mentioned in the article to point to the correct location)? If you have Xcode installed, try running it first time and it will ask to install some additional components (maybe that’s what it is missing?). Also you could try the “xcode-select –install” command to try to install the developer tools if they are missing. Try these few things out and let me know if it works out.
Regards
Shobhit
I spend almost 5 hours in internet searching answers, finally this worked for me, Thanks a lot, you made my day.
Regards
Junior Developer
Glad that the article could be of help to you.
Regards
Shobhit
I got a new Macbook Pro and have been setting it up with all the tools. On my previous computer, I can build for iOS withut any issues (after setting it all up in the past). Howeverk, with my new computer I’m still getting the Unity error about cocoapods even after following all the steps I’ve done in the past, plus your advice here. I can still get the xcodeworkspace to load and actually do the xcode build, but it’s a pain in the butt that Unity always ends with an error so “Build and Run” won’t work.
BTW, I’m using Unity 2020.3.7 currently for these particular projects.
That is quite unfortunate. I have heard a few people complaining on social media about issues with Unity on the latest macs. I suggest that you try to post your error on the Unity Forums and see if anyone from Unity can help. Try to post the complete build error, maybe there is a clue somewhere that someone else can pick up. Sometimes the actual error is hidden quite deep. Otherwise, it looks like a Unity bug, so you might have to raise a bug report. Also, try a few things such as upgrading/downgrading Unity and see if that works out. Maybe the bug is fixed in some later version (check release notes of the new versions and see if you can find something). Try upgrading any third party SDK’s as well. Double check all the steps for cocoapods installation juts to make sure. Sometimes it is as simple as Xcode not being in default location.
I would also like to point out that the Unity completes the iOS build even if there is a Cocoapods failure. But it is very likely that the third party sdk’s which depend on it might not work correctly in the final xcode build or the build itself will fail. So do try to fix this issue before moving on and building your Appstore builds from Xcode.
Hi, i follow the steps but i have a problem, when i press the install the cocoapods in the external dependency manager it says ‘gem install cocoapods –user-install’ succeeded but the pod tool cannot be found., it also says that the bin is not in the path, any help?
Hi Daniela
Can you try to install cocoapods outside of the exernal dependency manager? Do not use the “install cocoapods” option from the external dependency manager, instead maybe try to follow the steps in the article (step 3). Hopefully that should install cocoapods successfully on your system.
yes, i follow all the steps to install cocoapods on my pc, however when i tried to add it to the unity project is when this message pops up
Many many thanks for your great guide, it. help me and saved me many additional frustrating hours
+1