How to compile a project with app and library in the same workspace with different configuration names?

8.9k views Asked by At

I am developing an app and I am using an open source component.

I have a workspace containing both MyApp.xcodeproj and Component.xcodeproj. My app has three configurations: Debug, App Store and In House but the component has only two: Debug and Release

In the Debug configuration, everything works fine, but I can't compile my app in App Store or In House configuration because the configuration names do not match. I get a file not found error when trying to #import <Component/Component.h>

I need both App Store and In House configurations and I would really like to avoid modifying the component's configurations in order to ease future updates of the component.

I know I could use CocoaPods to solve this issue but I would like to know if there is a simple solution in Xcode

3

There are 3 answers

13
0xced On BEST ANSWER

You can get your project to compile with some tweaks to your app’s settings.

I suggest you to modify all settings at the project level so that all your targets can inherit these settings.

  1. Add a new DEFAULT_CONFIGURATION user-defined setting and define your configuration mapping. This is how it should look like:

    Default Configuration Screenshot

  2. Set FRAMEWORK_SEARCH_PATHS to $(BUILD_DIR)/$(DEFAULT_CONFIGURATION)-$(PLATFORM_NAME) for all configurations, add Any OS X SDK variants and set the value to $(BUILD_DIR)/$(DEFAULT_CONFIGURATION). Set HEADER_SEARCH_PATHS to $(FRAMEWORK_SEARCH_PATHS)/include and LIBRARY_SEARCH_PATHS to $(FRAMEWORK_SEARCH_PATHS). This is how it should look like:

    Search Paths Screenshot

    This step is quite tedious, it can be automated with the xcproj tool and by running this script in your project directory. Edit your configurations mapping as needed.

    #!/bin/bash
    
    CONFIGURATIONS=( "App Store:Release" "In House:Release" "Debug:Debug" )
    
    for CONFIGURATION in "${CONFIGURATIONS[@]}"; do
        xcproj --configuration "${CONFIGURATION%%:*}" write-build-setting DEFAULT_CONFIGURATION "${CONFIGURATION#*:}"
    done
    
    xcproj write-build-setting 'FRAMEWORK_SEARCH_PATHS' '$(BUILD_DIR)/$(DEFAULT_CONFIGURATION)-$(PLATFORM_NAME)'
    xcproj write-build-setting 'FRAMEWORK_SEARCH_PATHS[sdk=macosx*]' '$(BUILD_DIR)/$(DEFAULT_CONFIGURATION)'
    
    xcproj write-build-setting 'HEADER_SEARCH_PATHS' '$(FRAMEWORK_SEARCH_PATHS)/include'
    xcproj write-build-setting 'LIBRARY_SEARCH_PATHS' '$(FRAMEWORK_SEARCH_PATHS)'
    
  3. If the component is distributed as a static library, you are done here. If the component comes as a framework you have to update its path reference by editing your project.pbxproj file in a text editor. In the PBXFileReference section (under /* Begin PBXFileReference section */) find Component.framework and update its path like this:

    name = Component.framework; path = "../$(DEFAULT_CONFIGURATION)/Component.framework"; sourceTree = BUILT_PRODUCTS_DIR; };
    

    Also make sure that the sourceTree is set to BUILT_PRODUCTS_DIR, i.e. relative to built products. Once you edited the project file, this should look like:

    Location Screenshot

Your project should now build as expected.

2
Vignesh On

I solved it in the following way,

In my dependency project, Project -> Target -> Build Phases, added new Run Script

TARGET_DIR="Build"
TARGET_FILE="${TARGET_DIR}/${FULL_PRODUCT_NAME}"
mkdir -p ${TARGET_DIR}
rm -rf ${TARGET_FILE}
cp -rf "${BUILT_PRODUCTS_DIR}/${FULL_PRODUCT_NAME}" ${TARGET_DIR}

After the every build, I copy the build to my Build directory inside the project. And from the main project, Project -> Target -> General, I drag and dropped the dependency Framework (which is inside dependency project's Build folder) then, Project -> Target -> Build Settings -> Framework Search Paths, under my custom Build Configuration, I added the following

$(PROJECT_DIR)/../DependencyProject/Build

The folder is relative to my main project, and this should be relative to your project. Then in my all the schemes, I added a Pre-actions script for Build with

rm -Rf "${PROJECT_DIR}/../DependencyProject/Build"

Also you need to select the app against Provide build settings from drop down.

0
gpios On

I had this same problem, but I had multiple configurations (Debug, TestFlight, Release, Enterprise) in my app, and the bolded configurations would always fail to build cause it couldn't find the frameworks. I really didn't want to mess with the project settings of my sub projects in order to make updating them easy.

The answer I found was to just update the framework search path to account for the fact that the frameworks would be dropped in Release (or whatever the default configuration is set to) of the sub projects.

Specifically I set it to: $(BUILD_DIR)/Release-$(PLATFORM_NAME)

I set it to be recursive too. This works both for Simulator and Device, building in Xcode and command line.