使用xcodebuild打包 导出ipa 上传蒲公英或者提交AppStore

  • xcodebuild主要是用来编译,打包成Archive和导出ipa包。
  • 官方文档 Workspace、Project、Scheme、Target的区别
  • altool提交到App Store使用
  • altool使用官方文档第38页
  • altool 这个工具实际上是ApplicationLoader,打开Xcode-左上角Xcode-Open Developer Tool-Application Loader
  • altool的路径是:
/Applications/Xcode.app/Contents/Applications/Application\ Loader.app/Contents/Frameworks/ITunesSoftwareService.framework/Support/altool
  • 使用时如报如下错误
altool[] *** Error: 
Exception while launching iTunesTransporter: Transporter not found at path: /usr/local/itms/bin/iTMSTransporter. 
You should reinstall the application.
  • 建立软链
ln -s /Applications/Xcode.app/Contents/Applications/Application\ Loader.app/Contents/itms /usr/local/itms
  • 如果想要在脚本中修改plist文件的内容可以使用PlistBuddy,PlistBuddy是Mac里一个用于命令行下读写plist文件的工具,在/usr/libexec/下。可以通过它读取或修改plist文件的内容。
#!/bin/sh
# PlistBuddy程序的绝对路径
PlistBuddyPath=/usr/libexec/PlistBuddy
appInfoPlistPath="/Volumes/SourceCode/showstart_ios/ShowStart_3.0/Info.plist"
bundleShortVersion=$($PlistBuddyPath -c "print CFBundleShortVersionString" ${appInfoPlistPath})
bundleVersion=$($PlistBuddyPath  -c "print CFBundleVersion" ${appInfoPlistPath})
echo "$bundleShortVersion"
echo "$bundleVersion"

buildNumber="3.69"

bundleVersion=$($PlistBuddyPath  -c "Set :CFBundleVersion $buildNumber" ${appInfoPlistPath})
bundleVersion=$($PlistBuddyPath  -c "print CFBundleVersion" ${appInfoPlistPath})
echo "$bundleVersion"

开始打包

  • 百度了一下大多数文章使用xcodeuildxcrun编译导出ipa
xcodebuild -workspace XXX -scheme XXX -configuration Release
xcrun -sdk iphoneos PackageApplication -v "/XXX/XXX.app" -o "/XXX/XXX"
  • 另一种是xcodebuildarchive-exportArchive,然而最新需要使用--exportOptionsPlist选项,而不再使用--exportFromat
  • 使用xcodebuild -help命令可查看--exportOptionsPlist选项具体描述

如何获取工程的CODE_SIGN_IDENTITYPROVISIONING_PROFILE

  • 右键 xxx.xcodeproj 显示包内容 然后打开project.pbxproj文件
  • command F 搜索 CODE_SIGN_IDENTITY PROVISIONING_PROFILE
使用xcodebuild打包 导出ipa 上传蒲公英或者提交AppStore_第1张图片
QQ20161106-0.png
使用xcodebuild打包 导出ipa 上传蒲公英或者提交AppStore_第2张图片
QQ20161106-1.png
  • PROVISIONING_PROFILE 使用uuid或者名称都可以

  • 注意 因为我这里打包用的Release模式所以,我在查找CODE_SIGN_IDENTITYPROVISIONING_PROFILE都是找的Release模式配置

  • 使用atool时 最终输出的xml 中包含 success-message表示成功,如果包含product-errors表示失败

  • 执行脚本

// 首先把两个plist和脚本放到工程根目录,然后打开terminal进入项目
// 第一次需要设置脚本的执行权限
chmod +x xcodebuild.sh
// 执行脚本
./xcodebuild.sh

完整的脚本 其中 xxxx 替换为你自己的

#!/bin/sh

echo "~~~~~~~~~~~~~~~~开始执行脚本~~~~~~~~~~~~~~~~"


# 开始时间
beginTime=`date +%s`
DATE=`date '+%Y-%m-%d-%T'`
#需要编译的 targetName
TARGET_NAME="xxxx"
#编译模式 工程默认有 Debug Release 
CONFIGURATION_TARGET=Release
#编译路径
BUILDPATH=~/Desktop/${TARGET_NAME}_${DATE}
#archivePath
ARCHIVEPATH=${BUILDPATH}/${TARGET_NAME}.xcarchive
#输出的ipa目录
IPAPATH=${BUILDPATH}

#证书名
CODE_SIGN_IDENTITY="xxxxx"
#描述文件
PROVISIONING_PROFILE_NAME="xxxx"

#苹果账号
AppleID="xxxx"
AppleIDPWD="xxxx"

#导出ipa 所需plist
ADHOCExportOptionsPlist=./ADHOCExportOptionsPlist.plist
AppStoreExportOptionsPlist=./AppStoreExportOptionsPlist.plist

ExportOptionsPlist=${ADHOCExportOptionsPlist}


# 是否上传蒲公英
UPLOADPGYER=false
# 是否上传AppStore
UPLOADAPPSTore=false

echo "~~~~~~~~~~~~~~~~选择打包方式~~~~~~~~~~~~~~~~"
echo "      1 ad-hoc (默认)"
echo "      2 AppStore "

# 读取用户输入并存到变量里
read parameter
sleep 0.5
method="$parameter"

# 判读用户是否有输入 
if [ -n "$method" ]
then
    if [ "$method" = "1" ]
    then 
    PROVISIONING_PROFILE_NAME="xxxx"
    ExportOptionsPlist=${ADHOCExportOptionsPlist}
    elif [ "$method" = "2" ]
    then
    UPLOADAPPSTore=true
    PROVISIONING_PROFILE_NAME="xxxx"
    ExportOptionsPlist=${AppStoreExportOptionsPlist}
    else
    echo "参数无效...."
    exit 1
    fi
