Making fat static Libraries (Simulator + Device), and applying to Three20

I am starting to get involved with the Three20 project. This project contains valuable classes and UI elements that I need. However, this big library is notorious for its difficulty to include inside an XCode 4 project. The included install script does not work, and the manual install instructions are a miss, and I ended up with Xcode complaining that it can’t find header files, even if I had set up the header search paths correctly.

So, I decided to pre-build the static libraries and include them to my project, and I was successful… to an extent. You see, I couldn’t use the same static libraries for the Simulator and the device, because the libraries built are built each time for the device you specify, and that device only. For example, if you build the libraries for the simulator, the produced libraries will work for the simulator. For the device, you need a different library package.

That led me to the long trip of finding a way to compile a static library for iOS that works for different architectures: armv6, armv7, and i386. Read on to find out how you can manage to make a static library that will work on all platforms.

The first thing we are going to do is to learn to make static libraries that work on both the simulator, iPhone and iPad. Then, we will apply this knowledge to a project of our choice in order to be able to successfully include the Three20 framework to our project.

Step 1: How to build your library for all platforms

  • Open your existing static library project. Select your project file on the top, and then select your target. 
  • Go into the Build Phases
  • Add a new “Run Script” build phase.
  • In this phase, paste the following script:

This script tells Xcode to compile the library for all targets, both for the device and the iOS simulator. Then, it uses lipo() (a very handy terminal command) to take the resulting .a files, merge their architectures, and make a single .a file that will include code for every device.

You can check the resulting file with the following lipo command, which will output the kind of the library and the architectures which is build for:

And… that’s it! We have reached the end of the first part of this tutorial. For those of you who are interested in using this technique in order to be able to implement Three20 in their framework, read on.

Applying this technique to Three20

First of all, you will need to create the libraries and build them for all devices. Follow these steps:

  • Go into Three20 Root Dir-> src -> Three20 and open Three20.xcodeproj
  • Select your project On the top, select the target named “Three20” and add the above script into the appropriate build phase, as you did before.
  • Note that you don’t need to alter the “Three20UnitTests” target. This target is for testing only. It’s not used in any other occasion at all.
  • Now, in your files list, on the left, inside the Three20 project, go into Dependencies. See all the dependencies projects?
  • We need to add the same script phase we did before in all of these projects. Do this for every dependency project. If you don’t do this, Xcode will produce all .a libraries, but only the main Three20 library will be created for all devices. The rest will be device dependent, and will not work for all devices. DO NOT FORGET THIS STEP.
  • Once you have added the svript build phase to all projects and dependencies, build the project.
  • Building will take considerably longer this time. Be patient.
After building completes, Xcode will have produced following for the Three20 project:
  • Libraries for each configuration. They are inside the three20 root->Build->Products
  • Libraries for all devices will be put into three20 root->Build->Products into a folder named ***-universal where *** will be one of “Debug”, “Release” or anything that you had selected in Xcode at the time you pressed build. Note that whatever configuration you had selected at the time does not matter. Inside the universal folder there exist fat libraries, each built for ALL configurations.
  • Necessary headers. Put into three20 root->Build->Products->three20 . This folder is as much important as the universal folder. It contains necessary headers for inclusion in your project.
You can toss every folder except for the universal libraries folder and the headers folder. Now, you will only need these two. All you now have to do, is to go to your project, drag and drop the universal libraries into your project, and include the necessary headers. How you include these headers it’s up to you. You can import them, or you can go into your project settings and set your header include path to be “path-to-three20-root/Build/Products/three20”. I strongly recommend the second method. Note that when using the second method, make sure you DO NOT select the “recursive” button when specifying the path to the headers. Three20’s header files specify relative paths to required headers. By specifying recusrsive header search into Xcode, you basically tell Xcode to ignore relative paths, and that will create conflicts with all headers of the Three20 project. Be very careful with that option in general.

Want the libraries directly?
Here are the universal libraries compiled for the device and simulator. They are for version of Three20, which is the current version at the time these lines are written. I hope this saves people from the frustration of having to do everything themselves. Note that when a new version comes out, you will need to follow the instructions to produce a new version of the libraries. I will not share the new versions of the libraries every time.
Included with the .zip file, there are also the produced headers. You must leave the header directory structure exactly as you see it. Each header contains relative paths to other headers, you don’t want that to be messed up. On the contrary, you can do the actual libraries whatever you want.
I didn’t make the script we use here. I merely modified it, by saying not delete the universal folder each time the script encounters a new target. I found the script into a thread in stack overflow here:

You can find interesting information their, in addition to this script, which is a life saver.