Hi,
本文主要講Xcode 9中,如何使用最新的、簡單的方式來實(shí)現(xiàn)腳本打包。
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)的截圖:
我當(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è)官方的例子:
再來看看每一個(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/
若有疑問,可留言、簡信。