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.

看不懂參數用途的自己谷歌翻譯吧,自己動手試試就清楚了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM