美文网首页iOSios专题iOS精选
iOS 自动构建命令——xcodebuild

iOS 自动构建命令——xcodebuild

作者: ChenJZ | 来源:发表于2016-01-27 16:38 被阅读34506次

    想想当初天天来到公司,每天需要做一件事就是打开Xcode打包ipa,上传到fir。日复一日月复一月年复一年的做着同样的事情,作为有志成为优秀工程师的我来说,这是必须要解决的问题,所以决定自动化解决问题。

    简介

    xcodebuild 是苹果发布自动构建的工具。它在一个Xcode项目下能构建一个或者多个targets ,也能在一个workspace或者Xcode项目上构建scheme,总的来说,用它没错就是了。

    用法说明

    Tips:在终端输入man xcodebuild,可以看到Description里面有介绍用法。

    也可以看官方文档

    当你想构建一个Xcode项目,在项目目录下运行xcodebuild就可以了(目录下面包含着projectname.xcodeproj文件就行),如果目录下有多个项目,你需要用参数-project指定一个项目。默认xcodebuild命令会构建你第一个target的。当然你也可以用-targetname指定。

    如果要构建workspace,你必须指定-workspace-scheme参数。

    当然你可以以用就比如-version、-showsdks、-list等一些命令来获取一些项目相关的参数。

    之前文章使用xcodebuild+ xcrun的PackageApplication打包,但是已经不推荐使用了。下面使用arhive和exportArchive来打包

    Archive包

    在shell里面 [ ]表示这个参数是可选的,< > 表示参数是必须的

    话不多说,先上个命令:

        xcodebuild archive -workspace 项目名称.xcworkspace 
                           -scheme 项目名称 
                           -configuration 构建配置 
                           -archivePath archive包存储路径 
                           CODE_SIGN_IDENTITY=证书 
                           PROVISIONING_PROFILE=描述文件UUID  
    
    • -workspace 这个就是项目名
    • -scheme 可以通过xcodebuild -list获取
    • -configration 一些参数,也可以通过xcodebuild -list获取,一般使用Debug、Release
    • -archivePath archive后的路径
    • CODE_SIGN_IDENTITY 证书的Inentity
    • PROVISIONING_PROFILE 描述文件UUID

    来看看xcodebuild -list吧,看看怎么获取scheme和configration

         Information about project "ThreeDTouchTest":
            Targets:
                ThreeDTouchTest
                ThreeDTouchTestTests
                ThreeDTouchTestUITests
        
            Build Configurations:
                Debug
                Release
        
            If no build configuration is specified and -scheme is not passed then "Release" is used.
        
            Schemes:
                ThreeDTouchTest
    

    如果不需要特定去指定证书和Provisioning文件,那可以省略上面这两个参数也可以。不过还是得说一下这两参数怎么获取:

    证书Identity获取:

    打开你的钥匙串访问->选中其中一个证书->右键->显示简介,把标题复制出来就可以了。

    格式是:

    iPhone Distribution: Beijing xxoo yyooxx Technology Service CO., Ltd. (UA11AAJJKK8)

    证书.png

    获取Provisioning文件UUID

    在xcode8.0以上,Provisioning文件的位置是:

    /Users/用户名/Library/MobileDevice/Provisioning Profiles

    在终端上面进入的文件夹。使用/usr/bin/security可以把Provisioning文件解密

    /usr/bin/security cms -D -i 098a87e3-11fe-463d-75aa-12345678adba.mobileprovision
    

    在终端输出整个plist文件,里面包含着所有的信息

    对了,还有这个命令可以查看项目设置:

    xcodebuild -target <target> -configuration <configuration> -showBuildSettings
    

    生成ipa文件

    PackageApplication已经不推荐使用了。下面说一下另外一种打包的方法:

    xcodebuild -exportArchive -archivePath archive文件的地址.xcarchive 
                              -exportPath 导出的文件夹地址 
                              -exportOptionsPlist exprotOptionsPlist.plist 
                              CODE_SIGN_IDENTITY=证书 
                              PROVISIONING_PROFILE=描述文件UUID  
    

    同样,如果你不需要的指定证书和Provisioning文件,可以把上面的两个参数去掉,它会根据你的Xcode配置去匹配。

    exportOptionsPlist这个参数,它是一个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>
    

    下面是关于其他字段的解释:

    Available keys for -exportOptionsPlist:
    
        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.
    

    上传到Fir

    这个就更简单了,敬请参照:Fir的命令行客户端

    总结

    作为开发人员,肯定不可能天天跟着测试人员跑。自动化是非常有必要的,所以会点脚本,肯定不会吃亏。

    参考文档

    官方文档

    Mac Security工具使用总结

    iOS自动打包并发布脚本

    相关文章

      网友评论

      • Sainbyno:你好请问exportOptionsPlist这是什么啊,是怎么来的?是自己在项目目录里手动创建的么?
        ChenJZ:在终端里面 运行 `xcodebuild -help` 可以看得到 Available keys for -exportOptionsPlist: 下面就是里面的选项
        ChenJZ:自己创建的
      • 从此你不再颠沛流离:CODE_SIGN_IDENTITY=证书 证书字母之间有空格不识别证书怎么搞
        ChenJZ:用双引号包含起来
      • ad6850e88ae6:在本地脚本执行 xcodebuild archive 和 xcodebuild exportArchive 的时候,打出的包是可用的
        profile 和 证书是xcode自动管理的方式,

        但是把这个脚本放在jenkins中执行的时候 在console out 界面会报错
        No profiles for 'com.shcmct.cmfieldwork' were found: Xcode couldn't find a provisioning profile matching 'com.shcmct.cmfieldwork'
        并且在执行archive的时候回一直卡在那里
      • ad6850e88ae6:最后export 执行的时候 出现 archiveError Domain=IDEDistributionErrorDomain Code=1 "No valid iOS Distribution signing identities belonging to team 66PR3647A7 were found." UserInfo={NSLocalizedDescription=No valid iOS Distribution signing identities belonging to team 66PR3647A7 were found.}
        ChenJZ:错误内容不是说了么?没有有效的发布证书
      • 拾取灬回忆:打出来的ipa可以上传的苹果审核么?
        ChenJZ:可以,你选择的证书是发布证书就行了。如果想打包上传到AppStore一体化的,可以试一下fastlane
      • 847848c1cf27:Build log exceeded limit of 4194304 bytes.
        这是什么问题呢?到这里就失败了。
        一个栗:xcodebuild作自动化测试,如何只执行某一个用例呢
        847848c1cf27:@ChenJZ 谢谢,已经弄好了,不是这里的问题,这里只是log太多打印不出来了,以至于错误的信息没有被打印出来,导致我以为是这里出错了。
        ChenJZ:@百家第三 错误不是说了log的最大值了么
      • FicowShen:那个官方文档是过时了的,建议还是直接终端man xcodebuild看教程吧,而且里面写的挺详细的,还有例子的呢!:smile:
        ChenJZ:@FicowShen 上面有建议了,但是还是得养成看官方文档的的好习惯
      • 00bb77ad31da:你好,请问,你在脚本中配置工程的 buildsettings 后打包出来的 ipa 能安装成功不?
      • 小学生课代表:请问一下我加了输出路径之后报错"Ld /Users/chinapan/Library/Developer/Xcode/DerivedData/Build/Intermediates/PerfectBuyText.build/Release-iphoneos/PerfectBuyText.build/Objects-normal/arm64/PerfectBuyText normal arm64"
        不加的话就可以编译成功,是怎么回事
      • kl_: :smile:
      • 晒着的鱼_9524:用Xcodebuild -exportarchive 来打包ipa
        ChenJZ:@晒着的鱼_9524 嗯,是的。
      • 叶舞清风:福利!
      • wangyu2488:你好,请问你是怎么把部分字体设成青绿色的,简书默认字体颜色都是黑色
        wangyu2488:@ChenJZ 噢我知道了,原来是链接
        ChenJZ:@wangyu2488 青绿色?是指那些链接吗?
      • 魔鬼分界线:尼玛,我说杂嘛找不到打包workspace后的文件呢,原来要加输出路径
      • d546e5af7f42:PackageApplication 已经被apple弃用了。在新版xcode不兼容。
        d546e5af7f42:http://www.matrixprojects.net/p/xcodebuild-export-options-plist/#tldr

      本文标题:iOS 自动构建命令——xcodebuild

      本文链接:https://www.haomeiwen.com/subject/whodkttx.html