Xcode 9 最新 最准确 的自动化打包

Hi,

本文主要讲Xcode 9中,如何使用最新的、简单的方式来实现脚本打包。

code sign work flow

Xcode 8以后,关于iOS的App打包,Apple逐步做了调整,更简单了,我觉得是更趋近于傻瓜式了。

一开始接触iOS时,我们习惯使用XCode来做这些工作,有了自动化后,我们都是用命令行(Command line)来处理,所以本文基于命令行处理。

打包的具体流程应该是clean(清理缓存)、build(编译)、archive(打包)、export(导出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(可多个)组合使用;workspace和scheme(只一个)组合使用

2.configuration有Debug和Release两种模式。

3、Archive

先了解下打包,看一个 Apple提供的简单例子:

xcodebuild archive -workspace MyWorkspace.xcworkspace -scheme MyScheme

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

这例子中,除了指定必要的worksepace、sheme,未添加其他的配置,-configuration 默认是Release。

Xcode8 以后,Apple提供了两种思路:Automic(自动)、manual(手动),所以我们按此来分析。

3.1)Automic

Apple推荐的做法是什么?

先看一张图:

答案是:Automic,且code sign时使用Development。

很多人要问,以前的做法是archive时,是必须指定正确那一堆麻烦的证书配置,如code sign identify、team、provision file,也有很多人被此坑过。

Now,你无需担心这玩意,也无需使用脚本来修改aaa.xcproject文件中的那些证书配置,哪怕其中的配置都是错误的,参数会作为命令执行的标准,且也不会自动更改你的aaa.xcproject文件。

那该如何做?

当然是使用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文件、作为配置参数使用,只不过好多博客里,都是使用第一种。

2)Xcode 9才正式公开打包方式的配置:CODE_SIGN_SYTLE ,来配置automic、manual,默认方式是automic,我也推荐这种方式。

3)CODE_SIGN_IDENTITY="iPhone Developer”,为何使用developer,因为到了Export阶段,是可以重新code sign的,reCodeSign这是xcode 8更新的内容。

4)基于将配置都作为参数使用,若你不用Automatic方式,而采用Manual,那PROVISIONING_PROFILE_SPECIFIER的配置会有问题,比如你的项目中做了AppExtention,会导致Extenion的签名错误,这也是我当时遇到的问题,Apple未提供Extention的PROVISIONING_PROFILE_SPECIFIER该怎么配置。当然,你喜欢用脚本更改aa.xcproject文件,是可以随便更改。

5)最重要的一点:使用automic,xcode是不会再revoke你的证书了。

下面是我在xcode 9的PDF中,找了几个重点相关的截图:


xocde 9


build settings

我当时修改这些build settings ,也是比较费劲,在Xcode中手动更改证书等配置,然后使用fileMerge来比较到底有何不同,结合Xcode 9最后才总结出,使用Automic是非常简单的。

3.2)Manual

1)如果你的App没有做AppExtention,也可以跟上面使用Automic时一样,作为参数,无需脚本更改aa.xcproject文件

将如下命令中证书相关的,配置为准确的值就可:

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文件,无需作为参数执行命令,例子如下

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

3.3)-xcconfig

Build Setsing作为参数使用还有一种方式,-xcconfig configTest.xcconfig,将那些Build Setsings参数写在一个.xcconfig文件中,xcode可以直接创建这个文件。

看一下Apple的解释:

-xcconfig PATH                              

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

我也喜欢这种方式,命令会很简单:

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之中该如何写?

先看一个官方的例子:

exportOptionsPlist 示例

再来看看每一个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.

其中我们关心的,就是provisioningProfile的配置,它是要指定你的所有bundleID以及对应provisioning file的name或者UUID。

使用Automic打包时,提到了re-sign,如何重签名,稍加思考,就是exportOptionsPlist配置好证书那些玩意,有teamID、signingCertificate、provisioningProfiles

补充:Xcode 9还更新了两个可选项(Options),一般用不到,因为你都会将证书相关文件拷贝到本地

-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.

总结:推荐使用Automic方式来Archive,在ExportArchive重新签名。

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

若有疑问,可留言、简信。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 151,688评论 1 330
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 64,559评论 1 273
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 101,749评论 0 226
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 42,581评论 0 191
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 50,741评论 3 271
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 39,684评论 1 192
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,122评论 2 292
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,847评论 0 182
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,441评论 0 228
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,939评论 2 232
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,333评论 1 242
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,783评论 2 236
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,275评论 3 220
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,830评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,444评论 0 180
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 34,553评论 2 249
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 34,618评论 2 249

推荐阅读更多精彩内容