Xcode命令行脚本打包

Xcode命令行脚本打包

官方文档

准备

  • 证书,配置文件无误
  • 项目编译没有问题

命令行步骤

  1. clean,保证不会出现些未知的错误
xcodebuild clean -workspace   -scheme  -configuration 

// 实际操作:
xcodebuild clean -workspace /Users/wuzhihua/Desktop/Project/hiveconsumer/HiveConsumer/HiveConsumer.xcworkspace -scheme HiveConsumer -configuration Release

:项目.xcworkspace的绝对路径

: 项目App的scheme (非Extension App)

: 编译环境,Debug Or Release

2. archive

xcodebuild archive -workspace  -scheme  -archivePath 

// 实际操作:
xcodebuild archive -workspace /Users/wuzhihua/Desktop/Project/hiveconsumer/HiveConsumer/HiveConsumer.xcworkspace -scheme HiveConsumer -archivePath /Users/wuzhihua/Desktop/App/archive/hiveconsumer.xcarchive

: 导出achive文件路径

  1. 配置导包
xcodebuild -exportArchive -archivePath  -exportPath  -exportOptionsPlist 

// 实际操作:
xcodebuild -exportArchive -archivePath /Users/wuzhihua/Desktop/App/archive/hiveconsumer.xcarchive -exportPath /Users/wuzhihua/Desktop/App/ipa/ -exportOptionsPlist /Users/wuzhihua/Desktop/App/fcapp_config.plist

: 操作2中的achive文件路径

: ipa文件导出路径

:导包配置plist文件(需手动创建)

exportOptionsPlist配置

exportOptionsPlist配置文件为使用xcode导包的配置项

Xcode命令行脚本打包_第1张图片
exportOptionsPlist配置

provisioningProfiles: Dictionary类型,App的bundle ID作为key,mobileprovision的文件名作为value,注意Extension APP的配置也要包括

method: String类型,决定导出包的类型,有 app-store, ad-hoc, package, enterprise, development, developer-id, and mac-application

teamID: String类型,开发者ID,可在keychain或者登录开发者网站�中查看

teamID

compileBitcode: Bool类型,必须与项目中的bitcode值一致,默认true

注意其他字段配置都有默认值,详情配置说明可通过官方文档或终端命令xcodebuild -help 查看

测试打包出现的问题

1.未配置compileBitcode字段的值,打包失败
compileBitcode字段的值必须与项目中Build Setting中的bitcode的值一致
2.测试通过ssh远程连接iMac终端执行脚本打包,出现code sign签名问题
远程连接没有keychain权限,打包前需执行以下命令

// 在Mac Sierra中在Keychains下面是没有Login.keychain的,新的名字被改成了Login.keychain-db.所以需要做一个软连接
ln -s ~/Library/Keychains/login.keychain-db ~/Library/Keychains/login.keychain
// 解锁权限
security unlock -p  /Users//Library/Keychains/login.keychain

若未使用Cocoapods (workspace)

将操作1和操作2中,-workspace更改成-project为项目中.xcodeproj绝对路径

脚本

#!/bin/bash
#Use:命令行进入目录直接执行sh APP_BUILD.sh即可完成打包安装

 echo "~~~~~~~~~~~~~~~~开始执行脚本~~~~~~~~~~~~~~~~"

 #-------------------------脚本配置信息--------------------------------------------
 #工程目录
 #BASE_PROJECT=$(dirname $1)
 BASE_PROJECT=$1

 #工程名称
 PROJECT_NAME="HiveConsumer"

 #编译模式 工程默认有 Debug Release
 CONFIGURATION_TARGET=Debug

 #编译路径
 BUILDPATH=~/Desktop/
 #导出ipa 所需plist
 ADHOCExportOptionsPlist=$(dirname ${BASE_PROJECT})/config/fcapp_config.plist

 #archivePath
 ARCHIVEPATH=${BUILDPATH}/App/hiveconsumer/${PROJECT_NAME}_$(date +%F-%T)/${PROJECT_NAME}.xcarchive
 IPAPATH=${BUILDPATH}/App/hiveconsumer/ipa_$(date +%F-%T)/
 #---------------------------------------------------------------------------------
 if [ ! -d ${BASE_PROJECT}/${PROJECT_NAME} ]; then
        echo ${BASE_PROJECT}/${PROJECT_NAME}"不存在!"
        exit 1
 else
        echo "工程目录--->>"${BASE_PROJECT}
        cd ${BASE_PROJECT}/${PROJECT_NAME}
 fi

 echo "~~~~~~~~~~~~~~~~开始清理~~~~~~~~~~~~~~~~~~~"
 # 清理 避免出现一些莫名的错误
 xcodebuild clean -workspace ${PROJECT_NAME}.xcworkspace -scheme ${PROJECT_NAME} -configuration ${CONFIGURATION_TARGET}

 echo "~~~~~~~~~~~~~~~~开始解锁~~~~~~~~~~~~~~~~~~~"
 #执行解锁命令
 ln -s ~/Library/Keychains/login.keychain-db ~/Library/Keychains/login.keychain
 security unlock -p  /Users//Library/Keychains/login.keychain

 echo "~~~~~~~~~~~~~~~~开始编译~~~~~~~~~~~~~~~~~~~"
 #开始构建
 #xcodebuild archive -workspace ${PROJECT_NAME}.xcworkspace \
 #-scheme ${TARGET_NAME} \
 #-archivePath ${ARCHIVEPATH} \

 xcodebuild archive -workspace ${PROJECT_NAME}.xcworkspace -scheme ${PROJECT_NAME} -archivePath ${ARCHIVEPATH} -configuration ${CONFIGURATION_TARGET}

 echo "~~~~~~~~~~~~~~~~检查是否编译成功~~~~~~~~~~~~~~~~~~~"
 # xcarchive 实际是一个文件夹不是一个文件,使用 -d 判断
 if [ -d "$ARCHIVEPATH" ]
 then
        echo "编译成功......"
 else
        echo "编译失败......"
        #rm -rf ${ARCHIVEPATH}
 exit 1
 fi
 endTime=`date +%s`
 ArchiveTime="构建编译时间$[ endTime - beginTime ]秒"

 echo "~~~~~~~~~~~~~~~~导出ipa~~~~~~~~~~~~~~~~~~~"

 beginTime=`date +%s`

 xcodebuild -exportArchive -archivePath ${ARCHIVEPATH} -exportPath ${IPAPATH} -exportOptionsPlist ${ADHOCExportOptionsPlist}

 echo "~~~~~~~~~~~~~~~~检查是否成功导出  ipa~~~~~~~~~~~~~~~~~~~"
 IPAPATH=${IPAPATH}/${PROJECT_NAME}.ipa
 if [ -f "$IPAPATH" ]
 then
         echo "导出ipa成功......"

 else
         echo "导出ipa失败......"
 # 结束时间
 endTime=`date +%s`
        echo "$ArchiveTime"
        echo "导出ipa时间$[ endTime - beginTime ]秒"
 exit 1
 fi

 echo "~~~~~~~~~~~~~~~~安装ipa到手机~~~~~~~~~~~~~~~~~~~"
 echo "~~~~~~~~~~~~~~~手机UDID:$(/usr/local/bin/idevice_id -l)"

/usr/local/bin/ideviceinstaller -i ${IPAPATH}

你可能感兴趣的:(Xcode命令行脚本打包)