The first post about reducing build times for xcode was written at Reduce React Native XCode build time.
Since then, the author who made xcode-archive-cache
has updated the tool and added support for XCFrameworks.
This allows it to work with react-native
and any XCode project that uses CocoaPods.
Journey
I initially tried this tool but was facing issues when compiling the project. I opened an issue on GitHub with the relevant information to see if it could be fixed.
The author came back smashing with an update that allowed exactly that to happen.
As compared with buildcache, xcode-archive-cache works even better.
Here are the timings I was getting with the KopiRun project on the M1 Macbook Pro.
- Clean Project: 10minutes
- buildcache: 7 minutes
- xcode-archive-cache: 5 minutes
Solve
Create a file called Cachefile
in the ios
folder of your project.
Here's a sample that uses a Notification Service Extension with XCode Schemes.
workspace "kopirun" do
configuration "release" do
build_configuration "Release"
end
configuration "dev" do
build_configuration "Dev.Release"
end
derived_data_path "build"
target "kopirun" do
cache "libPods-kopirun.a"
end
target "notification-service-extension" do
cache "libPods-notification-service-extension.a"
end
end
Create a file called Gemfile
in the ios
folder of your project.
# Gemfile
source "https://rubygems.org"
gem "cocoapods", "1.10.1"
gem "xcode-archive-cache", "0.0.11"
Run the following commands to set it up in your project.
- Change to the
ios
directory - Install
cocoapods
andxcode-archive-cache
based on theGemfile
- Install Pods for the iOS project
cd ios
bundle install
bundle exec pod install
That's it for the configuration. Commit the modifications into your source control repository.
Before you build and archive the app, you can run the following to build the Pods and inject the built artifacts before you archive the app.
bundle exec xcode-archive-cache inject --configuration=release --storage="$HOME/build_cache"
One thing to note is that you do not need to commit the changed project files after you inject the build artifacts. You are supposed to discard the changes. This works very well especially when you need to reduce the time taken for repetitive builds for QA/Regression testing.
You can verify the the time it takes to build by appending the time
command infront of the xcodebuild
command.
time xcodebuild -workspace ios/kopirun.xcworkspace -scheme kopirun -sdk iphoneos -configuration Release archive -archivePath $PWD/ios/build/kopirun.xcarchive
You can use the above configuration in combination with my post on AppCenter caching for Vendor libraries and Speed Up React Native builds in AppCenter
Comments
Dox
11 March, 2022 at 4:03 PM
Hey! Nice article. I tried your cacheclean implementation first, do you think I should remove it? Can they both work together?
Thanks!
Zane
12 March, 2022 at 2:03 AM
That's a mighty good question Dox. Honestly I never tried both. I think it might work but I would caution the use of multiple level of caches as you might end up with some unwanted behaviour. I think using just xcode-archive-cache is good enough for me.
Dox
15 March, 2022 at 3:03 PM
Thanks Zane! Allright, so, for removing the other implementation I just have to remove the post_install script, right?
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings["CC"] = "clang"
config.build_settings["LD"] = "clang"
config.build_settings["CXX"] = "clang++"
config.build_settings["LDPLUSPLUS"] = "clang++"
end
end
And, another one; Im having this error on xcode with the xcode-archive-cache implementation:
Library not found for -lPods-exampleapp
How can I reset the cache for this implementation?
Thanks
Zane
16 March, 2022 at 1:03 AM
Yes, please remove the changes you made for the ccache implementation.
I actually linked the xcode-archive-cache issue ticket that has instructions on how to get you setup for a bare react native project, did you try that yet?
The cache for this implementation is only meant to be used to build and you discard all the changes it makes to your xcode projects afterwards. Do not commit the changes to those files into the git repo.
At which point do you get that error message?
Dox
16 March, 2022 at 4:03 PM
Thanks for the quick response!
Im trying to debug the error still, I think its because of a cache error, huh!
When you say to not commit the files, you mean the Gemfile and the Cachefile?
Zane
16 March, 2022 at 5:03 PM
If you can share a sample project that replicates that error via GitHub, I can take a look.
No. Those files you'll have to commit. I meant the `project.pbxproj` that gets modified by the xcode-archive-cache command