歡迎到個人博客: liumh.com查看本文。
與公司 QA 聊天,已不止1次被吐槽說移動端從開發(fā)環(huán)境轉到生產(chǎn)環(huán)境時,還要靠修改代碼來配置對應的環(huán)境參數(shù)。她認為,從 App 轉測試以后,就不應當再修改代碼,可以把所有的環(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)容以下:
包括了1些與 build settings 相干的知識。
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 定義了生成的唯1 product, 它將構建該 product 所需的文件和處理這些文件所需的指令集整合進 build system 中。Projects 會包括1個或多個 targets,每個 target 將會產(chǎn)出1個 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 中設置的為準. Project 可包括多個 target, 但是在同1時刻,只會有1個 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 有關,如果1個 target 的 build 依賴于另外一個 target 的輸出,那末我們就說前1個 target 依賴于后1個 target .如果這些 target 在同1個 workspace 中,那末 Xcode 能夠發(fā)現(xiàn)這類依賴關系,從而使其以我們期望的順序生成 products.這類關系被稱為隱式依賴關系。同時,你可以顯示指定 targets 之間的依賴關系,并且這類依賴關系會覆蓋 Xcode 推測出的隱式依賴關系。
指定 targets 之間的依賴關系的地方在 Project Editor->TRAGETS->Build Phases->Target Dependencies 處設置。以下圖所示:
官方文檔的解釋以下:
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 是1個倉庫,該倉庫包括了所有的文件,資源和用于生成1個或多個 software products 的信息。它包括1個或多個 targets,其中的每個 target 指明了如何生成 products。project 為其具有的所有 targets 定義了默許的 build settings,固然,每個 target 能夠制定其自己的 build settings,且 target 的 build settings 會重寫 project 的 build settings。
Xcode project 文件包括以下信息:
project 可獨立存在,也可被包括在 workspace 中。
官方文檔內(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.
1個 build configaration 指定了1套 build settings 用于生成某1 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.
關于繼承關系,The Unofficial Guide to xcconfig files 這里也有詳細的說明,強烈建議瀏覽。
現(xiàn)在就來看看如何使用自定義的 build settings 來到達本文開始處提到的需求.
目前公司中的開發(fā)大致分兩個階段,第1階段:開發(fā)階段,此時所打包都是使用 development 的證書,極光和友盟統(tǒng)計的賬號都是使用開發(fā)者自己申請的賬號,webservice 的地址使用開發(fā)環(huán)地步址;第2階段:uat 階段,此時屬于預發(fā)版階段,此時打包使用 ad-hoc 的證書,極光和友盟統(tǒng)計的賬號使用公司申請生成賬號,webservice 使用的特定的預發(fā)版環(huán)境;另外,打上傳到 App Store 的生產(chǎn)包,使用 distribution 的證書,webservice 的地址使用生產(chǎn)環(huán)境的地址。
由此,可新建1種 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 配置對應于開發(fā)階段、預發(fā)版階段、上傳 AppStore 3種情況下的打包。
新建1個 xcconfig 目錄,在該目錄下新建配置文件:
根據(jù)項目情況,每一個配置文件中都包括一樣的 key 值,內(nèi)容大致以下:
//網(wǎng)絡要求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,
想必大家看這個宏的名字已知道它的作用了, 使用上和在 pch 頭文件中添加宏定義沒有太大的區(qū)分, 但有以下好處:
xcconfig 支持可以根據(jù)不同的 Configuration 選項配置不同的文件。不同的 xcconfig 可以指定不同的 Build Settings 里的屬性值, 這模樣我們就能夠通過項目 xcconfig 去修改 GCC_PREPROCESSOR_DEFINITIONS 的值了(終究目的就到達了)。
配置文件中變量定義好以后,怎樣讓 Xcode 自動加載呢?以下圖設置所示,是將 project-level 的 build settings 基于配置文件,3種情況的 configurations 分別選擇與之對應的配置文件。
當我們想把 project-level 或 target-level 中的 Build Settings 的設置移動到 xcconfig 配置文件來設置時,是不是需要1個個手動輸入呢?固然不是,直接在 Build Settings 當選中你想要在 xcconfig 中配置的鍵值對所在行(固然也能夠選多行),command + c復制,然后到對應的 xcconfig 中去粘貼就行了,記得在 Build Settings 中改成你想要的值后再復制,如果為默許值的話則只可復制其鍵。如果需要改回去的話,還是選中這行,command + delete
就恢復默許值了。
現(xiàn)在我們將設置移動到了配置文件中,所有的配置文件都是鍵值對類型的文本文件,但是當同1個鍵同時存在于 target-level、project-level 和配置文件中時,究竟是哪個鍵值對起作用了呢?現(xiàn)在看看下圖。
注意: Xcode以從左至右的順序設置解析的優(yōu)先級,從左至右優(yōu)先級下降,最左側的具有最高優(yōu)先級,即 target-level > project-level > 自定義配置文件 > iOS 默許配置;且最左列 Resolved 列顯示的是終究使用的值。那末如何使 Xcode 使用配置文件中的配置項呢?這需要選中要使用配置文件的行,點擊 Delete 按鍵,你會發(fā)現(xiàn)項目的默許設置已被刪除,且 xcconfig 的配置文件列被標記為綠色。標記為綠色代表該列的值生效,其值應當與 Resolved 列的值相同。
最后,你可以像以下示例使用 xcconfig 中定義的宏:
NSLog(@"webservice url: %@, umeng appkey: %@", WEBSERVICE_URL, UMENG_APPKEY);
通過以上步驟,就到達了使用 xcconfig 文件來配置開發(fā)不同階段時的環(huán)境變量的目的了。
文中內(nèi)容為自己學習總結,如有毛病的地方請指正。
如果覺得本文對你有幫助,就請用微信隨便打賞我吧^_^
參考: