實(shí)現(xiàn)的功能
- 自動識別單項(xiàng)目工程和Workspace工程
- 指定版本號打包
- 動態(tài)指定簽名(可能會失斄龆谩)
1. 替換Xcode中的PackageApplication
做這個(gè)動作主要是因?yàn)樵诰幾g的過程中很容易出現(xiàn)ResourceRules.plist
導(dǎo)致的錯誤搂橙,所以干脆從源頭避免這個(gè)錯誤的產(chǎn)生毒涧。
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
其實(shí)很簡單,只要在原始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});
}
即可酌予,相信大家看到腳本后就會發(fā)現(xiàn)是怎么回事。
要注意代碼的縮進(jìn)搂捧,不正確可能導(dǎo)致找不到PackageApplication的錯誤。
重要說明:
如果是要打包AppStore版本懂缕,還要將這個(gè)改回來允跑,否則蘋果會認(rèn)為這個(gè)ipa包不合法。
2. 定義一些變量,用于默認(rèn)設(shè)置聋丝,具體情況還得看項(xiàng)目
PlistBuddy=/usr/libexec/PlistBuddy
ProjectName="YourName"
ProjectRootDir=$(PWD -P)
Configuration="Release"
# build輸出目錄
TargetBuildDir="$ProjectRootDir/build"
# 工程的Info.plist文件路徑索烹,可能在多個(gè)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. 參數(shù)解析
執(zhí)行腳本時(shí)弱睦,可以指定參數(shù)百姓,目前支持2個(gè)參數(shù),版本號和Configuration
# 參數(shù)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
# 參數(shù)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
# 拷貝靜態(tài)庫到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
選項(xiàng)去掉火惊,然后你要正確設(shè)置工程中的證書和描述文件求类,否則打出來的包會安裝不上。
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包應(yīng)該就在你設(shè)置的目錄下面了仓技!
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: 單個(gè)工程, 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 "目錄不是一個(gè)有效的工程目錄"
exit 1
}
# 校驗(yàn)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對應(yīng)的info.plist文件名稱, 參數(shù)為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, 參數(shù)為描述文件路徑
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
}
# 設(shè)置版本號, 參數(shù)1為version, 參數(shù)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
如果有任何問題,請聯(lián)系我!