工具: xcode9.1 和 xcodebuild
关于xcodebuild的使用,我们可以在终端中输入
xcodebuild -help
我说说我常用的几个命令:
xcodebuild -list (这个命令可以当前工程的Scheme和Configurations和Targets,这些信息我们下面需要使用)
xcodebuild -workspace xxx.xcworkspace -scheme xxx -configuration xxx -archivePath build/xxx.xcarchive archive build CODE_SIGN_IDENTITY="证书名称名称" PROVISIONING_PROFILE="配置文件名称" PRODUCT_BUNDLE_IDENTIFIER="bundleId"
我的项目是用xcworkspace编译的,所以这样敲命令,如果使用xcodeproj编译的话,自己查看help替换就好. 这个命令如果能正确执行的话,将会在工程目录下新建一个build目录,里面存放一个的是xxx.xcarchive文件
xcodebuild -exportArchive -archivePath build/xxx.xcarchive -exportOptionsPlist ./ExportOptionsPlist1.plist -exportPath ~/Desktop/ipa/xxxx
这个命令可以将xxx.xcarchive文件转成一个ipa文件,
这里说明一下ExportOptionsPlist1.plist这个文件,这个文件需要和脚本一样放在工程的根目录下,由于这个plist文件在Xcode不同的版本有不同的编写规范,这里我分享一个我个人百试不爽的方法:比如我们要打adhoc的一个包,先用xcode的GUI手动打一个adhoc包,这样Xcode就会帮我们生成一个ExportOptionsPlist.plist文件
我们在把这个文件复制一份到项目跟目录,这样就能万无一失了,也就不会出现配置不正确的小问题!哈哈
我的测试工程的目录如下:
sh脚本编写的内容如下:
#author by liyang
#注意:脚本目录和WorkSpace目录在同一个目录
#工程名字(Target名字)
Project_Name="Test1"
#workspace的名字
Workspace_Name="Test1"
#Scheme
Scheme="Test2"
#配置环境,Release或者Debug
Configuration="Release2"
#AdHoc版本的Bundle ID
AdHocBundleID="com.liyang.Test1"
# ADHOC证书名#描述文件
ADHOCCODE_SIGN_IDENTITY="iPhone Distribution: FEI WANG (5C9LZ6Y635)"
ADHOCPROVISIONING_PROFILE_NAME="Test1"
ADHOCOUTNAME="adhoc1"
#脚本命令
#生成xcarchive文件
xcodebuild -workspace $Workspace_Name.xcworkspace -scheme $Scheme -configuration $Configuration -archivePath build/$Project_Name-adhoc.xcarchive clean archive build CODE_SIGN_IDENTITY="${ADHOCCODE_SIGN_IDENTITY}" PROVISIONING_PROFILE="${ADHOCPROVISIONING_PROFILE_NAME}" PRODUCT_BUNDLE_IDENTIFIER="${AdHocBundleID}"
#编译成ipa包
xcodebuild -exportArchive -archivePath build/$Project_Name-adhoc.xcarchive -exportOptionsPlist ./ExportOptionsPlist/ExportOptionsPlist1.plist -exportPath ~/Desktop/ipa/$ADHOCOUTNAME
if [ $? -eq 0 ]
then
echo "打包成功"
rm -rf build
else
echo "打包失败"
fi
脚本中大家可能会问的问题:
1: ADHOCCODE_SIGN_IDENTITY这个是什么,后面的值如果取?
这个是打包时候需要的用到的证书,后面的证书名称可以通过钥匙串->找到项目的证书->显示简介
- ADHOCPROVISIONING_PROFILE_NAME这个是什么?
这个是配置证书的UUID,至于怎么获取,我们在用xcode第一次打包的时候生成了一个ExportOptionsPlist.plist文件,点开这个文件,你就能看到这个名称(以前的Xcode可以通过accounts->选择账号->viewDetails查看的,不过现在不可以了)
- 编写的sh脚本不能执行?
这个问题的话,是因为sh没有权限导致的,我们使用命令chmod 777 xxx.sh
给它最高权限就ok了. 你问为啥啥777? 这是shell方面的知识了,自己Google吧,这里我就不多言了.
4.放个动图演示下? ok啊
---2018年05月07日 在编辑--
现在的脚本新增两个功能
- 可以同时生成所有的马甲包,每当添加一个新的马甲包的时候,只需要在数组中插入一条数据就可
- 可以生成数组中已经存在的指定马甲包
脚本1内容:
#author by liyang
#当添加新的马甲包只需要在4个数组中分别配置对应的数据即可
#Scheme #输出文件夹名称
SchemeArray=(
"a1ios888"
"a3ios888"
"a4ios888"
"a6ios888"
"a7ios888"
"a8ios888"
"a8ios986"
"a8ios988"
"b0ios888"
"c4ios888"
"d3ios888"
"d4ios888"
"a9ios888"
"c4ios998"
)
#AdHoc版本的Bundle ID
AdHocBundleIDArray=(
"com.app.a1ios888"
"com.app.a3ios888"
"com.app.a4ios888"
"com.app.a6ios888"
"com.app.a7ios888"
"com.app.a8ios888"
"com.app.a8ios986"
"com.app.a8ios988"
"com.app.b0ios888"
"com.app.c4ios888"
"com.app.d3ios888"
"com.app.d4ios888"
"com.app.a9ios888"
"com.app.c4ios998"
)
# ADHOC证书名#描述文件
ADHOCCODE_SIGN_IDENTITY="iPhone Distribution: **** (**********)"
ADHOCPROVISIONING_PROFILE_NAME_Array=(
"DistributionProfile_a1ios888"
"DistributionProfile_a3ios888"
"DistributionProfile_a4ios888"
"DistributionProfile_a6ios888"
"DistributionProfile_a7ios888"
"DistributionProfile_a8ios888"
"DistributionProfile_a8ios986"
"DistributionProfile_a8ios988"
"DistributionProfile_b0ios888"
"DistributionProfile_c4ios888"
"DistributionProfile_d3ios888"
"Distribution_d4ios888"
"Distribution_a9ios888"
"DistributionProfile_c4ios998"
)
# OptionsPlist的路径
ADHOCExportOptionsPlistArray=(
./plists/ExportOptions1.plist
./plists/ExportOptions2.plist
./plists/ExportOptions3.plist
./plists/ExportOptions4.plist
./plists/ExportOptions5.plist
./plists/ExportOptions6.plist
./plists/ExportOptions7.plist
./plists/ExportOptions8.plist
./plists/ExportOptions9.plist
./plists/ExportOptions10.plist
./plists/ExportOptions11.plist
./plists/ExportOptions12.plist
./plists/ExportOptions13.plist
./plists/ExportOptions14.plist
)
#脚本命令
for ((i=0;i<${#SchemeArray[*]};i++))
do
xcodebuild -workspace veniceIOS.xcworkspace -scheme ${SchemeArray[$i]} -configuration Release -archivePath build/${SchemeArray[$i]}-adhoc.xcarchive clean archive build CODE_SIGN_IDENTITY="${ADHOCCODE_SIGN_IDENTITY}" PROVISIONING_PROFILE="${ADHOCPROVISIONING_PROFILE_NAME_Array[$i]}" PRODUCT_BUNDLE_IDENTIFIER="${AdHocBundleIDArray[$i]}"
xcodebuild -exportArchive -archivePath build/${SchemeArray[$i]}-adhoc.xcarchive -exportOptionsPlist ${ADHOCExportOptionsPlistArray[$i]} -exportPath ~/Desktop/ipa/${SchemeArray[$i]}
if [ $? -eq 0 ]
then
echo "打包成功: ${SchemeArray[$i]}"
else
echo "打包失败: ${SchemeArray[$i]}"
fi
done
脚本2,生成数组中指定的马甲包
#author by liyang 2018年03月09日 修改以前的脚本,更加方便
#Scheme
Scheme
#配置环境,Release或者Debug
Configuration="Release"
#AdHoc版本的Bundle ID
AdHocBundleID
# ADHOC证书名#描述文件
ADHOCCODE_SIGN_IDENTITY="iPhone Distribution: ******(**********)"
ADHOCPROVISIONING_PROFILE_NAME
echo "请选择需要打包scheme的 序号"
echo "1. a1ios888"
echo "2. a3ios888"
echo "3. a4ios888"
echo "4. a6ios888"
echo "5. a7ios888"
echo "6. a8ios888"
echo "7. a8ios986"
echo "8. a8ios988"
echo "9. b0ios888"
echo "10. c4ios888"
echo "11. d3ios888"
echo "12. d4ios888"
echo "13. a9ios888"
echo "14. c4ios998"
echo "----------------------------"
#读取用户输入并存到内存中去
read parameter
if [ "$parameter" -eq 1 ]
then
echo -e "选择的序号是 1\n将要打的包scheme是 a1ios888"
Scheme="a1ios888"
AdHocBundleID="com.app.a1ios888"
ADHOCPROVISIONING_PROFILE_NAME="DistributionProfile_a1ios888"
ADHOCExportOptionsPlist=./plists/ExportOptions1.plist
elif [ "$parameter" -eq 2 ]
then
echo -e "选择序号 2\n将要打的包scheme是 a3ios888"
Scheme="a3ios888"
AdHocBundleID="com.app.a3ios888"
ADHOCPROVISIONING_PROFILE_NAME="DistributionProfile_a3ios888"
ADHOCExportOptionsPlist=./plists/ExportOptions2.plist
elif [ "$parameter" -eq 3 ]
then
echo -e "选择序号 3\n将要打的包scheme是 a4ios888"
Scheme="a4ios888"
AdHocBundleID="com.app.a4ios888"
ADHOCPROVISIONING_PROFILE_NAME="DistributionProfile_a4ios888"
ADHOCExportOptionsPlist=./plists/ExportOptions3.plist
elif [ "$parameter" -eq 4 ]
then
echo -e "选择序号 4\n将要打的包scheme是 a6ios888"
Scheme="a6ios888"
AdHocBundleID="com.app.a6ios888"
ADHOCPROVISIONING_PROFILE_NAME="DistributionProfile_a6ios888"
ADHOCExportOptionsPlist=./plists/ExportOptions4.plist
elif [ "$parameter" -eq 5 ]
then
echo -e "选择序号 5\n将要打的包scheme是 a7ios888"
Scheme="a7ios888"
AdHocBundleID="com.app.a7ios888"
ADHOCPROVISIONING_PROFILE_NAME="DistributionProfile_a7ios888"
ADHOCExportOptionsPlist=./plists/ExportOptions5.plist
elif [ "$parameter" -eq 6 ]
then
echo -e "选择序号 6\n将要打的包scheme是 a8ios888"
Scheme="a8ios888"
AdHocBundleID="com.app.a8ios888"
ADHOCPROVISIONING_PROFILE_NAME="DistributionProfile_a8ios888"
ADHOCExportOptionsPlist=./plists/ExportOptions6.plist
elif [ "$parameter" -eq 7 ]
then
echo -e "选择序号 7\n将要打的包scheme是 a8ios986"
Scheme="a8ios986"
AdHocBundleID="com.app.a8ios986"
ADHOCPROVISIONING_PROFILE_NAME="DistributionProfile_a8ios986"
ADHOCExportOptionsPlist=./plists/ExportOptions7.plist
elif [ "$parameter" -eq 8 ]
then
echo -e "选择序号 8\n将要打的包scheme是 a8ios988"
Scheme="a8ios988"
AdHocBundleID="com.app.a8ios988"
ADHOCPROVISIONING_PROFILE_NAME="DistributionProfile_a8ios988"
ADHOCExportOptionsPlist=./plists/ExportOptions8.plist
elif [ "$parameter" -eq 9 ]
then
echo -e "选择序号 9\n将要打的包scheme是 b0ios888"
Scheme="b0ios888"
AdHocBundleID="com.app.b0ios888"
ADHOCPROVISIONING_PROFILE_NAME="DistributionProfile_b0ios888"
ADHOCExportOptionsPlist=./plists/ExportOptions9.plist
elif [ "$parameter" -eq 10 ]
then
echo -e "选择序号 10\n将要打的包scheme是 c4ios888"
Scheme="c4ios888"
AdHocBundleID="com.app.c4ios888"
ADHOCPROVISIONING_PROFILE_NAME="DistributionProfile_c4ios888"
ADHOCExportOptionsPlist=./plists/ExportOptions10.plist
elif [ "$parameter" -eq 11 ]
then
echo -e "选择序号 11\n将要打的包scheme是 d3ios888"
Scheme="d3ios888"
AdHocBundleID="com.app.d3ios888"
ADHOCPROVISIONING_PROFILE_NAME="DistributionProfile_d3ios888"
ADHOCExportOptionsPlist=./plists/ExportOptions11.plist
elif [ "$parameter" -eq 12 ]
then
echo -e "选择序号 12\n将要打的包scheme是 d4ios888"
Scheme="d4ios888"
AdHocBundleID="com.app.d4ios888"
ADHOCPROVISIONING_PROFILE_NAME="Distribution_d4ios888"
ADHOCExportOptionsPlist=./plists/ExportOptions12.plist
elif [ "$parameter" -eq 13 ]
then
echo -e "选择序号 13\n将要打的包scheme是 a9ios888"
Scheme="a9ios888"
AdHocBundleID="com.app.a9ios888"
ADHOCPROVISIONING_PROFILE_NAME="Distribution_a9ios888"
ADHOCExportOptionsPlist=./plists/ExportOptions13.plist
elif [ "$parameter" -eq 14 ]
then
echo -e "选择序号 14\n将要打的包scheme是 c4ios998"
Scheme="c4ios998"
AdHocBundleID="com.app.c4ios998"
ADHOCPROVISIONING_PROFILE_NAME="DistributionProfile_c4ios998"
ADHOCExportOptionsPlist=./plists/ExportOptions14.plist
else
echo "序号不正确,请选择正确的序号"
exit 1
fi
#休息1秒钟,检查打印的信息是否有误
sleep 1
#脚本命令
xcodebuild -workspace veniceIOS.xcworkspace -scheme $Scheme -configuration $Configuration -archivePath build/$Scheme-adhoc.xcarchive clean archive build CODE_SIGN_IDENTITY="${ADHOCCODE_SIGN_IDENTITY}" PROVISIONING_PROFILE="${ADHOCPROVISIONING_PROFILE_NAME}" PRODUCT_BUNDLE_IDENTIFIER="${AdHocBundleID}"
xcodebuild -exportArchive -archivePath build/$Scheme-adhoc.xcarchive -exportOptionsPlist ${ADHOCExportOptionsPlist} -exportPath ~/Desktop/ipa2/$Scheme
if [ $? -eq 0 ]
then
echo "打包成功: ${Scheme}"
rm -r build/$Scheme-adhoc.xcarchive
else
echo "打包失败: ${Scheme}"
fi
两个脚本,维护简单,功能足够目前使用.