手动给测试打包还是太费事了,尤其是项目要发布前,一天都要打N次包,还要上传蒲公英这类托管平台 给测试MM扫码下载。效率太低了,所以有时候自动打包,打完包自动上传蒲公英托管平台 还是很有必要的。
xcode提供了这样一套命令行管理项目工具 xcodebuild。
xcodebuild 简介
xcodebuild的官方文档可以通过在命令行输入man xcodebuild查看(当然也可以通过官方文档查看)。
官方文档详细列出了各个命令的格式,说明,并且还有举例。所以阅读官方文档还是很有必要的。
这个xcodebuild的使用还是很简单的,基本命令就是xcodebuild。其他都是参数和选项。
官方的使用说明
Usage
To build an Xcode project, run xcodebuild from the directory containing your
project (i.e. the directory containing the name.xcodeproj package). If you have
multiple projects in the this directory you will need to use -project to indicate
which project should be built. By default, xcodebuild builds the first target
listed in the project, with the default build configuration. The order of the tar-
gets is a property of the project and is the same for all users of the project.
To build an Xcode workspace, you must pass both the -workspace and -scheme options
to define the build. The parameters of the scheme will control which targets are
built and how they are built, although you may pass other options to xcodebuild to
override some parameters of the scheme.
There are also several options that display info about the installed version of
Xcode or about projects or workspaces in the local directory, but which do not ini-
tiate an action. These include -list, -showBuildSettings, -showsdks, -usage, and
-version.
需要在包含 name.xcodeproj 的目录下执行 xcodebuild 命令,且如果该目录下有多个 projects,那么需要使用 -project 指定需要 build 的项目。
在不指定 build 的 target 的时候,默认情况下会 build project 下的第一个 target
当 build workspace 时,需要同时指定 -workspace 和 -scheme 参数,scheme 参数控制了哪些 targets 会被 build 以及以怎样的方式 build。
有一些诸如 -list, -showBuildSettings, -showsdks 的参数还可以查看项目或者工程的信息,资本性不会对 build action 造成任何影响,可以放心使用。
命令列表:
xcodebuild [-project name.xcodeproj] [[-target targetname] ... | -alltargets]
[-configuration configurationname] [-sdk [sdkfullpath | sdkname]]
[action ...] [buildsetting=value ...] [-userdefault=value ...]
xcodebuild [-project name.xcodeproj] -scheme schemename
[[-destination destinationspecifier] ...] [-destination-timeout value]
[-configuration configurationname] [-sdk [sdkfullpath | sdkname]]
[action ...] [buildsetting=value ...] [-userdefault=value ...]
xcodebuild -workspace name.xcworkspace -scheme schemename
[[-destination destinationspecifier] ...] [-destination-timeout value]
[-configuration configurationname] [-sdk [sdkfullpath | sdkname]]
[action ...] [buildsetting=value ...] [-userdefault=value ...]
xcodebuild -version [-sdk [sdkfullpath | sdkname]] [infoitem]
xcodebuild -showsdks
xcodebuild -showBuildSettings
[-project name.xcodeproj | [-workspace name.xcworkspace -scheme schemename]]
xcodebuild -list [-project name.xcodeproj | -workspace name.xcworkspace]
xcodebuild -exportArchive -archivePath xcarchivepath -exportPath destinationpath
-exportOptionsPlist path
xcodebuild -exportLocalizations -project name.xcodeproj -localizationPath path
[[-exportLanguage language] ...]
xcodebuild -importLocalizations -project name.xcodeproj -localizationPath path
xcodebuild使用
上面列出了官方文档对xcodebuild的介绍和命令说明,下面具体看看如何使用。常用打包命令有六个,分别是三个查看项目配置命令,三个打包对应的命令。
查看项目配置的命令很简单,分别是
- xcodebuild -showsdks: 列出 Xcode 所有可用的 SDKs
- xcodebuild -showBuildSettings: 查看当前工程 build setting 的配置参数,Xcode 详细的 build setting 参数参考官方文档 Xcode Build Setting Reference, 已有的配置参数可以在终端中以 buildsetting=value 的形式进行覆盖重新设置
- xcodebuild -list: 上述序号7的使用方式,查看 project 中的 targets 和 configurations,或者 workspace 中 schemes, 输出如下:
Information about project "location":
Targets:
location
Build Configurations:
Debug
Release
If no build configuration is specified and -scheme is not passed then "Release" is used.
Schemes:
location
这就是查看工程配置方面的命令,他们不会对工程产生任何修改,可以放心玩耍,而且打包时候的部分参数,是一定要从这些配置里面选取的。
下面是三个与打包相关的命令:
1.xcodebuild -project
用xcode随便创建一个测试工程,打开终端, cd进入工程目录(还有.xcodeproj文件)。直接输入xcodebuild,按回车,就可以看到系统已经开始编译工程,终端不停的有内容输出。当终端显示** BUILD SUCCEEDED **时候,可以看到工程目录下多了build目录,该目录下有 Release-iphoneos 和 TestImg.build 文件,根据我们 build -configuration 配置的参数不同,Release-iphoneos 的文件名会不同。这个路径很重要,如果需要导出ipad包,就会用到它
这个命令很简单吧,其实它是缺省了很多的默认参数。
完整命令:
xcodebuild [-project name.xcodeproj] [[-target targetname] … | -alltargets] [-configuration configurationname] [-sdk [sdkfullpath | sdkname]] [action …] [buildsetting=value …] [-userdefault=value …]
这个命令会编译指定的project。如果缺省的话,就编译工程中的第一个.xcodeproj。target选项用于指定target,configuration是设置release或者debug模式,这些都必须由xcodebuild -list获取,-sdk选项的参数由xcodebuild -showsdks获取。
这个命令用于普通工程打包
2.xcodebuild -workspace
xcodebuild -workspace name.xcworkspace -scheme schemename
[[-destination destinationspecifier] ...] [-destination-timeout value]
[-configuration configurationname] [-sdk [sdkfullpath | sdkname]]
[action ...] [buildsetting=value ...] [-userdefault=value ...]
基本使用同上述一样,需要指定编译的xcworkspace和scheme,scheme也必须由xcodebuild -list获取。这个命令用于workspace打包
3.xcodebuild -workspace
xcodebuild -exportArchive -archivePath xcarchivepath -exportPath destinationpath
-exportOptionsPlist path
生成ipa包命令,archivePath是上面两个命令生成的路径,exportPath是导出的ipa包路径 -exportOptionsPlist path是指定工程打包配置属性(例如是否开启bitcode )的plist文件路径,这个plist的名字是exportOptions.plist。
内容格式:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>teamID</key>
<string>UA11AAJJKK8</string> //TeamID
<key>method</key>
<string>ad-hoc</string> //ad-hoc打包
<key> compileBitcode</key> //是否编译bitcode
<false/>
</dict>
</plist>
其中每一行key-value都是可选配置,完整配置可以参考官方文档,想要更改什么配置,将它写入plist即可
compileBitcode : Bool
For non-App Store exports, should Xcode re-compile the app from bitcode? Defaults to YES.
embedOnDemandResourcesAssetPacksInBundle : Bool
For non-App Store exports, if the app uses On Demand Resources and this is YES, asset
packs are embedded in the app bundle so that the app can be tested without a server to
host asset packs. Defaults to YES unless onDemandResourcesAssetPacksBaseURL is specified.
iCloudContainerEnvironment
For non-App Store exports, if the app is using CloudKit, this configures the
"com.apple.developer.icloud-container-environment" entitlement. Available options:
Development and Production. Defaults to Development.
manifest : Dictionary
For non-App Store exports, users can download your app over the web by opening your
distribution manifest file in a web browser. To generate a distribution manifest, the
value of this key should be a dictionary with three sub-keys: appURL, displayImageURL,
fullSizeImageURL. The additional sub-key assetPackManifestURL is required when using on demand resources.
method : String
Describes how Xcode should export the archive. Available options: app-store, ad-hoc,
package, enterprise, development, and developer-id. The list of options varies based on
the type of archive. Defaults to development.
onDemandResourcesAssetPacksBaseURL : String
For non-App Store exports, if the app uses On Demand Resources and
embedOnDemandResourcesAssetPacksInBundle isn't YES, this should be a base URL specifying
where asset packs are going to be hosted. This configures the app to download asset
packs from the specified URL.
teamID : String
The Developer Portal team to use for this export. Defaults to the team used to build the archive.
thinning : String
For non-App Store exports, should Xcode thin the package for one or more device
variants? Available options: <none> (Xcode produces a non-thinned universal app),
<thin-for-all-variants> (Xcode produces a universal app and all available thinned
variants), or a model identifier for a specific device (e.g. "iPhone7,1"). Defaults to <none>.
uploadBitcode : Bool
For App Store exports, should the package include bitcode? Defaults to YES.
uploadSymbols : Bool
For App Store exports, should the package include symbols? Defaults to YES.