iOS自动化打包命令xcodebuild大全

iOS实现自动化打包已经稳定运营几年了,不同的场景用到xcodebuild命令不一样,有的参数可能一直都用不到,列举一些常用的命令,比如编译命令:

xcodebuild archive 
-workspace covermedia.xcworkspace 
-scheme covermedia 
-configuration Release 
-archivePath /Users/Work/package/iOS/ex_rm_hk/AdHoc/20220110155609/covermedia.xcarchive 
-allowProvisioningUpdates 
-allowProvisioningDeviceRegistration
-derivedDataPath /Users/Work/package/derived_data/ex_rm_hk/ > /Users/Work/package/iOS/ex_rm_hk/log/03_build.log 

上面这个命令是基于xcode配置了自动获取证书的,如果没有配置,则需要指定证书,不然当有几十个iOS账号自动化打包时,会出现证书没指定而打包失败:

xcodebuild archive 
-workspace covermedia.xcworkspace 
-scheme covermedia 
-configuration Release 
-archivePath /Users/Work/package/iOS/ex_fm_cm/Development/covermedia.xcarchive 
"CODE_SIGN_IDENTITY"="Apple Distribution: Sicxxxan Coxx Mexx Co., Ltd. (SWMxx5PP)"  
"PROVISIONING_PROFILE"="fe8exxf-bf11-4965-86xx-2d6axxxxc27c1"
-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.

解释说是允许注册当前的设备,但往往场景是这样的:刚加了一个设备到iOS账号里,随后打新包,新加的设备无法安装。加上这个参数,打包时就会同步账号里的设备更新到本地设备文件里,再打新包,新的设备就能安装上了。

这个命令也很重要。

-derivedDataPath PATH   specifies the directory where build products and other derived data will go

指定编译过程中的生成文件,编译生成的文件一般存在/xxxxx/xxxx/deriveddata文件夹里(xcode里有配置这个地址),以workspace名称为前缀分项目存储,当然,好的开发习惯很重要,比如不同的项目workspace不同,而我遇到的研发恰恰是坏习惯:

我们做融媒体项目的iOS很多,项目之间很多是可以复用的,所谓定制化开发无非是把之前的某个项目拷过去修改某些功能。所以,比如A项目的workspace为covermedia,那么后续的项目几乎都叫covermedia,

有人说这也没啥,打包前用clean参数清理一下环境就好:

xcodebuild clean 
-workspace covermedia.xcworkspace 
-scheme covermedia
-configuration Release > /Users/Work/iOS/exrmle/log/01clean.log

但现实并不是如此,clean命令并不会清理deriveddata里的数据,或者说清理不到位,这个结论是无数次打包后得出的结果,比如不删除这个文件打的包有的功能没有更新,但change里看代码是提交了的,然后删除deriveddate里的数据再打包,功能又ok了。

所以就写了个打包前先删除deriveddata数据的命令,但是,上面说了,deriveddata里的文件是以workspace为前缀分项目存储的,哪个项目对应的是哪个文件并不知道,删除只能根据前缀模糊删除。当A项目打包中,B项目开始打包,执行删除deriveddata命令,

会把A项目编译文件也删除,完蛋,A项目必然会打包失败。如此,并发打包就不能实现。但有了-derivedDataPath参数,一切又可以解决了:打包时候各个项目指定存储路径,编译前删除各自的编译缓存文件,互不干扰,再多并发打包都没问题。

所以,有些参数看起来一直都用不到,只是没遇到特定场景而已。

常用的还有:

xcodebuild -exportArchive 
-archivePath /Users/Work/iOS/exrmle/AppStore/20190929171231/covermedia.xcarchive 
-exportOptionsPlist /Users/Work/iOS/exrmle/AppStore/ExportOptions.plist  
-exportPath /Users/Work/iOS/exrmle/AppStore/20190929171231/ > /Users/Work/iOS/exrmle/log/03export.log
archive 编译完后会生成.xcarchive文件,用-exportArchive命令导出生成ipa包,这里的-exportOptionsPlist是指定导出ipa包的属性,常见简单的plist文件有这些参数:
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>compileBitcode</key>
    <false/>
    <key>method</key>
    <string>ad-hoc</string>
    <key>signingStyle</key>
    <string>automatic</string>
    <key>stripSwiftSymbols</key>
    <true/>
    <key>teamID</key>
    <string>9KN4FBRN8M</string>
    <key>thinning</key>
    <string>&lt;none&gt;</string>
</dict>
</plist>

所以,当生成一个.xcarchive文件后,我们可以根据不同的plist属性导出不同类型的包,比如ad-hoc,appstore,develop。自动化打包过程中,archive 花的时间是最长的,如果没必要,尽量不编译,比如验证完ad-hoc的包没有问题后,可以直接导出文件生成

appstore类型上传到苹果市场,分分钟搞定,不用去等待漫长的编译时间。

清理、编译、导出、上传,上传用xcrun altool 命令:

xcrun altool 
--validate
-app -f /Users/Work/iOS/exrmle/AppStore/20190929171231/covermedia.ipa
-t ios --apiKey T8xxxD8 --apiIssuer 6xxxx89-bxxb-4xxe3-exx3-5b8xxxx1a4d1 --verbose --output-format xml # --verbose 显示详细日志信息,加上的话,会输出一堆东西,可以直接去掉 # --output-format xml 输出日志格式为xml结构化显示 xcrun 需要的key、issure的生成方式: 1.登录iTunesConnect --->用户与访问--->密钥,至此,生成相应身份的密钥,再将私钥下载下来 2.apiKey的值,就是图中的密钥ID,我们将其对应私钥下载下来后,需要放到一个固定目录下,'./private_keys'或者'~/private_keys' 或者'~/.private_keys' 或者'~/.appstoreconnect/private_keys'.

上传分为两个步骤,一个验证--validate,一个上传--upload,验证没有问题后才执行上传操作,验证步骤会苹果官网会静态分析ipa里有没有不合规的地方和版本号版本id是否已经存在等,验证通过后,你再去调用上传命令:

xcrun altool 
--upload
-app 
-f /Users/Work/iOS/exrmle/AppStore/20190929171231/covermedia.ipa 
-t ios --apiKey TXXXXX8 --apiIssuer 6XXX-bXXb-XXX3-e053-5xxXXXXXXXd1 --verbose --output-format xml

自动化打包都不要加上--output-format xml,执行上传命令后,输出界面就会实时显示上传日志,也能实时看到上传文件进度,一般一两分钟内就会上传成功,如果慢的话,就开启VPN吧:

mac自动启动某个程序

以上是常用的命令。

xcodebuild命令大全:

Usage: xcodebuild [-project <projectname>] [[-target <targetname>]...|-alltargets] [-configuration <configurationname>] [-arch <architecture>]... [-sdk [<sdkname>|<sdkpath>]] [-showBuildSettings [-json]] [<buildsetting>=<value>]... [<buildaction>]...
       xcodebuild [-project <projectname>] -scheme <schemeName> [-destination <destinationspecifier>]... [-configuration <configurationname>] [-arch <architecture>]... [-sdk [<sdkname>|<sdkpath>]] [-showBuildSettings [-json]] [-showdestinations] [<buildsetting>=<value>]... [<buildaction>]...
       xcodebuild -workspace <workspacename> -scheme <schemeName> [-destination <destinationspecifier>]... [-configuration <configurationname>] [-arch <architecture>]... [-sdk [<sdkname>|<sdkpath>]] [-showBuildSettings] [-showdestinations] [<buildsetting>=<value>]... [<buildaction>]...
       xcodebuild -version [-sdk [<sdkfullpath>|<sdkname>] [-json] [<infoitem>] ]
       xcodebuild -list [[-project <projectname>]|[-workspace <workspacename>]] [-json]
       xcodebuild -showsdks [-json]
       xcodebuild -exportArchive -archivePath <xcarchivepath> [-exportPath <destinationpath>] -exportOptionsPlist <plistpath>
       xcodebuild -exportNotarizedApp -archivePath <xcarchivepath> -exportPath <destinationpath>
       xcodebuild -exportLocalizations -localizationPath <path> -project <projectname> [-exportLanguage <targetlanguage>...[-includeScreenshots]]
       xcodebuild -importLocalizations -localizationPath <path> -project <projectname> [-mergeImport]
       xcodebuild -resolvePackageDependencies [-project <projectname>|-workspace <workspacename>] -clonedSourcePackagesDirPath <path>
       xcodebuild -create-xcframework [-help] [-framework <path>] [-library <path> [-headers <path>]] -output <path>

 

Options:
    -usage                                                   print brief usage
    -help                                                    print complete usage
    -verbose                                                 provide additional status output
    -license                                                 show the Xcode and SDK license agreements
    -checkFirstLaunchStatus                                  Check if any First Launch tasks need to be performed
    -runFirstLaunch                                          install packages and agree to the license
    -project NAME                                            build the project NAME
    -target NAME                                             build the target NAME
    -alltargets                                              build all targets
    -workspace NAME                                          build the workspace NAME
    -scheme NAME                                             build the scheme NAME
    -configuration NAME                                      use the build configuration NAME for building each target
    -xcconfig PATH                                           apply the build settings defined in the file at PATH as overrides
    -arch ARCH                                               build each target for the architecture ARCH; this will override architectures defined in the project
    -sdk SDK                                                 use SDK as the name or path of the base SDK when building the project
    -toolchain NAME                                          use the toolchain with identifier or name NAME
    -destination DESTINATIONSPECIFIER                        use the destination described by DESTINATIONSPECIFIER (a comma-separated set of key=value pairs describing the destination to use)
    -destination-timeout TIMEOUT                             wait for TIMEOUT seconds while searching for the destination device
    -parallelizeTargets                                      build independent targets in parallel
    -jobs NUMBER                                             specify the maximum number of concurrent build operations
    -maximum-concurrent-test-device-destinations NUMBER      the maximum number of device destinations to test on concurrently
    -maximum-concurrent-test-simulator-destinations NUMBER   the maximum number of simulator destinations to test on concurrently
    -parallel-testing-enabled YES|NO                         overrides the per-target setting in the scheme
    -parallel-testing-worker-count NUMBER                    the exact number of test runners that will be spawned during parallel testing
    -maximum-parallel-testing-workers NUMBER                 the maximum number of test runners that will be spawned during parallel testing
    -dry-run                                                 do everything except actually running the commands
    -quiet                                                   do not print any output except for warnings and errors
    -hideShellScriptEnvironment                              don't show shell script environment variables in build log
    -showsdks                                                display a compact list of the installed SDKs
    -showdestinations                                        display a list of destinations
    -showTestPlans                                           display a list of test plans
    -showBuildSettings                                       display a list of build settings and values
    -showBuildSettingsForIndex                               display build settings for the index service
    -list                                                    lists the targets and configurations in a project, or the schemes in a workspace
    -find-executable NAME                                    display the full path to executable NAME in the provided SDK and toolchain
    -find-library NAME                                       display the full path to library NAME in the provided SDK and toolchain
    -version                                                 display the version of Xcode; with -sdk will display info about one or all installed SDKs
    -enableAddressSanitizer YES|NO                           turn the address sanitizer on or off
    -enableThreadSanitizer YES|NO                            turn the thread sanitizer on or off
    -enableUndefinedBehaviorSanitizer YES|NO                 turn the undefined behavior sanitizer on or off
    -resultBundlePath PATH                                   specifies the directory where a result bundle describing what occurred will be placed
    -resultStreamPath PATH                                   specifies the file where a result stream will be written to (the file must already exist)
    -resultBundleVersion 3 [default]                         specifies which result bundle version should be used
    -clonedSourcePackagesDirPath PATH                        specifies the directory to which remote source packages are fetch or expected to be found
    -derivedDataPath PATH                                    specifies the directory where build products and other derived data will go
    -archivePath PATH                                        specifies the directory where any created archives will be placed, or the archive that should be exported
    -exportArchive                                           specifies that an archive should be exported
    -exportNotarizedApp                                      export an archive that has been notarized by Apple
    -exportOptionsPlist PATH                                 specifies a path to a plist file that configures archive exporting
    -enableCodeCoverage YES|NO                               turn code coverage on or off when testing
    -exportPath PATH                                         specifies the destination for the product exported from an archive
    -skipUnavailableActions                                  specifies that scheme actions that cannot be performed should be skipped instead of causing a failure
    -exportLocalizations                                     exports completed and outstanding project localizations
    -importLocalizations                                     imports localizations for a project, assuming any necessary localized resources have been created in Xcode
    -localizationPath                                        specifies a path to XLIFF localization files
    -exportLanguage                                          specifies multiple optional ISO 639-1 languages included in a localization export
    -xcroot                                                  specifies a path to a .xcroot to use for building and/or testing
    -xctestrun                                               specifies a path to a test run specification
    -testPlan                                                specifies the name of the test plan associated with the scheme to use for testing
    -only-testing                                            constrains testing by specifying tests to include, and excluding other tests
    -only-testing:TEST-IDENTIFIER                            constrains testing by specifying tests to include, and excluding other tests
    -skip-testing                                            constrains testing by specifying tests to exclude, but including other tests
    -skip-testing:TEST-IDENTIFIER                            constrains testing by specifying tests to exclude, but including other tests
    -test-timeouts-enabled YES|NO                            enable or disable test timeout behavior
    -default-test-execution-time-allowance SECONDS           the default execution time an individual test is given to execute, if test timeouts are enabled
    -maximum-test-execution-time-allowance SECONDS           the maximum execution time an individual test is given to execute, regardless of the test's preferred allowance
    -only-test-configuration                                 constrains testing by specifying test configurations to include, and excluding other test configurations
    -skip-test-configuration                                 constrains testing by specifying test configurations to exclude, but including other test configurations
    -testLanguage                                            constrains testing by specifying ISO 639-1 language in which to run the tests
    -testRegion                                              constrains testing by specifying ISO 3166-1 region in which to run the tests
    -resolvePackageDependencies                              resolves any Swift package dependencies referenced by the project or workspace
    -disableAutomaticPackageResolution                       prevents packages from automatically being resolved to versions other than those recorded in the `Package.resolved` file
    -disablePackageRepositoryCache                           disable use of a local cache of remote package repositories
    -packageCachePath                                        path of caches used for package support
    -json                                                    output as JSON (note: -json implies -quiet)
    -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.
    -scmProvider                                             which implementation to use for Git operations (system/xcode)
    -showBuildTimingSummary                                  display a report of the timings of all the commands invoked during the build
    -create-xcframework                                      create an xcframework from prebuilt libraries; -help for more information.

看不懂参数用途的自己谷歌翻译吧,自己动手试试就清楚了。

 

posted @ 2022-01-10 19:34  drewgg  阅读(3646)  评论(0编辑  收藏  举报