歡迎到我的 個人博客 http://liumh.com 瀏覽此文
與公司 QA 聊天,已不止一次被吐槽說移動端從開發(fā)環(huán)境轉(zhuǎn)到生產(chǎn)環(huán)境時,還要靠修改代碼來配置對應的環(huán)境參數(shù)。她認為,從 App 轉(zhuǎn)測試之后,就不應該再修改代碼,可以把所有的環(huán)境配置都整合到配置文件中,這樣打不同環(huán)境下的安裝包時,會自動選擇對應的環(huán)境參數(shù)。這里說到的環(huán)境參數(shù)包括但不僅限于: webservice 地址,友盟 AppKey,極光推送 AppKey 和是否是生產(chǎn)環(huán)境標志等。
其實,我也討厭修改環(huán)境參數(shù)啊,??
為達成上述目的,主要是使用 Xcode 的 Configurations Setting File(即后綴為 xcconfig 文件) 來配置開發(fā)不同階段下的環(huán)境。本文包含的內(nèi)容如下:
- Xcode Target
- Xcode Project
- Build Setting的繼承關(guān)系
- 如何使用xcconfig文件來配置不同開發(fā)階段的環(huán)境
包含了一些與 build settings 相關(guān)的知識。
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, 它將構(gòu)建該 product 所需的文件和處理這些文件所需的指令集整合進 build system 中。Projects 會包含一個或者多個 targets,每一個 target 將會產(chǎn)出一個 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 參數(shù)繼承自 project 的 build settings, 但是你可以在 target 中修改任意 settings 來重寫 project settings,這樣,最終生效的 settings 參數(shù)以在 target 中設(shè)置的為準. 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 有關(guān),如果一個 target 的 build 依賴于另一個 target 的輸出,那么我們就說前一個 target 依賴于后一個 target .如果這些 target 在同一個 workspace 中,那么 Xcode 能夠發(fā)現(xiàn)這種依賴關(guān)系,從而使其以我們期望的順序生成 products.這種關(guān)系被稱為隱式依賴關(guān)系。同時,你可以顯示指定 targets 之間的依賴關(guān)系,并且這種依賴關(guān)系會覆蓋 Xcode 推測出的隱式依賴關(guān)系。
指定 targets 之間的依賴關(guān)系的地方在 Project Editor->TRAGETS->Build Phases->Target Dependencies 處設(shè)置。如下圖所示:

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 文件包含以下信息:
- 源文件的引用:
- 源碼,包括頭文件和實現(xiàn)文件
- 內(nèi)部和外部的庫或者框架
- 資源文件
- 圖片文件
- Interface Builder(nib)文件
- 文件結(jié)構(gòu)導航中用來組織源文件的組
- Project-level build configurations.你可以為 project 指定多個 build configuration,例如,project 中默認包含 debug 和 release 兩種 build settings.
- Targets, 每一個 target 指定了:
- project 生成的 product
- 生成 product 所需的源文件
- 生成 product 所需的配置文件,包括對其他 targets 的依賴以及一些其他設(shè)置;當 targets 的 build configurations 沒有重寫 project-level 的 build settings 時,會直接使用 project-level 的 build setting.
- 可執(zhí)行環(huán)境,該環(huán)境用于調(diào)試或者測試程序,每個可執(zhí)行環(huán)境會指定:
- 運行或者調(diào)試程序時加載的可執(zhí)行程序
- 傳遞給可執(zhí)行程序的命令行參數(shù)
- 運行程序時需設(shè)置的環(huán)境變量
project 可獨立存在,也可被包含在 workspace 中。
Build Setting 的繼承關(guān)系
官方文檔內(nèi)容如下:
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 生成過程中所需的參數(shù)信息。你可以在 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 創(chuàng)建項目時默認就有兩套獨立的 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.
除了創(chuàng)建工程時生成的默認 build settings,你也可以自定義 project-level 或者 target-level 的 build settings.
關(guān)于繼承關(guān)系,The Unofficial Guide to xcconfig files 這里也有詳細的說明,強烈建議閱讀。
現(xiàn)在就來看看如何使用自定義的 build settings 來達到本文開始處提到的需求.
如何使用 xcconfig 文件來配置不同開發(fā)階段的環(huán)境
目前公司中的開發(fā)大致分兩個階段,第一階段:開發(fā)階段,此時所打包都是使用 development 的證書,極光和友盟統(tǒng)計的賬號都是使用開發(fā)者自己申請的賬號,webservice 的地址使用開發(fā)環(huán)境地址;第二階段:uat 階段,此時屬于預發(fā)版階段,此時打包使用 ad-hoc 的證書,極光和友盟統(tǒng)計的賬號使用公司申請生成賬號,webservice 使用的特定的預發(fā)版環(huán)境;另外,打上傳到 App Store 的生產(chǎn)包,使用 distribution 的證書,webservice 的地址使用生產(chǎn)環(huán)境的地址。
由此,可新建一種 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 中設(shè)置的值。
這里鑒于公司的情況,新建了 Debug.xcconfig/PreRelease.xcconfig/Release.xcconfig 配置對應于開發(fā)階段、預發(fā)版階段、上傳 AppStore 三種情況下的打包。
新建一個 xcconfig 目錄,在該目錄下新建配置文件:

