歡迎到我的 個人博客 http://liumh.com 瀏覽此文
與公司 QA 聊天,已不止一次被吐槽說移動端從開發環境轉到生產環境時,還要靠修改代碼來配置對應的環境參數。她認為,從 App 轉測試之后,就不應該再修改代碼,可以把所有的環境配置都整合到配置文件中,這樣打不同環境下的安裝包時,會自動選擇對應的環境參數。這里說到的環境參數包括但不僅限于: webservice 地址,友盟 AppKey,極光推送 AppKey 和是否是生產環境標志等。
其實,我也討厭修改環境參數啊,??
為達成上述目的,主要是使用 Xcode 的 Configurations Setting File(即后綴為 xcconfig 文件) 來配置開發不同階段下的環境。本文包含的內容如下:
- Xcode Target
- Xcode Project
- Build Setting的繼承關系
- 如何使用xcconfig文件來配置不同開發階段的環境
包含了一些與 build settings 相關的知識。
Xcode Target
target, 官方文檔如下解釋:
A target specifies a product to build and contains the instructions for building the product from a set of files in a project or workspace. A target defines a single product; it organizes the inputs into the build system—the source files and instructions for processing those source files—required to build that product. Projects can contain one or more targets, each of which produces one product.
target 定義了生成的唯一 product, 它將構建該 product 所需的文件和處理這些文件所需的指令集整合進 build system 中。Projects 會包含一個或者多個 targets,每一個 target 將會產出一個 product.
The instructions for building a product take the form of build settings and build phases, which you can examine and edit in the Xcode project editor. A target inherits the project build settings, but you can override any of the project settings by specifying different settings at the target level. There can be only one active target at a time; the Xcode scheme specifies the active target.
這些指令以 build setting 和 build phases 的形式存在,你可在 Xcode 的項目編輯器(TARGETS->Build Setting, TARGETS->Build Phases)中進行查看和編輯。target 中的 build setting 參數繼承自 project 的 build settings, 但是你可以在 target 中修改任意 settings 來重寫 project settings,這樣,最終生效的 settings 參數以在 target 中設置的為準. Project 可包含多個 target, 但是在同一時刻,只會有一個 target 生效,可用 Xcode 的 scheme 來指定是哪一個 target 生效.
A target and the product it creates can be related to another target. If a target requires the output of another target in order to build, the first target is said to depend upon the second. If both targets are in the same workspace, Xcode can discover the dependency, in which case it builds the products in the required order. Such a relationship is referred to as an implicit dependency. You can also specify explicit target dependencies in your build settings, and you can specify that two targets that Xcode might expect to have an implicit dependency are actually not dependent. For example, you might build both a library and an application that links against that library in the same workspace. Xcode can discover this relationship and automatically build the library first. However, if you actually want to link against a version of the library other than the one built in the workspace, you can create an explicit dependency in your build settings, which overrides this implicit dependency.
target 和其生成的 product 可與另一個 target 有關,如果一個 target 的 build 依賴于另一個 target 的輸出,那么我們就說前一個 target 依賴于后一個 target .如果這些 target 在同一個 workspace 中,那么 Xcode 能夠發現這種依賴關系,從而使其以我們期望的順序生成 products.這種關系被稱為隱式依賴關系。同時,你可以顯示指定 targets 之間的依賴關系,并且這種依賴關系會覆蓋 Xcode 推測出的隱式依賴關系。
指定 targets 之間的依賴關系的地方在 Project Editor->TRAGETS->Build Phases->Target Dependencies 處設置。如下圖所示:

