美文网首页iOS开发iOS开发笔录
5分钟掌握iOS打包脚本

5分钟掌握iOS打包脚本

作者: sytuzhouyong | 来源:发表于2016-05-27 15:02 被阅读1134次

实现的功能

  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


如果有任何问题,请联系我!

相关文章

  • Xcode自动打包

    [iOS]从零开始写个自动打包IPA脚本 【iOS打ipa包】:使用终端实现自动打包 iOS自动打包并发布脚本

  • iOS自动打包ipa(shell脚本)

    系列 iOS自动打包ipa(shell脚本)iOS自动打包ipa(Python脚本) 安装xctool shell...

  • iOS自动打包ipa(Python脚本)

    系列 iOS自动打包ipa(shell脚本)iOS自动打包ipa(Python脚本) 安装Python库 Pyth...

  • iOS脚本一键打包

    ios打包脚本的使用 相关链接: 1. IOS工程自动打包并发布脚本的实现。 2. Xcode中Command L...

  • 2019-02-15

    ios打包时签名失败 ios打包时证书和provisioningprofile都已经切换更新,打包脚本中指定tea...

  • iOS自动打包脚本

    iOS自动打包脚本 iOS自动打包脚本,并实现图片素材、文字资源、部分代码的替换和重签名,基于python实现。 ...

  • ios-面试-脚本打包原理

    脚本打包,自然是为了方便打包,便于ios开发不用手动打包,便于非ios开发能去打包! 打包过程 Xcode打包的过...

  • iOS面试题:脚本打包

    脚本打包,自然是为了方便打包,便于ios开发不用手动打包,便于非ios开发能去打包! 打包过程 Xcode打包的过...

  • iOS开发知识体系之《脚本自动化打包--xcodebuild》

    iOS脚本自动化打包方案--xcodebuild 本文主要xcodebuild脚本自动化打包并上传到蒲公英或者Ap...

  • App itunes connect 提交审核"UIR

    解决方案 xcodebuild 来自iOS自动打包并发布脚本

网友评论

  • Fatm:你好,请问bundleId可以通过脚本修改吗,我尝试通过脚本去修改plist里面的bundleId然后打包失败了,报错,xcode里面的bundleId还没被改过来(这个报错有点绕,你试试就能看到了),有其它的实现方法吗?
    Fatm:@sytuzhouyong 通过修改plist上面的bundleId是不能打包的,因为xcode内部有个值把旧的存起来了
    sytuzhouyong:@Fatm 兄弟,我试过了是可以的

    PlistBuddy=/usr/libexec/PlistBuddy
    $PlistBuddy -c "Set:CFBundleIdentifier com.xing.bb" "$PlistFilePath"

    不知你的脚本怎么写的
    sytuzhouyong:@Fatm 理论上讲是可以的,我晚上回去试试,不好意思,最近没上
  • 78e38af561ef:感谢分享,这几天一直在尝试将项目部署到travis,自动测试,打包然后用fir.im 分发测试。
    一直卡在打包,xcrun签名时出现各种各样的错误,解决一个又冒出来一个。
    现在完全卡在 error: Failed to read entitlements from ‘xxx.app’
    先给兄弟留个言,再细读 :blush:
    sytuzhouyong:@YasenDylan 你这个流程网上应该有很多教程,不妨找一找,我看你这个错误貌似和resource rules有关,可以查一查相关资料
  • ac2c5e86bd8c:没试过
    sytuzhouyong:@最美不过三月天 可以先记录下,用的时候能找到就可以了!😄

本文标题:5分钟掌握iOS打包脚本

本文链接:https://www.haomeiwen.com/subject/npdwrttx.html