日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國(guó)內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁(yè) > php開源 > 綜合技術(shù) > Xcode使用xcconfig文件配置環(huán)境

Xcode使用xcconfig文件配置環(huán)境

來(lái)源:程序員人生   發(fā)布時(shí)間:2016-06-23 08:05:56 閱讀次數(shù):5018次

歡迎到個(gè)人博客: liumh.com查看本文。

與公司 QA 聊天,已不止1次被吐槽說(shuō)移動(dòng)端從開發(fā)環(huán)境轉(zhuǎn)到生產(chǎn)環(huán)境時(shí),還要靠修改代碼來(lái)配置對(duì)應(yīng)的環(huán)境參數(shù)。她認(rèn)為,從 App 轉(zhuǎn)測(cè)試以后,就不應(yīng)當(dāng)再修改代碼,可以把所有的環(huán)境配置都整合到配置文件中,這樣打不同環(huán)境下的安裝包時(shí),會(huì)自動(dòng)選擇對(duì)應(yīng)的環(huán)境參數(shù)。這里說(shuō)到的環(huán)境參數(shù)包括但不但限于: webservice 地址,友盟 AppKey,極光推送 AppKey 和是不是是生產(chǎn)環(huán)境標(biāo)志等。

其實(shí),我也討厭修改環(huán)境參數(shù)啊~

為達(dá)成上述目的,主要是使用 Xcode 的 Configurations Setting File(即后綴為 xcconfig 文件) 來(lái)配置開發(fā)不同階段下的環(huán)境。本文包括的內(nèi)容以下:

  1. Xcode Target
  2. Xcode Project
  3. Build Setting的繼承關(guān)系
  4. 如何使用xcconfig文件來(lái)配置不同開發(fā)階段的環(huán)境

包括了1些與 build settings 相干的知識(shí)。

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 定義了生成的唯1 product, 它將構(gòu)建該 product 所需的文件和處理這些文件所需的指令集整合進(jìn) build system 中。Projects 會(huì)包括1個(gè)或多個(gè) targets,每個(gè) target 將會(huì)產(chǎn)出1個(gè) 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 的情勢(shì)存在,你可在 Xcode 的項(xiàng)目編輯器(TARGETS->Build Setting, TARGETS->Build Phases)中進(jìn)行查看和編輯。target 中的 build setting 參數(shù)繼承自 project 的 build settings, 但是你可以在 target 中修改任意 settings 來(lái)重寫 project settings,這樣,終究生效的 settings 參數(shù)以在 target 中設(shè)置的為準(zhǔn). Project 可包括多個(gè) target, 但是在同1時(shí)刻,只會(huì)有1個(gè) target 生效,可用 Xcode 的 scheme 來(lái)指定是哪個(gè) 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 可與另外一個(gè) target 有關(guān),如果1個(gè) target 的 build 依賴于另外一個(gè) target 的輸出,那末我們就說(shuō)前1個(gè) target 依賴于后1個(gè) target .如果這些 target 在同1個(gè) workspace 中,那末 Xcode 能夠發(fā)現(xiàn)這類依賴關(guān)系,從而使其以我們期望的順序生成 products.這類關(guān)系被稱為隱式依賴關(guān)系。同時(shí),你可以顯示指定 targets 之間的依賴關(guān)系,并且這類依賴關(guān)系會(huì)覆蓋 Xcode 推測(cè)出的隱式依賴關(guān)系。

指定 targets 之間的依賴關(guān)系的地方在 Project Editor->TRAGETS->Build Phases->Target Dependencies 處設(shè)置。以下圖所示:

添加targets間的依賴關(guān)系

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 是1個(gè)倉(cāng)庫(kù),該倉(cāng)庫(kù)包括了所有的文件,資源和用于生成1個(gè)或多個(gè) software products 的信息。它包括1個(gè)或多個(gè) targets,其中的每個(gè) target 指明了如何生成 products。project 為其具有的所有 targets 定義了默許的 build settings,固然,每個(gè) target 能夠制定其自己的 build settings,且 target 的 build settings 會(huì)重寫 project 的 build settings。

Xcode project 文件包括以下信息:

  • 源文件的援用:
    • 源碼,包括頭文件和實(shí)現(xiàn)文件
    • 內(nèi)部和外部的庫(kù)或框架
    • 資源文件
    • 圖片文件
    • Interface Builder(nib)文件
  • 文件結(jié)構(gòu)導(dǎo)航中用來(lái)組織源文件的組
  • Project-level build configurations.你可以為 project 指定多個(gè) build configuration,例如,project 中默許包括 debug 和 release 兩種 build settings.
  • Targets, 每個(gè) target 指定了:
    • project 生成的 product
    • 生成 product 所需的源文件
    • 生成 product 所需的配置文件,包括對(duì)其他 targets 的依賴和1些其他設(shè)置;當(dāng) targets 的 build configurations 沒(méi)有重寫 project-level 的 build settings 時(shí),會(huì)直接使用 project-level 的 build setting.
  • 可履行環(huán)境,該環(huán)境用于調(diào)試或測(cè)試程序,每一個(gè)可履行環(huán)境會(huì)指定:
    • 運(yùn)行或調(diào)試程序時(shí)加載的可履行程序
    • 傳遞給可履行程序的命令行參數(shù)
    • 運(yùn)行程序時(shí)需設(shè)置的環(huán)境變量

project 可獨(dú)立存在,也可被包括在 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 生成進(jìn)程中所需的參數(shù)信息。你可以在 project-level 和 target-level 層指定 build settings。project-level 的 build settings 適用于 project 中的所有targets,但是當(dāng) target-level 的 build settings 重寫了 project-level 的 build settings,以 target-level 中的 build settings 中的值為準(zhǔn)。

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個(gè) build configaration 指定了1套 build settings 用于生成某1 target 的 product,例如,在 Xcode 創(chuàng)建項(xiàng)目時(shí)默許就有兩套獨(dú)立的 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)建工程時(shí)生成的默許 build settings,你也能夠自定義 project-level 或 target-level 的 build settings.

關(guān)于繼承關(guān)系,The Unofficial Guide to xcconfig files 這里也有詳細(xì)的說(shuō)明,強(qiáng)烈建議瀏覽。

現(xiàn)在就來(lái)看看如何使用自定義的 build settings 來(lái)到達(dá)本文開始處提到的需求.

如何使用 xcconfig 文件來(lái)配置不同開發(fā)階段的環(huán)境

目前公司中的開發(fā)大致分兩個(gè)階段,第1階段:開發(fā)階段,此時(shí)所打包都是使用 development 的證書,極光和友盟統(tǒng)計(jì)的賬號(hào)都是使用開發(fā)者自己申請(qǐng)的賬號(hào),webservice 的地址使用開發(fā)環(huán)地步址;第2階段:uat 階段,此時(shí)屬于預(yù)發(fā)版階段,此時(shí)打包使用 ad-hoc 的證書,極光和友盟統(tǒng)計(jì)的賬號(hào)使用公司申請(qǐng)生成賬號(hào),webservice 使用的特定的預(yù)發(fā)版環(huán)境;另外,打上傳到 App Store 的生產(chǎn)包,使用 distribution 的證書,webservice 的地址使用生產(chǎn)環(huán)境的地址。

由此,可新建1種 build configuration, 由 Xcode 自動(dòng)生成的 Release 復(fù)制而來(lái),以下所示:

新建Configurations

并命名為 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.

這里需要注意的是:當(dāng)你的 target-level 或 project-levle 的 build configurations 基于配置文件時(shí),build configuration 會(huì)自動(dòng)繼承配置文件(和配置文件中引入的配置文件)中定義的 build settings,但是如果你又在以后 target 或 project 中修改了配置文件中定義的 build settings 值,那末終究配置文件中的值會(huì)失效,實(shí)際使用的是 target 或 project 中設(shè)置的值。

這里鑒于公司的情況,新建了 Debug.xcconfig/PreRelease.xcconfig/Release.xcconfig 配置對(duì)應(yīng)于開發(fā)階段、預(yù)發(fā)版階段、上傳 AppStore 3種情況下的打包。

新建1個(gè) xcconfig 目錄,在該目錄下新建配置文件:

創(chuàng)建配置文件

根據(jù)項(xiàng)目情況,每一個(gè)配置文件中都包括一樣的 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)'

其作用是將配置文件中定義的常量定義成預(yù)編譯宏,以便于在代碼中獲得。

其中 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 預(yù)編譯頭參數(shù),通常我們可以在 Project 文件下的 Build Settings 對(duì)預(yù)編譯宏定義進(jìn)行默許賦值。在 Xcode7 下的路徑為 Build Settings->Apple LLVM 7.x Preprocessing->Preprocessor Macros,

GCC_PREPROCESSOR_DEFINITIONS

想必大家看這個(gè)宏的名字已知道它的作用了, 使用上和在 pch 頭文件中添加宏定義沒(méi)有太大的區(qū)分, 但有以下好處:

  1. Xcode 的 Project 的 Build Settings 是由1個(gè) plist 文件進(jìn)行描寫的, plist 本質(zhì)上是1個(gè) XML 配置文件, 通過(guò)外部的腳本比較容易去修改。
  2. Preprocessor Macros 可以依照 Configuration 選項(xiàng)進(jìn)行默許配置, 也就是說(shuō)可以根據(jù)不同的環(huán)境預(yù)先制定不同定義的宏,或?yàn)椴煌h(huán)境下的相同變量定義不同的值

xcconfig 支持可以根據(jù)不同的 Configuration 選項(xiàng)配置不同的文件。不同的 xcconfig 可以指定不同的 Build Settings 里的屬性值, 這模樣我們就能夠通過(guò)項(xiàng)目 xcconfig 去修改 GCC_PREPROCESSOR_DEFINITIONS 的值了(終究目的就到達(dá)了)。

配置文件中變量定義好以后,怎樣讓 Xcode 自動(dòng)加載呢?以下圖設(shè)置所示,是將 project-level 的 build settings 基于配置文件,3種情況的 configurations 分別選擇與之對(duì)應(yīng)的配置文件。

將配置文件與項(xiàng)目關(guān)聯(lián)

當(dāng)我們想把 project-level 或 target-level 中的 Build Settings 的設(shè)置移動(dòng)到 xcconfig 配置文件來(lái)設(shè)置時(shí),是不是需要1個(gè)個(gè)手動(dòng)輸入呢?固然不是,直接在 Build Settings 當(dāng)選中你想要在 xcconfig 中配置的鍵值對(duì)所在行(固然也能夠選多行),command + c復(fù)制,然后到對(duì)應(yīng)的 xcconfig 中去粘貼就行了,記得在 Build Settings 中改成你想要的值后再?gòu)?fù)制,如果為默許值的話則只可復(fù)制其鍵。如果需要改回去的話,還是選中這行,command + delete 就恢復(fù)默許值了。

現(xiàn)在我們將設(shè)置移動(dòng)到了配置文件中,所有的配置文件都是鍵值對(duì)類型的文本文件,但是當(dāng)同1個(gè)鍵同時(shí)存在于 target-level、project-level 和配置文件中時(shí),究竟是哪個(gè)鍵值對(duì)起作用了呢?現(xiàn)在看看下圖。

多個(gè)配置項(xiàng)列

注意: Xcode以從左至右的順序設(shè)置解析的優(yōu)先級(jí),從左至右優(yōu)先級(jí)下降,最左側(cè)的具有最高優(yōu)先級(jí),即 target-level > project-level > 自定義配置文件 > iOS 默許配置;且最左列 Resolved 列顯示的是終究使用的值。那末如何使 Xcode 使用配置文件中的配置項(xiàng)呢?這需要選中要使用配置文件的行,點(diǎn)擊 Delete 按鍵,你會(huì)發(fā)現(xiàn)項(xiàng)目的默許設(shè)置已被刪除,且 xcconfig 的配置文件列被標(biāo)記為綠色。標(biāo)記為綠色代表該列的值生效,其值應(yīng)當(dāng)與 Resolved 列的值相同。

最后,你可以像以下示例使用 xcconfig 中定義的宏:

NSLog(@"webservice url: %@, umeng appkey: %@", WEBSERVICE_URL, UMENG_APPKEY);

通過(guò)以上步驟,就到達(dá)了使用 xcconfig 文件來(lái)配置開發(fā)不同階段時(shí)的環(huán)境變量的目的了。

文中內(nèi)容為自己學(xué)習(xí)總結(jié),如有毛病的地方請(qǐng)指正。


如果覺(jué)得本文對(duì)你有幫助,就請(qǐng)用微信隨便打賞我吧^_^

請(qǐng)我喝杯咖啡^_^


參考:

  • Xcode Concepts
  • Using xcconfig files for your XCode Project
  • iOS開發(fā)必備 - 環(huán)境變量配置
  • xcconfig-how-to-set-environment-variables
生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 中文字幕第一页在线 | 99精品久久99久久久久 | 欧美成人在线免费视频 | 91高清在线观看 | 久久精品久久久久久 | 日韩一级视频 | 日韩av成人在线 | 在线国产一区二区 | 国产精品免费在线 | 亚洲自拍偷拍视频 | 亚洲精品国产成人 | 欧美日韩中文在线观看 | 日韩三区在线 | 成人免费福利 | 国产日本在线视频 | 亚洲高清视频在线观看 | 精品视频网站在线观看 | 天堂在线www | 国产精品国产三级国产在线观看 | 亚洲不卡电影 | 高清成人 | 天天干人人 | 狠狠久久综合 | av九九 | 亚洲第一se情网站 | 国产69精品久久久久毛片 | 国产福利91精品一区二区三区 | 日韩精品视频免费 | 欧美成人性生活 | 欧美一区二区在线免费观看 | 国产精品亚洲综合 | 福利在线观看 | 99九九久久 | 在线国产视频观看 | 久久精品日产第一区二区三区 | 人妖被c到高潮欧美gay | 成人免费大片黄在线播放 | 成人在线免费 | 超碰中文字幕 | 日韩精品视频中文字幕 | 日韩h视频|