else
    ExportOptionsPlist=${ADHOCExportOptionsPlist}
fi

if [ $UPLOADAPPSTore = false ]
then
    echo "~~~~~~~~~~~~~~~~是否上传蒲公英~~~~~~~~~~~~~~~~"
    echo "      1 不上传 (默认)"
    echo "      2 上传 "
    read para
    sleep 0.5

    if [ -n "$para" ]
    then
        if [ "$para" = "1" ]
        then 
        UPLOADPGYER=false
        elif [ "$para" = "2" ]
        then
        UPLOADPGYER=true
        else
        echo "参数无效...."
        exit 1
        fi
    else
        UPLOADPGYER=false
    fi
fi


echo "~~~~~~~~~~~~~~~~开始编译~~~~~~~~~~~~~~~~~~~"
echo "~~~~~~~~~~~~~~~~开始清理~~~~~~~~~~~~~~~~~~~"
# 清理 避免出现一些莫名的错误
xcodebuild clean -workspace ${TARGET_NAME}.xcworkspace \
-configuration \
${CONFIGURATION} -alltargets

echo "~~~~~~~~~~~~~~~~开始构建~~~~~~~~~~~~~~~~~~~"
#开始构建
xcodebuild archive -workspace ${TARGET_NAME}.xcworkspace \
-scheme ${TARGET_NAME} \
-archivePath ${ARCHIVEPATH} \
-configuration ${CONFIGURATION_TARGET} \
CODE_SIGN_IDENTITY="${CODE_SIGN_IDENTITY}" \
PROVISIONING_PROFILE="${PROVISIONING_PROFILE_NAME}"

echo "~~~~~~~~~~~~~~~~检查是否构建成功~~~~~~~~~~~~~~~~~~~"
# xcarchive 实际是一个文件夹不是一个文件所以使用 -d 判断
if [ -d "$ARCHIVEPATH" ]
then
echo "构建成功......"
else
echo "构建失败......"
rm -rf $BUILDPATH
exit 1
fi
endTime=`date +%s`
ArchiveTime="构建时间$[ endTime - beginTime ]秒"


echo "~~~~~~~~~~~~~~~~导出ipa~~~~~~~~~~~~~~~~~~~"

beginTime=`date +%s`

xcodebuild -exportArchive \
-archivePath ${ARCHIVEPATH} \
-exportOptionsPlist ${ExportOptionsPlist} \
-exportPath ${IPAPATH}

echo "~~~~~~~~~~~~~~~~检查是否成功导出ipa~~~~~~~~~~~~~~~~~~~"
IPAPATH=${IPAPATH}/${TARGET_NAME}.ipa
if [ -f "$IPAPATH" ]
then
echo "导出ipa成功......"
else
echo "导出ipa失败......"
# 结束时间
endTime=`date +%s`
echo "$ArchiveTime"
echo "导出ipa时间$[ endTime - beginTime ]秒"
exit 1
fi

endTime=`date +%s`
ExportTime="导出ipa时间$[ endTime - beginTime ]秒"

# 上传AppStore
if [ $UPLOADAPPSTore = true ]
then    

    altoolPath="/Applications/Xcode.app/Contents/Applications/Application\ Loader.app/Contents/Frameworks/ITunesSoftwareService.framework/Versions/A/Support/altool"
    ${altoolPath} --validate-app \
    -f ${IPAPATH} \
    -u ${AppleID} \
    -p ${AppleIDPWD} \
    -t ios --output-format xml

        if [ $? = 0 ]
        then
        echo "~~~~~~~~~~~~~~~~验证ipa成功~~~~~~~~~~~~~~~~~~~"
            ${altoolPath} --upload-app \
            -f ${IPAPATH} \
            -u ${AppleID} \
            -p ${AppleIDPWD} \
            -t ios --output-format xml

            if [ $? = 0 ]
            then
            echo "~~~~~~~~~~~~~~~~提交AppStore成功~~~~~~~~~~~~~~~~~~~"
            else
            echo "~~~~~~~~~~~~~~~~提交AppStore失败~~~~~~~~~~~~~~~~~~~"
            fi
        else
        echo "~~~~~~~~~~~~~~~~验证ipa失败~~~~~~~~~~~~~~~~~~~"
        fi
else
    # 上传蒲公英 
    if [ $UPLOADPGYER = true ]
    then
        echo "~~~~~~~~~~~~~~~~上传ipa到蒲公英~~~~~~~~~~~~~~~~~~~"
        curl -F "file=@$IPAPATH" \
        -F "uKey=xxxxx" \
        -F "_api_key=xxxx" \
        -F "password=xxxxx" \
        -F "isPublishToPublic=xxxx" \
        https://www.pgyer.com/apiv1/app/upload --verbose

        if [ $? = 0 ]
        then
        echo "~~~~~~~~~~~~~~~~上传蒲公英成功~~~~~~~~~~~~~~~~~~~"
        else
        echo "~~~~~~~~~~~~~~~~上传蒲公英失败~~~~~~~~~~~~~~~~~~~"
        fi
    fi
fi



echo "~~~~~~~~~~~~~~~~配置信息~~~~~~~~~~~~~~~~~~~"
echo "开始执行脚本时间: ${DATE}"
echo "编译模式: ${CONFIGURATION_TARGET}"
echo "导出ipa配置: ${ExportOptionsPlist}"
echo "打包文件路径: ${ARCHIVEPATH}"
echo "导出ipa路径: ${IPAPATH}"

echo "$ArchiveTime"
echo "$ExportTime"
exit 1

github

你可能感兴趣的:(使用xcodebuild打包 导出ipa 上传蒲公英或者提交AppStore)