If you're using Firebase Firestore in your Flutter project and don't know why your iOS builds take so much time to compile, you're not alone, this problem is ancient and most developers are suffering through it but don’t worry, I got you covered.
On my machine, M3 Pro MacBook with 18 GB of RAM and a fast SSD, my iOS build time was around 750 seconds for first build (after adding firestore in pubspec.yaml
and running flutter clean && flutter pub get
).
Digging into this issue, I found the culprit, and it was Firestore library itself. Though the download size might not be that big of an issue as it can be reduced with high speed internet connectivity but library core is. We will discuss the problem in detail later in this article.
Only available solution right now is to move towards precompiled firestore library for iOS. Switching to precompiled library reduced my iOS build time to just 142 seconds, massive 81% reduction in the compile time for iOS.
Before we dive into the step by step guide, we should first go through the actual problem itself that causes such long compile time for iOS.
Firestore’s C++ Core - The Problem
Actual problem is the massive size of core C++ library on which flutter firebase package is built upon. Flutter firestore package depends massively on C++ library which is consists of around 500,000 lines of code and when you add the package into your pubspec.yaml
file, the cocoa pods have to download this massive library from the source first and then compiles this into flutter library, this building of library using the core C++ is the actual time consuming culprit and will take longer iOS compile time regardless of how powerful your hardware is.
Using Precompiled Library - The Solution
The FlutterFire team introduced a precompiled version of firestore library which you can directly connect with pods, it skips C++ code conversion process and reduces the build time drastically for iOS.
Let’s now dive into step by step guide on how to integrate precompiled firestore library into your flutter project and save your time.
Adding Precompiled Firestore Library
1. Find Correct Version To Use
First of all you need the correct version of precompiled firestore library to download. Open ios/Podfile.lock
and look for line similar to - Firebase/Firestore (= 11.15.0)
and note the version, in my case, it is 11.15.0, it will be used in tag
when connecting the precompiled library to indicate the correct version (incorrect/mismatching versions may lead to compile time or run time errors and might become difficult to trace back).
2. Add Library In Your Podfile
In your Flutter project, open ios/Podfile
and add pod 'FirebaseFirestore', :git => 'https://github.com/invertase/firestore-ios-sdk-frameworks.git', :tag => '11.15.0'
in the target ‘Runner’
block like below:
target 'Runner' do
use_frameworks!
use_modular_headers!
# Precompiled Firestore SDK:
pod 'FirebaseFirestore', :git => 'https://github.com/invertase/firestore-ios-sdk-frameworks.git', :tag => '11.15.0'
flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
target 'RunnerTests' do
inherit! :search_paths
end
end
Notice that the version that you copied earlier, it is used right after :tag
and this indicates the library version that will be downloaded.
3. Clean Your Project
Clean the project by running flutter clean
4. Switch to iOS Directory
Switch to iOS directory by running cd ios
5. Clear All Pods
Clear the pods and ios/Podfile.lock
by running rm -rf Pods Podfile.lock
6. Update Pods Repo & Install the Pods
Update pods repository and install the pods by running pods install
with --repo-update
flag, final command to run will look like pod install --repo-update
7. Download Libraries
Before you can download libraries, you need to switch back to root directory of the project, to do so, run cd ..
and then run flutter pub get
If you have followed the steps correctly, you will see in the console that firestore library will be downloaded separately from the precompiled source mentioned in ios/Podfile
and then rest of the libraries added in your project’s pubspec.yaml
will be downloaded
8. Run Project
Once libraries are downloaded successfully, run the project on any iOS simulator or real iOS device, you will see a drastic reduction in the compile time of your iOS build than before. It might not exactly be 81% like me as hardware and many other factors come into play as well but the reduction will be significant enough for you to notice clearly.
Can You Run flutter clean
?
Good news is, yes, you can absolutely run flutter clean
without having to download pods again because the downloaded precompiled version of library was done through the ios/Podfile
instead of pubspec.yaml
which store the library in the pods and running flutter clean
does not remove the pods from your project.
Are There Precompiled Libraries For Other Firebase Packages As well?
No, there are no precompiled versions for other firebase libraries for iOS but good that is a news because adding other firebase libraries like messaging, remote config or storage does not significantly increase the build time for iOS, making it completely impractical to publish the precompiled versions for rest of the firebase libraries.
Do I have to Worry About Build Size?
No need to worry about iOS app build size just because precompiled firestore library was added directly into the pods using ios/Podfile
as flutter framework removes extra code for release builds and results in reduced release app size.