Xcode Project
官方文檔的解釋如下:
An Xcode project is a repository for all the files, resources, and information required to build one or more software products. A project contains all the elements used to build your products and maintains the relationships between those elements. It contains one or more targets, which specify how to build products. A project defines default build settings for all the targets in the project (each target can also specify its own build settings, which override the project build settings).
Xcode project 是一個倉庫,該倉庫包含了所有的文件,資源和用于生成一個或者多個 software products 的信息。它包含一個或者多個 targets,其中的每一個 target 指明了如何生成 products。project 為其擁有的所有 targets 定義了默認的 build settings,當然,每一個 target 能夠制定其自己的 build settings,且 target 的 build settings 會重寫 project 的 build settings。
Xcode project 文件包含以下信息:
- 源文件的引用:
- 源碼,包括頭文件和實現文件
- 內部和外部的庫或者框架
- 資源文件
- 圖片文件
- Interface Builder(nib)文件
- 文件結構導航中用來組織源文件的組
- Project-level build configurations.你可以為 project 指定多個 build configuration,例如,project 中默認包含 debug 和 release 兩種 build settings.
- Targets, 每一個 target 指定了:
- project 生成的 product
- 生成 product 所需的源文件
- 生成 product 所需的配置文件,包括對其他 targets 的依賴以及一些其他設置;當 targets 的 build configurations 沒有重寫 project-level 的 build settings 時,會直接使用 project-level 的 build setting.
- 可執行環境,該環境用于調試或者測試程序,每個可執行環境會指定:
- 運行或者調試程序時加載的可執行程序
- 傳遞給可執行程序的命令行參數
- 運行程序時需設置的環境變量
project 可獨立存在,也可被包含在 workspace 中。
Build Setting 的繼承關系
官方文檔內容如下:
A build setting is a variable that contains information about how a particular aspect of a product’s build process should be performed. For example, the information in a build setting can specify which options Xcode passes to the compiler.
You can specify build settings at the project or target level. Each project-level build setting applies to all targets in the project unless explicitly overridden by the build settings for a specific target.
build setting 中包含了 product 生成過程中所需的參數信息。你可以在 project-level 和 target-level 層指定 build settings。project-level 的 build settings 適用于 project 中的所有targets,但是當 target-level 的 build settings 重寫了 project-level 的 build settings,以 target-level 中的 build settings 中的值為準。
Each target organizes the source files needed to build one product. A build configuration specifies a set of build settings used to build a target's product in a particular way. For example, it is common to have separate build configurations for debug and release builds of a product.
一個 build configaration 指定了一套 build settings 用于生成某一 target 的 product,例如,在 Xcode 創建項目時默認就有兩套獨立的 build configarations, 分別用于生成 debug 和 release 模式下的 product。
In addition to the default build settings provided by Xcode when you create a new project from a project template, you can create user-defined build settings for your project or for a particular target. You can also specify conditional build settings. The value of a conditional build setting depends on whether one or more prerequisites are met. This mechanism allows you to, for example, specify the SDK to use to build a product based on the targeted architecture.
除了創建工程時生成的默認 build settings,你也可以自定義 project-level 或者 target-level 的 build settings.
關于繼承關系,The Unofficial Guide to xcconfig files 這里也有詳細的說明,強烈建議閱讀。
現在就來看看如何使用自定義的 build settings 來達到本文開始處提到的需求.
如何使用 xcconfig 文件來配置不同開發階段的環境
目前公司中的開發大致分兩個階段,第一階段:開發階段,此時所打包都是使用 development 的證書,極光和友盟統計的賬號都是使用開發者自己申請的賬號,webservice 的地址使用開發環境地址;第二階段:uat 階段,此時屬于預發版階段,此時打包使用 ad-hoc 的證書,極光和友盟統計的賬號使用公司申請生成賬號,webservice 使用的特定的預發版環境;另外,打上傳到 App Store 的生產包,使用 distribution 的證書,webservice 的地址使用生產環境的地址。
由此,可新建一種 build configuration, 由 Xcode 自動生成的 Release 復制而來,如下所示:
并命名為 PreRelease。
官方文檔Adding a Build Configuration 中如下提到:
A configuration file is a plain text file with a list of build setting definitions, one per line. You can base a build configuration only on a configuration file that is in your project, not on an external file.
When you base a target or project’s build configuration on a configuration file, that build configuration automatically inherits the build setting definitions in that configuration file (and any configuration files it includes). If you then modify the value of any of those build settings in the target or project, the new value is used instead of the value in the configuration file.
Build settings defined at the target level override any values assigned to those build settings at the project level. Therefore, target-level configurations take precedence over any project-level configurations.
這里需要注意的是:當你的 target-level 或者 project-levle 的 build configurations 基于配置文件時,build configuration 會自動繼承配置文件(以及配置文件中引入的配置文件)中定義的 build settings,但是如果你又在之后 target 或者 project 中修改了配置文件中定義的 build settings 值,那么最終配置文件中的值會失效,實際使用的是 target 或者 project 中設置的值。
這里鑒于公司的情況,新建了 Debug.xcconfig/PreRelease.xcconfig/Release.xcconfig 配置對應于開發階段、預發版階段、上傳 AppStore 三種情況下的打包。
新建一個 xcconfig 目錄,在該目錄下新建配置文件:
根據項目情況,每個配置文件中都包含同樣的 key 值,內容大致如下:
//網絡請求baseurl
WEBSERVICE_URL = @"http:\/\/127.0.0.1"
//友盟配置
UMENG_APPKEY = @"xxxvvv555999=="
//極光推送配置
JPUSH_DEVELOPMENT_APPKEY = @"nnncccvvvwww"
IS_PRODUCATION = NO
#include "Generator.xcconfig"
你可在配置文件中包含其他配置文件,其中 Generator.xcconfig 文件的內容是:
GCC_PREPROCESSOR_DEFINITIONS = $(inherited) WEBSERVICE_URL='$(WEBSERVICE_URL)' MESSAGE_SYSTEM_URL='$(MESSAGE_SYSTEM_URL)' UMENG_APPKEY='$(UMENG_APPKEY)' IS_PRODUCATION='$(IS_PRODUCATION)'
其作用是將配置文件中定義的常量定義成預編譯宏,以便于在代碼中獲取。
其中 GCC_PREPROCESSOR_DEFINITIONS, 文檔如下:
Space-separated list of option specifications. Specifies preprocessor macros in the form foo (for a simple #define) or foo=1 (for a value definition). This list is passed to the compiler through the gcc -D option when compiling precompiled headers and implementation files.
GCC_PREPROCESSOR_DEFINITIONS 是 GCC 預編譯頭參數,通常我們可以在 Project 文件下的 Build Settings 對預編譯宏定義進行默認賦值。在 Xcode7 下的路徑為 Build Settings->Apple LLVM 7.x Preprocessing->Preprocessor Macros,
想必大家看這個宏的名字已經知道它的作用了, 使用上和在 pch 頭文件中添加宏定義沒有太大的區別, 但有以下好處:
- Xcode 的 Project 的 Build Settings 是由一個 plist 文件進行描述的, plist 本質上是一個 XML 配置文件, 通過外部的腳本比較容易去修改。
- Preprocessor Macros 可以按照 Configuration 選項進行默認配置, 也就是說可以根據不同的環境預先制定不同定義的宏,或者為不同環境下的相同變量定義不同的值
xcconfig 支持可以根據不同的 Configuration 選項配置不同的文件。不同的 xcconfig 可以指定不同的 Build Settings 里的屬性值, 這樣子我們就可以通過項目 xcconfig 去修改 GCC_PREPROCESSOR_DEFINITIONS 的值了(最終目的就達到了)。
配置文件中變量定義好之后,怎么讓 Xcode 自動加載呢?如下圖設置所示,是將 project-level 的 build settings 基于配置文件,三種情況的 configurations 分別選擇與之對應的配置文件。
當我們想把 project-level 或者 target-level 中的 Build Settings 的設置挪動到 xcconfig 配置文件來設置時,是否需要一個個手動輸入呢?當然不是,直接在 Build Settings 中選中你想要在 xcconfig 中配置的鍵值對所在行(當然也可以選多行),command + c復制,然后到對應的 xcconfig 中去粘貼就好了,記得在 Build Settings 中改為你想要的值后再復制,如果為默認值的話則只可復制其鍵。如果需要改回去的話,還是選中這行,command + delete
就恢復默認值了。
現在我們將設置挪動到了配置文件中,所有的配置文件都是鍵值對類型的文本文件,但是當同一個鍵同時存在于 target-level、project-level 和配置文件中時,到底是哪一個鍵值對起作用了呢?現在看看下圖。
注意: Xcode以從左至右的順序設置解析的優先級,從左至右優先級降低,最左邊的具有最高優先級,即 target-level > project-level > 自定義配置文件 > iOS 默認配置;且最左列 Resolved 列顯示的是最終使用的值。那么如何使 Xcode 使用配置文件中的配置項呢?這需要選中要使用配置文件的行,點擊 Delete 按鍵,你會發現項目的默認設置已經被刪除,且 xcconfig 的配置文件列被標記為綠色。標記為綠色代表該列的值生效,其值應該與 Resolved 列的值相同。
最后,你可以像如下示例使用 xcconfig 中定義的宏:
NSLog(@"webservice url: %@, umeng appkey: %@", WEBSERVICE_URL, UMENG_APPKEY);
通過以上步驟,就達到了使用 xcconfig 文件來配置開發不同階段時的環境變量的目的了。
文中內容為自己學習總結,如有錯誤之處請指正。
參考: