Xcode 9 最新 最準(zhǔn)確 的自動(dòng)化打包

Hi,

本文主要講Xcode 9中,如何使用最新的、簡單的方式來實(shí)現(xiàn)腳本打包。

code sign work flow

Xcode 8以后,關(guān)于iOS的App打包,Apple逐步做了調(diào)整,更簡單了,我覺得是更趨近于傻瓜式了。

一開始接觸iOS時(shí),我們習(xí)慣使用XCode來做這些工作,有了自動(dòng)化后,我們都是用命令行(Command line)來處理,所以本文基于命令行處理。

打包的具體流程應(yīng)該是clean(清理緩存)、build(編譯)、archive(打包)、export(導(dǎo)出ipa)


1、Clean

Apple提供的例子:

xcodebuild clean install

釋:Cleans the build directory; then builds and installs the first target in the Xcode project in the directory from which xcodebuild was started.

2、Build

2.1 )編譯project工程,Apple提供的例子

xcodebuild -project MyProject.xcodeproj -target Target1 -target Target2 -configuration Debug

釋:Builds the targets Target1 and Target2 in the project MyProject.xcodeproj using the Debug configuration.

2.2) 編譯workspace,Apple提供的例子:

xcodebuild -workspace MyWorkspace.xcworkspace -scheme MyScheme

釋:Builds the scheme MyScheme in the Xcode workspace MyWorkspace.xcworkspace.

注意:

1.project 和taget(可多個(gè))組合使用;workspace和scheme(只一個(gè))組合使用

2.configuration有Debug和Release兩種模式。

3、Archive

先了解下打包,看一個(gè) Apple提供的簡單例子:

xcodebuild archive -workspace MyWorkspace.xcworkspace -scheme MyScheme

釋:Archives the scheme MyScheme in the Xcode workspace MyWorkspace.xcworkspace.

這例子中,除了指定必要的worksepace、sheme,未添加其他的配置,-configuration 默認(rèn)是Release。

Xcode8 以后,Apple提供了兩種思路:Automic(自動(dòng))、manual(手動(dòng)),所以我們按此來分析。

3.1)Automic

Apple推薦的做法是什么?

先看一張圖:

答案是:Automic,且code sign時(shí)使用Development。

很多人要問,以前的做法是archive時(shí),是必須指定正確那一堆麻煩的證書配置,如code sign identify、team、provision file,也有很多人被此坑過。

Now,你無需擔(dān)心這玩意,也無需使用腳本來修改aaa.xcproject文件中的那些證書配置,哪怕其中的配置都是錯(cuò)誤的,參數(shù)會(huì)作為命令執(zhí)行的標(biāo)準(zhǔn),且也不會(huì)自動(dòng)更改你的aaa.xcproject文件。

那該如何做?

當(dāng)然是使用Automic。

使用Automic就簡單了,只需要這樣:

xcodebuild -workspace My.xcworkspace -scheme MySheme -destination generic/platform=iOS -configuration Release \

ONLY_ACTIVE_ARCH=NO \

CODE_SIGN_IDENTITY="iPhone Developer” \

PROVISIONING_PROFILE_SPECIFIER="Automatic" \

PROVISIONING_PROFILE="Automatic" \

CODE_SIGN_STYLE="Automatic" \

PROVISIONING_STYLE="Automatic" \

DEVELOPMENT_TEAM="My Team" \

-archivePath MyPath \

archive

你需要更換的是My.xcworkspace、MySheme 、"My Team"(你的Team Identifier)、MyPath(文件輸出路徑)。

我來解釋下原因:

1)證書的配置,在xcode 8以前,就有兩種方式:腳本更改aaa.xcproject文件、作為配置參數(shù)使用,只不過好多博客里,都是使用第一種。

2)Xcode 9才正式公開打包方式的配置:CODE_SIGN_SYTLE ,來配置automic、manual,默認(rèn)方式是automic,我也推薦這種方式。

3)CODE_SIGN_IDENTITY="iPhone Developer”,為何使用developer,因?yàn)榈搅薊xport階段,是可以重新code sign的,reCodeSign這是xcode 8更新的內(nèi)容。

4)基于將配置都作為參數(shù)使用,若你不用Automatic方式,而采用Manual,那PROVISIONING_PROFILE_SPECIFIER的配置會(huì)有問題,比如你的項(xiàng)目中做了AppExtention,會(huì)導(dǎo)致Extenion的簽名錯(cuò)誤,這也是我當(dāng)時(shí)遇到的問題,Apple未提供Extention的PROVISIONING_PROFILE_SPECIFIER該怎么配置。當(dāng)然,你喜歡用腳本更改aa.xcproject文件,是可以隨便更改。

5)最重要的一點(diǎn):使用automic,xcode是不會(huì)再revoke你的證書了。

下面是我在xcode 9的PDF中,找了幾個(gè)重點(diǎn)相關(guān)的截圖:


xocde 9


build settings

我當(dāng)時(shí)修改這些build settings ,也是比較費(fèi)勁,在Xcode中手動(dòng)更改證書等配置,然后使用fileMerge來比較到底有何不同,結(jié)合Xcode 9最后才總結(jié)出,使用Automic是非常簡單的。

3.2)Manual

1)如果你的App沒有做AppExtention,也可以跟上面使用Automic時(shí)一樣,作為參數(shù),無需腳本更改aa.xcproject文件

