目前公司有一个需求,就是我们App要做多渠道,之前一个App但是现在客户要求各自的渠道包要用自的服务器地址。也就是之前一个包,现在有可能几十上百个包。
同一套代码,打出多个企业包,这些企业包应用的不同点如下:
- 应用名字
- Bundle id
- icon
- launch image
- 功能上稍有不同,比如第三方sdk 的key不同。
- 服务器地址不同
如果实现。目前网上也就两种打包方式,一种是全部打包,一种是只打一个母包然后修改plist文件,icon,launch image
看业务需求,两种打包方式都可能会用到,下面我就介绍这两种打包方法
全部打包,就是多Target方式,这里就叙述了,有关多Target可以自行搜索相关内容
#!/bin/sh
echo "~~~~~~~~~~~~~~~~~~~~ 开始执行打包脚本 ~~~~~~~~~~~~~~~~~~~~"
########################## 工程基本信息配置 ###########################
#循环数组,需要打包的渠道名称,以空格隔开
channelArray=("Channel1" "Channel2")
#项目路径
MWBuildDir="Users/Destiny/Desktop/wamingdaqi"
#工程名
projectName="MeteorologicalMonitoring"
#ExportOptions.plist 路径
exportOptionsPlistPath="/Users/Destiny/Desktop/BuildIpa/BuildShell/ExportOptions.plist"
#Release还是Debug
buildConfiguration="Release"
#Ipa导出路径
IpaExportPath="/Users/Destiny/Desktop/WamingIpa"
for ((i=0;i<${#channelArray};i++))
do
echo ${channelArray[$i]}
targetName=${channelArray[$i]}
echo $projectName
echo $targetName
#编译
xcodebuild archive -workspace ${projectName}.xcworkspace -scheme ${targetName} -configuration ${buildConfiguration} -sdk "iphoneos" clean archive -archivePath ./ArchivePath/${targetName}.xcarchive
#生成ipa
xcodebuild -exportArchive -archivePath ./ArchivePath/${targetName}.xcarchive -exportOptionsPlist ${exportOptionsPlistPath} -exportPath ${IpaExportPath}
#xcodebuild archive -workspace MeteorologicalMonitoring.xcworkspace -scheme WamingJinshan -configuration Release -sdk "iphoneos" clean archive -archivePath ./ArchivePath/WamingJinshan.xcarchive
done
这种打包方式的缺点就是慢,假如有100个渠道就要打100次,优点就是多Target方式各个渠道一目了然。
可能会遇到两个问题
1.在终端执行Shell脚本可能会报没有权限的错误
解决办法:chmod +x /路径/xxx.sh
2.ExportOptions.plist 是什么?
请参考:https://blog.csdn.net/lovechris00/article/details/79141752
第二种方式先打一个包,然后修改plist icon
#!/bin/sh
# Script.sh
# MPackage
#
# Created by Destiny on 2018/7/11.
# Copyright © 2018年 Destiny. All rights reserved.
# 1.Configuration Info
# 项目路径 需修改
projectDir="/Users/Destiny/Desktop/BuildIpafast"
# 打包生成路径 需修改
ipaPath="/Users/Destiny/Desktop/WamingIpa"
# 图标路径 需修改
iconPath="/Users/Destiny/Desktop/BuildIpafast/AppIcon"
# Project Name
projectName="MeteorologicalMonitoring"
# Scheme Name
schemeName="MeteorologicalMonitoring"
exportOptionsPlistPath="/Users/Destiny/Desktop/BuildIpafast/AutoBuildIpaShell/ExportOptions.plist"
#############
# 版本号
bundleVersion="2.0.0"
# 选择打包序号 多选则以空格隔开 如("1" "2" "3")
appPackNum=("1")
# 蒲公英分发参数 不分发可忽略 默认不分发 下面的两个KEY是默认测试的网址对应KEY
ISUPLOAD=0
USERKEY="xxx"
APIKEY="xxx"
# ---------------------------可选 如果需要替换 app 的 icon --------------------------------- #
# 配置App信息数组 格式:"AppName(和工程中appInfo.Plist对应)" "icon"
#Schemes:
# 1.app1 app1Icon
# 2.app2 app2Icon
# 3.app3 app3Icon
# --------------------------------------------------------------------------------------- #
# 打包个数
appPackNumLength=${#appPackNum[*]}
appNameInfos=(
"Channel1Name"
"Channel2Name"
)
appIconInfos=(
"AppIcon"
"AppIconShuangliu"
)
appChannelInfos=(
"jinshan"
"shuangliu"
)
appBundleInfos=(
"com.xxxxx.xxx"
"com.xxxxx.xxx"
)
appInfosLength=${#appBundleInfos[*]}
# 生成 APP 路径
buildDir="build/Release-iphoneos"
# 开始时间
beginTime=`date +%s`
# 创建打包目录
mkdir ${ipaPath}/allChannelIpa
# 本地存放全部 IPA 的路径
allIPAPackPath="${ipaPath}/allChannelIpa"
# 清除缓存
rm -rf ${projectDir}/$buildDir
# Build 生成 APP
xcodebuild archive -workspace ${projectName}.xcworkspace -scheme ${schemeName} -configuration Release -sdk iphoneos clean -archivePath ${projectDir}/build/${schemeName}.xcarchive
if [[ $? = 0 ]]; then
echo "\033[31m 编译成功\n \033[0m"
else
echo "\033[31m 编译失败\n \033[0m"
fi
# 先创建 payload 文件夹
mkdir ${ipaPath}/Payload
# 移动编译生成的 app 到的 Payload 文件夹下
cp -Rf ${projectDir}/build/${schemeName}.xcarchive ${ipaPath}/Payload
if [[ $? = 0 ]]; then
echo "\033[31m app移动成功\n \033[0m"
else
echo "\033[31m app移动失败\n \033[0m"
fi
# ----全部打包----
for (( i=0; i<appInfosLength; i++ )); do
# App Bundle Name (CFBundleName)
appName=${appNameInfos[${i}]}
# App DisPlay Name
appDisplayName=${appNameInfos[${i}]}
# App Icon Name
appIconName=${appIconInfos[${i}]}
# App Download Name
appChannelName=${appChannelInfos[${i}]}
# App Bundle id
appBundleId=${appBundleInfos[${i}]}
# 创建不同 app ipa 目录
mkdir $allIPAPackPath/${appChannelName}
rm -rf $allIPAPackPath/${appChannelName}}/*
echo "\033[31m appName:$appName appIconName:$appIconName appChannelName:$appChannelName bundleID:$appBundleId \n \033[0m"
# 将对应的 icon 复制到需要修改的 app 的目录下
# .xcarchive 文件下对应的Applications文件路径
Applications_Path=$ipaPath/Payload/${schemeName}.xcarchive/Products/Applications
cp -Rf $iconPath/$appIconName/* $Applications_Path/*.app
if [[ $? = 0 ]]; then
echo "\033[31m 修改 icon 成功\033[0m"
else
echo "\033[31m 修改 icon 失败\033[0m"
fi
# 修改 Plist
# plist路径
infoPlist_File_Path=$Applications_Path/*.app/info.plist
/usr/libexec/PlistBuddy -c "Set :CFBundleName $appName" $infoPlist_File_Path
/usr/libexec/PlistBuddy -c "Set :CFBundleDisplayName $appDisplayName" $infoPlist_File_Path
/usr/libexec/PlistBuddy -c "Set :CFBundleIdentifier $appBundleId" $infoPlist_File_Path
/usr/libexec/PlistBuddy -c "Set :Channel $appChannelName" $infoPlist_File_Path
if [[ $? = 0 ]]; then
echo "\033[31m 修改 Plist 成功\033[0m"
else
echo "\033[31m 修改 Plist 失败\033[0m"
fi
# 重签名
#codesign -f -s "iPhone Distribution: Beijing Waming Environmental Technology Co., Ltd" --entitlements $exportOptionsPlistPath ${ipaPath}/Payload/${schemeName}.app
#if [[ $? = 0 ]]; then
#echo "\033[31m 签名成功\n \033[0m"
#else
#echo "\033[31m 签名失败\n \033[0m"
#fi
# 生成 ipa
xcodebuild -exportArchive -archivePath ${ipaPath}/Payload/${schemeName}.xcarchive -exportOptionsPlist ${exportOptionsPlistPath} -exportPath ${allIPAPackPath}/$appChannelName
if [[ $? = 0 ]]; then
echo "\033[31m \n 生成 IPA 成功 \n\n\n\n\n\033[0m"
else
echo "\033[31m \n 生成 IPA 失败 \n\n\n\n\n\033[0m"
fi
# 移动
#mv ${ipaPath}/$appChannelName.ipa ${allIPAPackPath}/$appName
# 6.上传蒲公英分发平台
if [[ $ISUPLOAD = 1 ]]; then
echo "正在上传蒲公英..."
curl -F "file=@$allIPAPackPath/$appName/$appChannelName.ipa" -F "uKey=$USERKEY" -F "_api_key=$APIKEY" http://www.pgyer.com/apiv1/app/upload
fi
done
# 清除无关文件
rm -rf ${ipaPath}/Payload
# 结束时间
endTime=`date +%s`
echo -e "打包时间$[ endTime - beginTime ]秒"
有个问题需要注意
xxx.xcarchive右键显示包含内容,Products->Applications->xxxx->右键显示包含内容->复制出App Icon文件,要替换的Icon按照这种命名方式来
脚本都很简单都测试过打包没问题,有问题请留言
网友评论