5分鐘掌握iOS打包腳本

實現的功能

  1. 自動識別單項目工程和Workspace工程
  2. 指定版本號打包
  3. 動態指定簽名(可能會失敗)

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_IDENTITYPROVISIONING_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

如果有任何問題,請聯系我!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 本文始發于我的博文詳解Shell腳本實現iOS自動化編譯打包提交,現轉發至此。 目錄 前言 Shell腳本涉及的工...
    zackzheng閱讀 44,546評論 95 173
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,845評論 25 708
  • 前言 眾所周知,現在App的競爭已經到了用戶體驗為王,質量為上的白熱化階段。用戶們都是很挑剔的。如果一個公司的推廣...
    閑云清煙閱讀 2,532評論 1 4
  • 前言 眾所周知,現在App的競爭已經到了用戶體驗為王,質量為上的白熱化階段。用戶們都是很挑剔的。如果一個公司的推廣...
    偏偏就是禰閱讀 9,040評論 34 59
  • 我問佛:如果遇到了可以愛的人,卻又怕不能把握該怎么辦? 佛曰:留人間多少愛,迎浮世千重變,和有情人,做快樂事,別問...
    石川河女神閱讀 409評論 0 1