將如下命令中證書相關(guān)的,配置為準(zhǔn)確的值就可:

xcodebuild -workspace My.xcworkspace -scheme MySheme -destination generic/platform=iOS -configuration Release \

ONLY_ACTIVE_ARCH=NO \

CODE_SIGN_IDENTITY="iPhone Developer” \

PROVISIONING_PROFILE_SPECIFIER="Automatic" \

PROVISIONING_PROFILE="Automatic" \

CODE_SIGN_STYLE="Manual" \

PROVISIONING_STYLE="Manual" \

DEVELOPMENT_TEAM="My Team" \

-archivePath MyPath \

archive

2)可以使用,腳本更改aa.xcproject文件,無需作為參數(shù)執(zhí)行命令,例子如下

xcodebuild -workspace My.xcworkspace -scheme MySheme -destination generic/platform=iOS -configuration Release -archivePath MyPath archive

3.3)-xcconfig

Build Setsing作為參數(shù)使用還有一種方式,-xcconfig configTest.xcconfig,將那些Build Setsings參數(shù)寫在一個(gè).xcconfig文件中,xcode可以直接創(chuàng)建這個(gè)文件。

看一下Apple的解釋:

-xcconfig PATH ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

釋:apply the build settings defined in the file at PATH as overrides

我也喜歡這種方式,命令會(huì)很簡單:

xcodebuild -workspace My.xcworkspace -scheme MySheme -xcconfig configTest.xcconfig

4、ExportArchive

先看一下Apple給的例子:

xcodebuild -exportArchive -archivePath MyMobileApp.xcarchive -exportPath ExportDestination -exportOptionsPlist 'export.plist'

釋:Exports the archive MyMobileApp.xcarchive to the path ExportDestination using the options specified in export.plist.

xcode 8.3之后,exportOptionsPlist是必須指定,我在另一篇博客中,也有提到。

那exportOptionsPlist之中該如何寫?

先看一個(gè)官方的例子:

exportOptionsPlist 示例

再來看看每一個(gè)key啥意思:

compileBitcode : Bool

? For non-App Store exports, should Xcode re-compile the app from bitcode? Defaults to YES.

method : String

? Describes how Xcode should export the archive. Available options: app-store, ad-hoc, package, enterprise, development, developer-id, and mac-application. The list of options varies based on the type of archive. Defaults to development.

provisioningProfiles : Dictionary

? ? For manual signing only. Specify the provisioning profile to use for each executable in your app. Keys in this dictionary are the bundle identifiers of executables; values are the provisioning profile name or UUID to use.

signingCertificate : String

? ? For manual signing only. Provide a certificate name, SHA-1 hash, or automatic selector to use for signing. Automatic selectors allow Xcode to pick the newest installed certificate of a particular type. The available automatic selectors are "Mac App Distribution", "iOS Developer", "iOS Distribution", "Developer ID Application", and "Mac Developer". Defaults to an automatic certificate selector matching the current distribution method.

signingStyle : String

? ? The signing style to use when re-signing the app for distribution. Options are manual or automatic. Apps that were automatically signed when archived can be signed manually or automatically during distribution, and default to automatic. Apps that were manually signed when archived must be manually signed during distribtion, so the value of signingStyle is ignored.

teamID : String

The Developer Portal team to use for this export. Defaults to the team used to build the archive.

stripSwiftSymbols : Bool

? ? Should symbols be stripped from Swift libraries in your IPA? Defaults to YES.

其中我們關(guān)心的,就是provisioningProfile的配置,它是要指定你的所有bundleID以及對(duì)應(yīng)provisioning file的name或者UUID。

使用Automic打包時(shí),提到了re-sign,如何重簽名,稍加思考,就是exportOptionsPlist配置好證書那些玩意,有teamID、signingCertificate、provisioningProfiles

補(bǔ)充:Xcode 9還更新了兩個(gè)可選項(xiàng)(Options),一般用不到,因?yàn)槟愣紩?huì)將證書相關(guān)文件拷貝到本地

-allowProvisioningUpdates? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

? ? Allow xcodebuild to communicate with the Apple Developer website. For automatically signed targets, xcodebuild will create and update profiles, app IDs, and certificates. For manually signed targets, xcodebuild will download missing or updated provisioning profiles. Requires a developer account to have been added in Xcode's Accounts preference pane.

-allowProvisioningDeviceRegistration? ? ? ? ? ? ? ?

? ? Allow xcodebuild to register your destination device on the developer portal if necessary. This flag only takes effect if -allowProvisioningUpdates is also passed.

總結(jié):推薦使用Automic方式來Archive,在ExportArchive重新簽名。

Xcode 9更新:https://developer.apple.com/videos/play/wwdc2017/403/

若有疑問,可留言、簡信。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,702評(píng)論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,143評(píng)論 3 415
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,553評(píng)論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,620評(píng)論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,416評(píng)論 6 405
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 54,940評(píng)論 1 321
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,024評(píng)論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,170評(píng)論 0 287
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,709評(píng)論 1 333
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,597評(píng)論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,784評(píng)論 1 369
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,291評(píng)論 5 357
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,029評(píng)論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,407評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,663評(píng)論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,403評(píng)論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,746評(píng)論 2 370

推薦閱讀更多精彩內(nèi)容