Are you Duplicating a piece of code for a particular functionality across multiple Apps? Do you find it difficult to fix a bug on all those places? If Yes, then Static Library is an answer to your problems.
Balancing Rock, Courtesy: Tide Chambers
When the going gets tough, the Static Library gets going 🚀
‒ Shahrukh Alam
It’s nothing fancy, but a collection of compiled source code files to achieve a particular functionality.
Let’s say we have FileA.swift, FileB.swift & FileC.swift, then we can compile them & wrap them together to form a Static Library which will have an extension of .a (short for Archive).
Simply put, it’s like:
Static Library Packaging
We are going to create a Static Library called Painter which can paint a random color on a View passed by the Client App.
Let’s Build for Simulator, we would see something like this on DerivedData Folder of Xcode.
Let’s Build for Generic iOS Device, we would see something like this on DerivedData Folder of Xcode.
As discussed earlier, Static Library is nothing but:
Static Library Packaging Flow
We can use ar tool to see the Object Files inside the libPainter.a:
Contents of a Static Library
We can use nm tool to see the Symbols (eg: Types & Functions) declared inside the libPainter.a, notice the first one in Painter.O is for our only public function paintRandomColor(onView view: UIView)
Symbols contained in a Static Library
(in continuation to previous steps)
Let’s create a folder named StaticLibrary somewhere safe(say Desktop) & copy the .swiftdoc
& .swiftmodule
files from both the Debug-iphonesimulator & Debug-iphoneos folders of Painter Static Library.
This folder will be needed to integrate the Static Library in the Client App.
It should look something like this now:
.swiftdoc
& .swiftmodule
files basically mimic the Objective-C Header for a Swift Module.
Let’s create a Universal Binary or a Fat Binary which can work for both the iOS Simulator & iOS Device using lipo tool using command:
lipo -create path_to_archive_for_simulator path_to_archive_for_device -output path_for_universal_binary
Important
Universal Binary = libPainter.a from Debug-iphonesimulator + libPainter.a from Debug-iphoneos
Now our StaticLibrary folder should look something like this:
This folder is all that’s needed to integrate the Static Library in the Client App. Now we can distribute this folder to our Clients.
Let’s Copy & Paste the StaticLibrary Folder from the previous step into the PainterClientApp root directory.
Let’s Add libPainter.a to Link Binary with Libraries of App Target Build Settings.
How to link Static library to the App
Let’s Set $(SRCROOT)/StaticLibrary as LIBRARY SEARCH PATHS
& IMPORT PATHS
of App Target Build Settings
LIBRARY SEARCH PATHS
A list of Folders(or Paths) which Xcode(rather Linker) will search for Library(.a) Files. You may already know about FRAMEWORK SEARCH PATHS, it’s similar to that, but for Libraries.
IMPORT PATHS
A list of Folders(or Paths) which Xcode(rather Compiler) will search for additional Swift Modules. You may already know HEADER SEARCH PATHS, It’s similar to that, but for Swift Libraries.
Let’s import Painter
& use the paintRandomColor
method from the Static Library in ViewController.
Let’s Build & Run the App in both the Simulator & Device. We can see different random colors on every launch.
Demo of App using Static Library
Let’s check if Archive also works, if we would see the contents in the Archive by Show Package Contents, then Products -> Applications -> Show Package Contents for PainterClientApp, then we would find no sign of our Painter Static Library. That’s because it’s sitting right inside the App Binary. We will understand this in more detail in the next section.
Let’s check for Show Package Contents for our PainterClientApp first.
Contents of App Package
Even if we just linked libPainter.a to our App Target, it got copied into App Binary. That’s because of Static Linking of the Library.
Static Library Linking
So, now when System loads our App, it loads the Static Library Functionality as a single App Executable. Hence, the faster load/launch time.
Static Libraries are great for code reusability and abstracting things. But, it has few demerits compared to it’s counterparts: Dynamic Libraries & Dynamic Frameworks.
In this post we have learned how to leverage the use of Static Libraries to bundle up common functionalities across multiple Apps, how it will reduce duplicity & help maintainability. We have also put some light on the areas where Static Libraries don’t shine compared to Dynamic Libraries & Frameworks.
Try to figure out the scenarios where you can make good use of them for a Cleaner, Sharable & Maintainable Approach.
You could find the full code on Github: PainterStaticLib, PainterClientApp
This is a free third party commenting service we are using for you, which needs you to sign in to post a comment, but the good bit is you can stay anonymous while commenting.