Slim down your iOS app by excluding files from production builds
If you use additional files or libraries during development like HockeyKit or TestFlight you’ll want to exclude those in your production app store builds. Likewise, if you have an iPad and iPhone app that share the same Xcode project but are published under separate app IDs, this technique is handy for excluding iPad assets from your iPhone builds and vice-versa.
You won’t find this feature in Apple’s documentation. But it does exist in the form of a user-defined build setting called EXCLUDED_SOURCE_FILE_NAMES and it’s a really handy feature. I’ll use TestFlight in this example but you can use this approach to exclude any files from a build:
Excluding TestFlight’s shared library from the Release build configuration
- Open your project file. At the bottom of the screen click Add Build Setting -> Add User Defined Setting and name it EXCLUDED_SOURCE_FILE_NAMES.
- Click the arrow to the left of EXCLUDED_SOURCE_FILE_NAMES to expand it. In the Release configuration for this variable, add libTestFlight.a and any other files you’d like to exclude (separated by spaces).
Including TestFlight code only in the Debug build configuration
The problem now is that your release target won’t build. You need to surround any code that depends on the excluded files with a macro that checks to ensure it’s only included in the Debug configuration. You can use #ifdef DEBUG
for this, but in this case we’ll create a macro specific to TestFlight.
- Open your project file and navigate to Targets -> Your app target -> Build Settings -> Preprocessor Macros
- Add a new preprocessor macro
TESTFLIGHT=1
to the Debug build configuration.
Now we’ll prevent TestFlight code from being compiled if the TESTFLIGHT
macro isn’t defined:
[TestFlight takeOff:@"testflightkey"];
#endif
Now when you build your production binary, if all goes well, you’ll see that the resulting ipa file is quite a bit smaller since you’re no longer including libTestFlight.a!
Similarly, to exclude iPhone-specific files from your HD iPad builds, just add them to the EXCLUDED_SOURCE_FILE_NAMES
for your iPad build configuration.
3 Comments
Jules
January 21, 2012Shouldn’t that be adhoc instead of debug ?
Christopher Pickslay
January 21, 2012Jules, do you mean the build configuration name? It depends how your project is set up. The defaults for an iOS project are Debug and Release, and people generally use the Debug configuration for testing and the Release configuration for app store submissions. Most mature projects will have more build configurations. The point is that you don’t want to compile code in a configuration that doesn’t include the supporting files.
Jules
January 22, 2012When I archive my build, and then share, select either release or debug ( pr for me ad-hoc) and save the file.
Shouldn’t the release file be a smaller file than the debug / adhoc file ?
Confused.