背景:
在此之前,我是使用过fastlane进行打包的,据我的了解,fastlane
也是xcodebuild
,altool
等一系列xcode打包命令的集合。因为换了公司,公司的开发者账号并没有向全部开发者开放,所以功能更强大的fastlane
在这里肯定是用不了了。但也不甘愿就此回到解放前(完全手动点击去打包。)
所以,就决定自己讲xcode打包命令集成到一个脚本上面,到时候自己运行脚本打包。这虽然不够完全自动化,但也能很大程度上省时省力了
废话到此结束,进入正题
xcodebuild
目前使用的xcode版本是9.4
xcodebuild -h
: 查看xcodebuild
改如何使用
在终端输入 xcodebuild -h
可以看到如下常用命令
➜ Desktop xcodebuild -h
Usage: xcodebuild [-project <projectname>] [[-target <targetname>]...|-alltargets] [-configuration <configurationname>] [-arch <architecture>]... [-sdk [<sdkname>|<sdkpath>]] [-showBuildSettings] [<buildsetting>=<value>]... [<buildaction>]...
xcodebuild [-project <projectname>] -scheme <schemeName> [-destination <destinationspecifier>]... [-configuration <configurationname>] [-arch <architecture>]... [-sdk [<sdkname>|<sdkpath>]] [-showBuildSettings] [-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>] [<infoitem>] ]
xcodebuild -list [[-project <projectname>]|[-workspace <workspacename>]] [-json]
xcodebuild -showsdks
xcodebuild -exportArchive -archivePath <xcarchivepath> -exportPath <destinationpath> -exportOptionsPlist <plistpath>
xcodebuild -exportLocalizations -localizationPath <path> -project <projectname> [-exportLanguage <targetlanguage>...]
xcodebuild -importLocalizations -localizationPath <path> -project <projectname>
xcodebuild -resolvePackageDependencies [-project <projectname>|-workspace <workspacename>] -clonedSourcePackagesDirPath <path>
这里我主要介绍以下几个命令
-
xcodebuild clean
: 清理项目缓存,等同于xcode快捷键shift+command+K
一般使用:
xcodebuild clean -workspace ${TARGET_NAME}.xcworkspace -scheme ${SCHEME_NAME} -configuration ${BUILD_TYPE}
-
xcodebuild archive
: 导出.xcarchive文件
一般使用:
xcode9+:
xcodebuild archive -workspace ${TARGET_NAME}.xcworkspace -scheme ${SCHEME_NAME} -archivePath {ARCHIVEPATH}
xcode9之前
xcodebuild archive -workspace ${TARGET_NAME}.xcworkspace -scheme ${SCHEME_NAME} -configuration ${BUILD_TYPE} -archivePath "${ARCHIVEPATH}/${TARGET_NAME}.xcarchive" CODE_SIGN_IDENTITY=证书 PROVISIONING_PROFILE=描述文件UUID
-
xcodebuild -exportArchive
: 导出ipa包
一般使用:
xcode9+
xcodebuild -exportArchive -archivePath "${ARCHIVEPATH}/${TARGET_NAME}.xcarchive" -exportPath ${EXPORTPATH} -exportOptionsPlist ${EXPORTOPTIONSPLIST}
xcode9之前
xcodebuild -exportArchive -archivePath "${ARCHIVEPATH}/${TARGET_NAME}.xcarchive" -exportPath ${EXPORTPATH} -exportOptionsPlist "${EXPORT_PLIST}" CODE_SIGN_IDENTITY=证书 PROVISIONING_PROFILE=描述文件UUID
参数解释:
- ${TARGET_NAME} 项目对应targets的名字
- ${SCHEME_NAME} 项目对应的Scheme的名字
- ${BUILD_TYPE} 打包类型 Debug,Release 等
- ${archivePath} .xcarchive文件导出目录
- ${EXPORTPATH} 导出.ipa包的目录
- ${EXPORTOPTIONSPLIST} exportOptionsPlist文件所在目录,可判断development, ad-hoc等
从上面xcode9前后的打包命令就可以看出来,还是有点不一样的。xcode9之前有证书和描述文件信息这两个参数。没填xcode会找默认设置。xcode9之后,这两个参数被删除了。将这两个参数的信息放到了ExportOptions.plist这个文件里面。两者之前的差异如下:
xcode9之前的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>method</key>
<string>development</string>
<key>teamID</key>
<string>yourTeamID</string>
</dict>
</plist>
xcode9之后的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>compileBitcode</key>
<true/>
<key>method</key>
<string>development</string>
<key>provisioningProfiles</key>
<dict>
<key>com.xingshulin.abc</key>
<string>abc_dev</string>
<key>com.xingshulin.abc.NotificationServiceExtension</key>
<string>abc-Notification-dev</string>
</dict>
<key>signingCertificate</key>
<string>iPhone Developer</string>
<key>signingStyle</key>
<string>manual</string>
<key>stripSwiftSymbols</key>
<true/>
<key>teamID</key>
<string>yourTeamID</string>
<key>thinning</key>
<string><none></string>
</dict>
</plist>
所以xcode从8+升级到9一般会遇到以下错误。这就是这个文件的变化导致的。我们只需要提供对应的ExportOptions.plist
文件就可以了。
error: exportArchive: "AppName.app" requires a provisioning profile with the Push Notifications and App Groups features.
Error Domain=IDEProvisioningErrorDomain Code=9
"AppName.app" requires a provisioning profile with the Push Notifications and App Groups features." UserInfo={NSLocalizedDescription="AppName.app" requires a provisioning profile with the Push Notifications and App Groups features., NSLocalizedRecoverySuggestion=Add a profile to the "provisioningProfiles" dictionary in your Export Options property list.}
// 或
"Error Domain=IDEProvisioningErrorDomain Code=9 \"\"ios-simple-objc.app\" requires a provisioning profile.\"
UserInfo={NSLocalizedDescription=\"ios-simple-objc.app\" requires a provisioning profile., NSLocalizedRecoverySuggestion=Add a profile to the \"provisioningProfiles\" dictionary in your Export Options property list.}"
下面说说怎么获取这个文件吧
1、可以自己创建一个plist文件,然后按照文件格式填上相关的参数就ok。
2、用xcode手动导出一个包。在导出的文件夹里面就有ExportOptions.plist
这个文件,我拿来用就行了。
大概的目录结构如下
ipa包所在的文件夹
├── DistributionSummary.plist
├── ExportOptions.plist
├── ipa文件
├── Packaging.log
具体的脚本文件:
介绍完了打包的几个命令,就来说说打包的脚本文件吧。
具体内容如下:
##!/bin/sh
TARGET_NAME=hdproject ## 项目名
SCHEME=hdproject_test ## Scheme名
BUILD_TYPE=Debug ## 编译类型
SORCEPATH=$( cd "$( dirname $0 )" & pwd )##当前目录
WORKSPACE=$SORCEPATH/../${TARGET_NAME}.xcworkspace ## workspace名
ARCHIVEPATH=$SORCEPATH/$TARGET_NAME.xcarchive ##xcarchive文件的存放路径
EXPORTPATH=$SORCEPATH/$TARGET_NAME.ipa ## ipa文件的存放路径
EXPORTOPTIONSPLIST=$SORCEPATH/ExportOptions.plist ## ExportOptions.plist文件的存放路径
# 清理缓存
xcodebuild clean -workspace $WORKSPACE -scheme ${SCHEME} -configuration ${BUILD_TYPE}
# 输出关键信息
echo -e " TARGET_NAME : ${TARGET_NAME}"
echo -e " BUILD_TYPE : ${BUILD_TYPE}"
echo -e " SORCEPATH : ${SORCEPATH}"
echo -e " ARCHIVEPATH : ${ARCHIVEPATH}"
echo -e " EXPORTPATH : ${EXPORTPATH}"
echo -e " EXPORTOPTIONSPLIST : ${EXPORTOPTIONSPLIST}"
# 导出archive包
xcodebuild archive -workspace ${WORKSPACE} -scheme ${SCHEME} -archivePath $ARCHIVEPATH
#导出IPA包
xcodebuild -exportArchive -archivePath $ARCHIVEPATH -exportPath ${EXPORTPATH} -exportOptionsPlist ${EXPORTOPTIONSPLIST}
路径信息有疑问的可以结合项目目录结构来件,我的项目目录结构如下
项目根目录
├── ***.xcworkspace
├── ***.xcodeproj
├── build
├── ExportOptions.plist
├── build.sh
其中build.sh
为打包脚本文件,内容如上.
PS:后续还会将这篇文章完善。
1、包括代码管理。
2、ipa的发布。
3、jenkins自动化。