实现的功能
- 自动识别单项目工程和Workspace工程
- 指定版本号打包
- 动态指定签名(可能会失败)
1. 替换Xcode中的PackageApplication
做这个动作主要是因为在编译的过程中很容易出现ResourceRules.plist
导致的错误,所以干脆从源头避免这个错误的产生。
PackageApplicationPath="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/PackageApplication"
if [[ ! -f "${PackageApplicationPath}_backup" ]]; then
cp $PackageApplicationPath ${PackageApplicationPath}_backup
cp PackageApplication $PackageApplicationPath
fi
其实很简单,只要在原始PackageApplication
中搜索@codesign_args
,找到下面这段脚本
my @codesign_args = ("/usr/bin/codesign", "--force", "--preserve-metadata=identifier,entitlements,resource-rules",
"--sign", $opt{sign},
"--resource-rules=$destApp/ResourceRules.plist");
替换成
my @codesign_args;
if (-e '$destApp/ResourceRules.plist') { # If ResourceRules.plist exists, include it in codesign arguments, for backwards compatability
@codesign_args = ("/usr/bin/codesign", "--force", "--preserve-metadata=identifier,entitlements,resource-rules",
"--sign", $opt{sign},
"--resource-rules=$destApp/ResourceRules.plist");
} else { # If ResourceRules.plist isn't found, don't include it in the codesign arguments
@codesign_args = ("/usr/bin/codesign", "--force", "--preserve-metadata=identifier,entitlements",
"--sign", $opt{sign});
}
即可,相信大家看到脚本后就会发现是怎么回事。
要注意代码的缩进,不正确可能导致找不到PackageApplication的错误。
重要说明:
如果是要打包AppStore版本,还要将这个改回来,否则苹果会认为这个ipa包不合法。
2. 定义一些变量,用于默认设置,具体情况还得看项目
PlistBuddy=/usr/libexec/PlistBuddy
ProjectName="YourName"
ProjectRootDir=$(PWD -P)
Configuration="Release"
# build输出目录
TargetBuildDir="$ProjectRootDir/build"
# 工程的Info.plist文件路径,可能在多个targets中需要修改
PlistFilePath="$ProjectRootDir/$ProjectName/Info.plist"
VersionString=$($PlistBuddy -c "Print CFBundleVersion" $PlistFilePath)
ShortVersionString=$($PlistBuddy -c "Print CFBundleShortVersionString" $PlistFilePath)
# 打包的ipa路径
IPAFilePath="$ProjectRootDir/ipa"
# ipa文件名
IPAFileName="$ProjectName-$VersionString.ipa"
# 描述文件路径,用于签名
ProvisionProfileFilePath="$IPAFilePath/xxx.mobileprovision"
3. 参数解析
执行脚本时,可以指定参数,目前支持2个参数,版本号和Configuration
# 参数1:版本号
if [ x$1 != x ]; then
echo_tip "ready to update version from $VersionString to $1"
echo_tip "$PlistBuddy -c \"Set:CFBundleVersion $1\" \"$PlistFilePath\""
$PlistBuddy -c "Set:CFBundleVersion $1" "$PlistFilePath"
ShortVersionString=$(echo $1 | cut -d "." -f1-3)
$PlistBuddy -c "Set:CFBundleShortVersionString $ShortVersionString" "$PlistFilePath"
VersionString=$($PlistBuddy -c "Print CFBundleVersion" $PlistFilePath)
ShortVersionString=$($PlistBuddy -c "Print CFBundleShortVersionString" $PlistFilePath)
echo_tip "after update, version = $VersionString, short version string = $ShortVersionString"
IPAFileName="$ProjectName-$VersionString.ipa"
fi
# 参数2:编译配置
if [ x$2 != x ]; then
echo_tip "set configuration = $2"
Configuration=$2
fi
4. 解析指定的描述文件
TempPlistFilePath="$IPAFilePath/temp.plist"
if [[ -f "$TempPlistFilePath" ]]; then
rm $TempPlistFilePath
fi
security cms -D -i $ProvisionProfileFilePath > $TempPlistFilePath
IdentityString=`/usr/libexec/PlistBuddy -c 'Print DeveloperCertificates:0' $TempPlistFilePath | \
openssl x509 -subject -inform der|head -n 1`
CodeSignIdentity=`echo "$IdentityString" | cut -d "/" -f3 | cut -d "=" -f2`
UUID=`/usr/libexec/PlistBuddy -c "Print UUID" $TempPlistFilePath`
rm $TempPlistFilePath
这里感谢 @YoXung 提供的方法
5. 清理工程
/usr/bin/xcodebuild clean -configuration $Configuration
6. 编译工程
mkdir -p $TargetBuildDir
# 拷贝静态库到Build目录:
for path in `find "$ProjectRootDir" -name "*.a"`; do
echo_tip " cp $path $TargetBuildDir"
cp $path $TargetBuildDir
done
if [[ -d "$ProjectRootDir/$ProjectName.xcworkspace" ]]; then
/usr/bin/xcodebuild \
-workspace $ProjectName.xcworkspace \
-scheme $ProjectName \
-configuration $Configuration \
-sdk iphoneos \
OBJROOT=$TargetBuildDir \
TARGET_BUILD_DIR=$TargetBuildDir \
LIBRARY_SEARCH_PATHS=$TargetBuildDir \
CODE_SIGN_IDENTITY="$CodeSignIdentity" \
PROVISIONING_PROFILE="$UUID"
else
/usr/bin/xcodebuild \
-target $ProjectName \
-configuration $Configuration \
-sdk iphoneos \
OBJROOT=$TargetBuildDir \
TARGET_BUILD_DIR=$TargetBuildDir \
LIBRARY_SEARCH_PATHS=$TargetBuildDir \
CODE_SIGN_IDENTITY="$CodeSignIdentity" \
PROVISIONING_PROFILE="$UUID"
fi
ReturnCode=$(echo $?)
if ([[ $ReturnCode == "0" ]] && [[ -d "$TargetBuildDir/$ProjectName.app" ]]); then
echo_tip "编译成功!"
else
echo_error "编译失败,请修改后重试!"
exit 1
fi
这里需要注意一下:
如果脚本提示下面错误
Check dependencies
Code Sign error: No codesigning identities found: No codesigning identities
(i.e. certificate and private key pairs) that match the provisioning profile
specified in your build settings (“xxxxx”) were found.
则需要把编译命令中的CODE_SIGN_IDENTITY
和PROVISIONING_PROFILE
选项去掉,然后你要正确设置工程中的证书和描述文件,否则打出来的包会安装不上。
7. 打包
# 删除已有的ipa包
if [[ -f "$IPAFilePath/$IPAFileName" ]]; then
rm -f "$IPAFilePath/$IPAFileName"
fi
/usr/bin/xcrun -sdk iphoneos \
PackageApplication \
-v $TargetBuildDir/$ProjectName.app \
-o $IPAFilePath/$IPAFileName \
--sign "$CodeSignIdentity" \
--embed "$ProvisionProfileFilePath"
if [[ -f "$IPAFilePath/$IPAFileName" ]]; then
echo_tip "打包成功!"
else
echo_error "打包失败,请修改后重试!"
exit 1
fi
如果一切顺利,ipa包应该就在你设置的目录下面了!
8. 最后附上完成的脚本:
#!/bin/sh
PlistBuddy=/usr/libexec/PlistBuddy
function echo_tip() {
echo "\033[36;49m$1\033[0m"
}
function echo_error() {
echo "\033[31;49m$1\033[0m"
}
# if [[ "$1"x != "x" ]]; then
# project_root_path=$1
# fi
project_root_path=$(PWD -P)
project_name=""
project_type=""
scheme=""
info_plist_file_path=""
ipa_file_name=""
code_sign=""
uuid=""
ipa_file_path="$project_root_path/ipa"
configuration="Release"
provision_profile_path="$ipa_file_path/ZXIPTV_InHouse.mobileprovision"
build_path="$project_root_path/build"
# 获取工程名称 1: 单个工程, 2: 工作空间
function get_project_info() {
cd $1
workspace=`ls . | grep "xcworkspace$"`
if [[ x"$workspace" != "x" ]]; then
project_name=`echo $workspace | cut -d "." -f1`
project_type="2"
return "0"
else
project=`ls . | grep "xcodeproj$"`
if [[ x"$project" != "x" ]]; then
project_name=`echo $project | cut -d "." -f1`
project_type="1"
return "0"
fi
fi
echo_error "目录不是一个有效的工程目录"
exit 1
}
# 校验scheme是否正确
function validate_scheme() {
info=`xcodebuild -list`
schemes=${info#*Schemes:}
for item in $schemes; do
if [[ "$1"x == "$item"x ]]; then
return "0"
fi
done
echo "$1 is not in [$schemes]"
exit 1
}
function validate_configuration() {
info=`xcodebuild -list`
configurations=${info#*Configurations:}
configurations=${configurations%If*}
for item in $configurations; do
if [[ "$1"x == "$item"x ]]; then
return "0"
fi
done
exit 1
}
# 获取指定target对应的info.plist文件名称, 参数为scheme值
function get_info_plist_file_path() {
settings=`xcodebuild -showBuildSettings -scheme $1`
str1=${settings#*PRODUCT_SETTINGS_PATH = }
path=${str1%%.plist*}
info_plist_file_path=$path".plist"
}
# 解析provision profile, 参数为描述文件路径
function parse_provision_profile() {
temp_plist_path="$ipa_file_path/temp.plist"
if [[ -f "$temp_plist_path" ]]; then
rm $temp_plist_path
fi
security cms -D -i $1 > $temp_plist_path
code_sign_identity=`$PlistBuddy -c 'Print DeveloperCertificates:0' $temp_plist_path | \
openssl x509 -subject -inform der|head -n 1`
code_sign=`echo "$code_sign_identity" | cut -d "/" -f3 | cut -d "=" -f2`
uuid=`$PlistBuddy -c "Print UUID" $temp_plist_path`
rm $temp_plist_path
if [[ $code_sign == "" || $uuid == "" ]]; then
echo_error "无法解析描述文件[$provision_profile_path]"
exit 1
fi
}
# 设置版本号, 参数1为version, 参数2为plist文件路径
function update_version() {
old_version=$($PlistBuddy -c "Print CFBundleVersion" $2)
old_short_version=$($PlistBuddy -c "Print CFBundleShortVersionString" $2)
echo_tip "before update, version = $old_version, short version = $old_short_version"
short_version=$(echo $1 | cut -d "." -f 1-3)
$PlistBuddy -c "Set:CFBundleVersion $1" "$2"
$PlistBuddy -c "Set:CFBundleShortVersionString $short_version" "$2"
new_version=$($PlistBuddy -c "Print CFBundleVersion" $2)
new_short_version=$($PlistBuddy -c "Print CFBundleShortVersionString" $2)
echo_tip "after update, version = $new_version, short version = $new_short_version"
return "0"
}
#=====================================================================================
get_project_info "$project_root_path"
parse_provision_profile "$provision_profile_path"
scheme=$project_name
validate_scheme "$scheme"
validate_configuration "$configuration"
get_info_plist_file_path "$scheme"
version=`$PlistBuddy -c "Print CFBundleVersion" "$info_plist_file_path"`
if [[ "$1"x != "x" ]]; then
version=$1
update_version "$version" "$info_plist_file_path"
fi
ipa_file_name="$project_name-$version.ipa"
echo_tip "project_root_path = $project_root_path"
echo_tip "project_name = $project_name"
echo_tip "scheme = $scheme"
echo_tip "info_plist_file_path = $info_plist_file_path"
echo_tip "version = $version"
echo_tip "ipa_file_name = $ipa_file_name"
echo_tip "ipa_file_path = $ipa_file_path"
echo_tip "code_sign = $code_sign"
echo_tip "uuid = $uuid"
#=====================================================================================
function prepare_build() {
rm -rf $build_path
mkdir -p $build_path
mkdir -p $ipa_file_path
for path in `find "$project_root_path" -name "*.a"`; do
cp $path $build_path
done
}
function build() {
if [[ "$project_type" == "1" ]]; then
/usr/bin/xcodebuild \
-target $scheme \
-configuration $configuration -sdk iphoneos \
OBJROOT=$build_path TARGET_BUILD_DIR=$build_path LIBRARY_SEARCH_PATHS=$build_path \
CODE_SIGN_IDENTITY="$code_sign" PROVISIONING_PROFILE="$uuid"
else
/usr/bin/xcodebuild \
-workspace $project_name.xcworkspace -scheme $scheme \
-configuration $configuration -sdk iphoneos \
OBJROOT=$build_path TARGET_BUILD_DIR=$build_path LIBRARY_SEARCH_PATHS=$build_path \
CODE_SIGN_IDENTITY="$code_sign" PROVISIONING_PROFILE="$uuid"
fi
ReturnCode=$(echo $?)
if ([[ $ReturnCode == "0" ]] && [[ -d "$build_path/$project_name.app" ]]); then
echo_tip "编译成功!"
else
echo_error "编译失败,请修改后重试!"
exit 1
fi
}
function package() {
# 删除旧的ipa文件
if [[ -f "$ipa_file_path/$ipa_file_name" ]]; then
rm -f "$ipa_file_path/$ipa_file_name"
fi
/usr/bin/xcrun -sdk iphoneos \
PackageApplication \
-v $build_path/$project_name.app \
-o $ipa_file_path/$ipa_file_name \
--sign "$code_sign" \
--embed "$provision_profile_path"
rm -rf $TargetBuildDir
if [[ -f "$ipa_file_path/$ipa_file_name" ]]; then
echo_tip "打包成功!"
else
echo_error "打包失败,请修改后重试!"
exit 1
fi
}
prepare_build
build
package
如果有任何问题,请联系我!