根據(jù)項目情況,每個配置文件中都包含同樣的 key 值,內(nèi)容大致如下:
//網(wǎng)絡(luò)請求baseurl
WEBSERVICE_URL = @"http:\/\/127.0.0.1"
//友盟配置
UMENG_APPKEY = @"xxxvvv555999=="
//極光推送配置
JPUSH_DEVELOPMENT_APPKEY = @"nnncccvvvwww"
IS_PRODUCATION = NO
#include "Generator.xcconfig"
你可在配置文件中包含其他配置文件,其中 Generator.xcconfig 文件的內(nèi)容是:
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 預編譯頭參數(shù),通常我們可以在 Project 文件下的 Build Settings 對預編譯宏定義進行默認賦值。在 Xcode7 下的路徑為 Build Settings->Apple LLVM 7.x Preprocessing->Preprocessor Macros,

想必大家看這個宏的名字已經(jīng)知道它的作用了, 使用上和在 pch 頭文件中添加宏定義沒有太大的區(qū)別, 但有以下好處:
- Xcode 的 Project 的 Build Settings 是由一個 plist 文件進行描述的, plist 本質(zhì)上是一個 XML 配置文件, 通過外部的腳本比較容易去修改。
- Preprocessor Macros 可以按照 Configuration 選項進行默認配置, 也就是說可以根據(jù)不同的環(huán)境預先制定不同定義的宏,或者為不同環(huán)境下的相同變量定義不同的值
xcconfig 支持可以根據(jù)不同的 Configuration 選項配置不同的文件。不同的 xcconfig 可以指定不同的 Build Settings 里的屬性值, 這樣子我們就可以通過項目 xcconfig 去修改 GCC_PREPROCESSOR_DEFINITIONS 的值了(最終目的就達到了)。
配置文件中變量定義好之后,怎么讓 Xcode 自動加載呢?如下圖設(shè)置所示,是將 project-level 的 build settings 基于配置文件,三種情況的 configurations 分別選擇與之對應的配置文件。

當我們想把 project-level 或者 target-level 中的 Build Settings 的設(shè)置挪動到 xcconfig 配置文件來設(shè)置時,是否需要一個個手動輸入呢?當然不是,直接在 Build Settings 中選中你想要在 xcconfig 中配置的鍵值對所在行(當然也可以選多行),command + c復制,然后到對應的 xcconfig 中去粘貼就好了,記得在 Build Settings 中改為你想要的值后再復制,如果為默認值的話則只可復制其鍵。如果需要改回去的話,還是選中這行,command + delete 就恢復默認值了。
現(xiàn)在我們將設(shè)置挪動到了配置文件中,所有的配置文件都是鍵值對類型的文本文件,但是當同一個鍵同時存在于 target-level、project-level 和配置文件中時,到底是哪一個鍵值對起作用了呢?現(xiàn)在看看下圖。

注意: Xcode以從左至右的順序設(shè)置解析的優(yōu)先級,從左至右優(yōu)先級降低,最左邊的具有最高優(yōu)先級,即 target-level > project-level > 自定義配置文件 > iOS 默認配置;且最左列 Resolved 列顯示的是最終使用的值。那么如何使 Xcode 使用配置文件中的配置項呢?這需要選中要使用配置文件的行,點擊 Delete 按鍵,你會發(fā)現(xiàn)項目的默認設(shè)置已經(jīng)被刪除,且 xcconfig 的配置文件列被標記為綠色。標記為綠色代表該列的值生效,其值應該與 Resolved 列的值相同。
最后,你可以像如下示例使用 xcconfig 中定義的宏:
NSLog(@"webservice url: %@, umeng appkey: %@", WEBSERVICE_URL, UMENG_APPKEY);
通過以上步驟,就達到了使用 xcconfig 文件來配置開發(fā)不同階段時的環(huán)境變量的目的了。
文中內(nèi)容為自己學習總結(jié),如有錯誤之處請指正。
參考: