如何使用Shell脚本编译生成Archive文件并导出签名IPA文件。

如何使用Shell脚本编译生成Archive文件并导出签名IPA文件。

1. build 脚本说明

本Shell脚本用于build,XCode的workspace源代码工程,并导出成可重签名的IPA文件,用于分发测试安装文件和用于提交appstore审核的文件。实现辅助产品开发,测试的配置管理工作。

使用本脚本需要以下环境
- Mac OS 10.9+
- XCode 6.0 or later and command line tools
- 用于WorkSpace工程,例如使用了Cocoapods依赖库管理的工程
- *配置好开发证书和ad ho 证书(for Debug)和(for Release)
- 源代码工程中配置好Scheme名字和build
- 对Debug配置和Release配置设置好证书名称
- 确认在Xcode UI界面中能够完全build,并通过环境生成Archive和IPA,并正确签名。
- 在workspace文件夹下建立build文件夹


2. 复制脚本文件到WorkSpace目录

3. 打开终端工具,并进入workspace目录

4. 给脚本执行权限

chmod 777 ./build_one_target.sh

5. 修改脚本参数,源代码里已经有注释

需要修改的
workspace name
provisioningProfile
scheme name
build_config

代码块

代码块语法遵循标准markdown代码

#!/bin/bash

# init build configuration
# archive and ipa output path
build_path="./build"

# workspace name
build_workspace="workspacename.xcworkspace" 


# project name and path
project_path=$(pwd)
project_name=$(ls | grep xcodeproj | awk -F.xcodeproj ‘{print $1}‘)

# provisiong profile name
provisioningProfile=‘"XC Ad Hoc: com.xxxxxx.yyyyyy"‘

# timestamp for ouput file name
timeStamp="$(date +"%Y%m%d_%H%M%S")"

echo "$project_path/$build_workspace"
if [ ! -d "$project_path/$build_workspace" ]; then
    echo  "Error!Current path is not a xcode workspace.Please check, or do not use -w option."
    exit 2
fi 

# get the info.plist

app_infoplist_path=${project_path}/${project_name}/${project_name}-AppStore-Info.plist
echo ${app_infoplist_path}

# get the main version
bundleShortVersion=$(/usr/libexec/PlistBuddy -c "print CFBundleShortVersionString" "${app_infoplist_path}")

# get the build version
bundleVersion=$(/usr/libexec/PlistBuddy -c "print CFBundleVersion" "${app_infoplist_path}")

# get the svn revision
svn_revision=$(svn info |grep Revision: |awk   ‘{print $2}‘)

#`svnversion -c |sed ‘s/^.*://‘ |sed ‘s/[A-Z]*$//‘`
#svn info |grep Revision: |awk   ‘{print $2}‘


#workspace_name=‘*.xcworkspace‘
#ls $project_path/$build_workspace &>/dev/null
#rtnValue=$?
#if [ !$rtnValue = 0 ];then
#   #build_workspace=$(echo $(basename $project_path/$workspace_name))
#       echo  "Error!Current path is not a xcode workspace.Please check, or do not use -w option."
#   exit 2
#fi


# scheme name
build_scheme="schemename" 

# buidl config. the default is Debug|Release
build_config="Release"

# clean build
clean_cmd=‘xcodebuild‘
clean_cmd=${clean_cmd}‘ clean -workspace ‘${build_workspace}‘ -scheme ‘${build_scheme}‘ -configuration ‘${build_config}
$clean_cmd >  $build_path/clean_qa.txt || exit

# build & archive, generate the archive file
archive_name="targetname_QA_${timeStamp}.xcarchive"
archive_path="./build/"$archive_name 

build_cmd=‘xcodebuild‘
build_cmd=${build_cmd}‘ -workspace ‘${build_workspace}‘ -scheme ‘${build_scheme}‘ -destination generic/platform=iOS archive -configuration ‘${build_config}‘ ONLY_ACTIVE_ARCH=NO -archivePath ‘${archive_path}
echo "** Archiving QA ** to the ${archive_path}"
echo ${build_cmd}
$build_cmd > ./build/build_archive_qa.log || exit

if [ ! -d "${archive_path}" ]; then
    echo  "** Error! ARCHIVE QA FAILED ** Please check ./build/build_archive_qa.log."
    exit 2
else
    echo "** ARCHIVE QA SUCCEEDED ** to the ${archive_path}"
fi 

# export to ipa with QA server
ipa_name="targetname_QA_adhoc_v${bundleShortVersion}_b${bundleVersion}_rev${svn_revision}_t${timeStamp}.ipa"
ipa_path="./build/"$ipa_name 

ipa_cmd=‘xcodebuild‘
ipa_cmd=${ipa_cmd}‘ -exportArchive -exportFormat ipa -archivePath ‘${archive_path}‘ -exportPath ‘${ipa_path}‘ -exportProvisioningProfile ‘${provisioningProfile}

echo "** Exporting QA ** to the ${ipa_path}"
echo ${ipa_cmd}
eval ${ipa_cmd} > ./build/export_ipa_qa.log || exit

if [ ! -f "${ipa_path}" ]; then
    echo  "** Error! Export IPA QA FAILED ** Please check ./build/export_ipa_qa.log."
    exit 2
else
    echo "** Export IPA QA SUCCEEDED ** to the ${ipa_path}"
fi 

6. 执行脚本

./build_one_target.sh

大一些的工程,大约3-5分钟完成。
完成之后,在workspace目录下的build目录下,会有如下两个主要文件。
targetname_QA_20150420_094731.xcarchive
targetname_QA_adhoc_v2.1.1_b44_rev7849_t20150420_094731.ipa

xcarchive文件很重要,可以用于不同证书签名的ipa文件
ipa文件使用的是adhoc 证书导出,用于真机测试分发。证书名称在源代码中配置。根据不同的产品bundle id,做相应修改,这里的名称和证书,仅做演示参考,不具有任何商业意义。

目录

[TOC]来生成目录:

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。