背景
新项目这边每次版本移交,
给测试都是直接xcode挨个拿手机安装的流程.
一次两次还好,天天这么搞,而且每次一大波手机扔过来,瞬间觉得,自己好像是个疫苗注射员似的,顺次给每个手机打一针.
biu!
NEXT...
biu!
NEXT...
biu!
NEXT...
...
😢
...
于是..我崩溃了..
好吧,
是在是看不下去了,于是准备写个脚本以后自己打包,测试直接下载就好了.
思路
那么怎么实现这么一个事情呢?
其实上一家公司,做个类似的东西,虽不是我做的,但是大体流程还是知道的:
- 写个bash脚本,执行自动打包iOS版本,到指定的目录 (有条件的公司,可以自己搭个小服务器,这样谁都可以随时随地的打包)
- 将打包好的文件上传到fir.im (当然上传到自己公司的服务器或者任何地方都行,只是fir.im我一直用,觉得比较方便)
- 开发一个内部使用的类似APPStore,上面放着自己公司的所有APP,每次有更新的时候,测试童鞋直接通过这个自己下载新APP就可以了
开始做
1. 准备背景知识
其实当我们Xcode点击了build或者运行comand+R之后,Xcode自己执行的命令是xcodebuild这条命令.
然后,编译好之后,怎么生产ipa包?
用xcrun命令
话不多说,先上手:
打开终端,cd到你的工程位置,然后先试一下xcodebuild命令,
//xcrun
chengpoleness@polen xcodebuild_iPA $ xcrun --version
xcrun version 29.
//xcodebuild
chengpoleness@polen ios (develop) $ xcodebuild -version
Xcode 7.3
Build version 7D175
看上去很简单,大概了解了一下,就开始用:
chengpoleness@polen ios (develop) $ xcodebuild
2016-05-02 13:05:04.623 xcodebuild[1015:16272] [MT] PluginLoading: Required plug-in compatibility UUID ACA8656B-FEA8-4B6D-8E4A-93F4C95C362C for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/XcodeColors.xcplugin' not present in DVTPlugInCompatibilityUUIDs
2016-05-02 13:05:04.625 xcodebuild[1015:16272] [MT] PluginLoading: Required plug-in compatibility UUID ACA8656B-FEA8-4B6D-8E4A-93F4C95C362C for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/OMColorSense.xcplugin' not present in DVTPlugInCompatibilityUUIDs
=== BUILD TARGET xxx OF PROJECT xxx WITH THE DEFAULT CONFIGURATION (Release) ===
Check dependencies
Write auxiliary files
write-file /Users/chengpoleness/Documents/code/xxx/ios/build/xxx.build/Release-iphoneos/xxx.build/xxx.hmap
write-file /Users/chengpoleness/Documents/code/xxx/ios/build/xxx.build/Release-iphoneos/xxx.build/xxx-own-target-headers.hmap
write-file /Users/chengpoleness/Documents/code/xxx/ios/build/xxx.build/Release-iphoneos/xxx.build/Script-492B764475E022A63FB67F55.sh
接着满屏的快速滚动,可以看到clang依次在编译各个文件,登录大概十几秒闪屏,然后失败了!!!💔
ld: library not found for -lPods
clang: error: linker command failed with exit code 1 (use -v to see invocation)
** BUILD FAILED **
The following build commands failed:
Ld build/xxx.build/Release-iphoneos/xxx.build/Objects-normal/armv7/xxx normal armv7
Ld build/xxx.build/Release-iphoneos/xxx.build/Objects-normal/arm64/xxx normal arm64
(2 failures)
这个就是找不到pods了,
为什么会这样?
自己直接用Xcode 亲自product-archive 了一遍,okey啊,没出现任何问题!
那是为什么呢?
各种goole,stackoveflow,但是基本说的是不要只打开project,而是打开workspace,其实这个是针对直接用xcode进行编译或者打包的时候,出现问题的解决方案,
然后尝试了其他解决方案,诸如ONLY_ACTIVE_ARCHS = NO等,都无效.
最终在下面这篇文章里,找到了灵感:
iOS Integration Tests With Appium
其实解决方案很简单,执行xcodebuild需要指定你所需要对应的workspace和scheme
所以命令如下:
xcodebuild -workspace /Users/chengpoleness/Documents/code/xxx/ios/xxx.xcworkspace -scheme xxx
执行前,先查看下-list,这个可以知道xcodebuild命令下对应的参数需要填写的内容
chengpoleness@polen ios (develop) $ xcodebuild -list
2016-05-02 15:24:26.656 xcodebuild[16535:154176] [MT] PluginLoading: Required plug-in compatibility UUID ACA8656B-FEA8-4B6D-8E4A-93F4C95C362C for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/XcodeColors.xcplugin' not present in DVTPlugInCompatibilityUUIDs
2016-05-02 15:24:26.661 xcodebuild[16535:154176] [MT] PluginLoading: Required plug-in compatibility UUID ACA8656B-FEA8-4B6D-8E4A-93F4C95C362C for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/OMColorSense.xcplugin' not present in DVTPlugInCompatibilityUUIDs
Information about project "xxx":
Targets:
xxx
xxxTests
Build Configurations:
Debug
Release
If no build configuration is specified and -scheme is not passed then "Release" is used.
Schemes:
xxx
然后,成功了,如下:
chengpoleness@polen ios (develop) $ xcodebuild -workspace /Users/chengpoleness/Documents/code/xxx/ios/xxx.xcworkspace -scheme xxx
2016-05-02 15:26:29.627 xcodebuild[16552:156501] [MT] PluginLoading: Required plug-in compatibility UUID ACA8656B-FEA8-4B6D-8E4A-93F4C95C362C for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/XcodeColors.xcplugin' not present in DVTPlugInCompatibilityUUIDs
2016-05-02 15:26:29.629 xcodebuild[16552:156501] [MT] PluginLoading: Required plug-in compatibility UUID ACA8656B-FEA8-4B6D-8E4A-93F4C95C362C for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/OMColorSense.xcplugin' not present in DVTPlugInCompatibilityUUIDs
=== BUILD TARGET Pods OF PROJECT Pods WITH CONFIGURATION Debug ===
Check dependencies
=== BUILD TARGET xxx OF PROJECT xxx WITH CONFIGURATION Debug ===
Check dependencies
PhaseScriptExecution Check\ Pods\ Manifest.lock /Users/chengpoleness/Library/Developer/Xcode/DerivedData/xxx-djwnuzytnxhekrhjhypwxdqrxtet/Build/Intermediates/xxx.build/Debug-iphoneos/xxx.build/Script-492B764475E022A63FB67F55.sh
...
...
...
Validate /Users/chengpoleness/Library/Developer/Xcode/DerivedData/xxx-djwnuzytnxhekrhjhypwxdqrxtet/Build/Products/Debug-iphoneos/xxx.app
cd /Users/chengpoleness/Documents/code/xxx/ios
export PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/Users/chengpoleness/.rvm/gems/ruby-2.0.0-p598/bin:/Users/chengpoleness/.rvm/gems/ruby-2.0.0-p598@global/bin:/Users/chengpoleness/.rvm/rubies/ruby-2.0.0-p598/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/chengpoleness/.rvm/bin:/usr/local/marven/apache-maven-3.3.9/bin:/Users/chengpoleness/Library/Android/sdk/platform-tools:/Users/chengpoleness/Library/Android/sdk/tools"
export PRODUCT_TYPE=com.apple.product-type.application
builtin-validationUtility /Users/chengpoleness/Library/Developer/Xcode/DerivedData/xxx-djwnuzytnxhekrhjhypwxdqrxtet/Build/Products/Debug-iphoneos/xxx.app
** BUILD SUCCEEDED **
2. 写脚本
(挑重点地说) 大致流程就是:
2.1 先git 指令,pull到最新的分支
这个很简单,没什么好说的了.
git clean -df
git reset --hard
git fetch
git checkout $BRANCHNAME
git pull --rebase origin $BRANCHNAME
echo "即将打包的分支log如下:"
git log -5
pod update --verbose --no-repo-update
不过这几个指令,带有删除性,比如git clean -df,git reset 这种,所以用的时候要谨慎,如果你把脚本放在自己的工程目录下,就可能一不小心脚本就被删掉了(亲历者说...😢)
所以最终版本改成了
# git update
git checkout $BRANCHNAME
if [ $? -ne 0 ]; then
exit 1
fi
git pull
#pod update --verbose --no-repo-update
if [ $? -ne 0 ]; then
exit 1
fi
只保留了checkout 和pull, pod没有用,是因为我们这边除了googleMap其他全是自造轮子,所以...并不需要😄.
2.2 xcodebuild进行编译
xcodebuild \
-workspace $SORCEPATH/xxx.xcworkspace \
-scheme $SCHEMENAMEPLQ \
-configuration Debug \
CODE_SIGN_IDENTITY="iPhone Developer: xxxx(T8TXPM5FC7)" \
PROVISIONING_PROFILE="5110ff0a-b845-475e-a4xxxxxxxxxx" \
clean \
build \
-derivedDataPath $IPAPATH/$BRANCHNAME/$DATE
备注:
我这边因为是研发阶段,所以打包是debug包,如果是要打release 包,configuration中修改成Release,以及其他对应的修改下就行.
2.3 成功后xcrun打包成ipa包
xcrun -sdk iphoneos PackageApplication \
-v $IPAPATH/Build/Products/Debug-iphoneos/$SCHEMENAME.app \
-o $IPAPATH/$IPANAME
最终脚本如下:
屏幕快照 2016-05-04 下午8.20.47.png打包的文件命名我是按照时间来命名的,这样也方便找,尤其应对于可能分分钟就要突然来打包的测试团队.
3. 生成ipa包,上传到fir.im
3.1 fir比较好,有命令可以直接上传,但是需先安装fir-cli
安装过程如下:
chengpoleness@polen xcodebuild_iPA $ gem install fir-cli
Fetching: chunky_png-1.3.5.gem (100%)
Successfully installed chunky_png-1.3.5
Fetching: rqrcode-0.10.1.gem (100%)
Successfully installed rqrcode-0.10.1
...
...
______________ ________ ____
/ ____/ _/ __ \ / ____/ / / _/
/ /_ / // /_/ /_____/ / / / / /
/ __/ _/ // _, _/_____/ /___/ /____/ /
/_/ /___/_/ |_| \____/_____/___/
...
...
Installing ri documentation for thor-0.19.1
Parsing documentation for fir-cli-1.4.9
unable to convert "\xCE" from ASCII-8BIT to UTF-8 for lib/fir/util/parser/bin/pngcrush, skipping
Installing ri documentation for fir-cli-1.4.9
13 gems installed
安装如果遇到问题,可以到这里查看原因
3.2 使用fir指令,上传我们的ipa包
非常简单
fir login -T c525718a775b954882xxxxxxxx # fir.im token
fir publish $IPAPATH/Develop/xxx.ipa
3.3 扩展
另外,也可以直接用fir指令,进行打包或者编译,这个具体也没研究,其实就是对xcodebuild做了个封装,本质上还是执行那些指令,这里不再详述了.
chengpoleness@polen xcodebuild_iPA $ fir
Commands:
fir build_apk BUILD_DIR # Build Android app (alias: `ba`).
fir build_ipa BUILD_DIR [options] [settings] # Build iOS app (alias: `bi`).
fir help # Describe available commands or one specific command (aliase...
fir info APP_FILE_PATH # Show iOS/Android app info, support ipa/apk file (aliases: `...
fir login # Login fir.im (aliases: `l`).
fir mapping MAPPING_FILE_PATH # Upload app mapping file to BugHD.com (aliases: `m`).
fir me # Show current user info if user is logined.
fir publish APP_FILE_PATH # Publish iOS/Android app to fir.im, support ipa/apk file (al...
fir upgrade # Upgrade fir-cli and quit (aliases: `u`).
fir version # Show fir-cli version number and quit (aliases: `v`).
Options:
-T, [--token=TOKEN] # User's API Token at fir.im
-L, [--logfile=LOGFILE] # Path to writable logfile
-V, [--verbose], [--no-verbose] # Show verbose
# Default: true
-q, [--quiet], [--no-quiet] # Silence commands
-h, [--help], [--no-help] # Show this help message and quit
4. 测试一下
4.1 执行.sh文件
chengpoleness@polen xcodebuild_iPA $ sh xcodebuild.sh
要是有比较懒的同学,直接把.sh文件,拖到命令行里面也行
4.2 过程与结果
编译过程的输出信息太多,删减了下,大概看一下流程:
chengpoleness@polen xcodebuild_iPA $ sh xcodebuild.sh
HEAD is now at 30865e1 version 3.5.0, build 29
Already on 'develop'
...
...
=== CLEAN TARGET Pods OF PROJECT Pods WITH CONFIGURATION Debug ===
=== CLEAN TARGET xxx OF PROJECT xxx WITH CONFIGURATION Debug ===
...
...
** BUILD SUCCEEDED **
/* '以上是编译成功,接下来打包ipa' */
...
Packaging application: '/Users/chengpoleness/Documents/code/xxx/buildiPA/develop/20160502_1801/Build/Products/Debug-iphoneos/xxx.app'
Arguments: output=/Users/chengpoleness/Documents/code/xxx/buildiPA/develop/20160502_1801/1xxx_20160502_1801.ipa verbose=1
Environment variables:
_system_name = OSX
...
### Checking original app
...
Done checking the original app
...
adding: Payload/xxxx.app/xxxViewGpsOverlay.nib (in=4889) (out=2502) (deflated 49%)
total bytes=53793797, compressed=23588024 -> 56% savings
]
Results at 'xxxxxxxx/Documents/code/xxx/buildiPA/develop/20160502_1801/xxx_20160502_1801.ipa'
/* '以上是打包成功,接下来是上传fir.im' */
I, [2016-05-02T18:03:56.614082 #47670] INFO -- : Login succeed, current user's email: xxxx
I, [2016-05-02T18:03:56.614215 #47670] INFO -- :
I, [2016-05-02T18:03:57.766434 #47674] INFO -- : Publishing app via chenglong<chenglong@squarevalleytech.com>.......
I, [2016-05-02T18:03:57.766570 #47674] INFO -- : ✈ -------------------------------------------- ✈
I, [2016-05-02T18:03:59.813783 #47674] INFO -- : Fetching com.osmgolf.xxx@fir.im uploading info......
I, [2016-05-02T18:03:59.813868 #47674] INFO -- : Uploading app: xxx-3.5.0(Build 29)
I, [2016-05-02T18:04:00.562896 #47674] INFO -- : Uploading app icon......
I, [2016-05-02T18:04:00.563029 #47674] INFO -- : Converting app's icon......
I, [2016-05-02T18:04:01.715439 #47674] INFO -- : Uploading app binary......
I, [2016-05-02T18:04:16.091891 #47674] INFO -- : Updating devices info......
I, [2016-05-02T18:04:16.244710 #47674] INFO -- : Updating app info......
I, [2016-05-02T18:04:16.514628 #47674] INFO -- : ✈ -------------------------------------------- ✈
I, [2016-05-02T18:04:16.514862 #47674] INFO -- : Fetch app info from fir.im
I, [2016-05-02T18:04:17.019304 #47674] INFO -- : Published succeed: http://fir.im/xxxx
I, [2016-05-02T18:04:17.019409 #47674] INFO -- :
恭喜!!!上传fir.im成功!
OK,测试通过.
5.开发内部版“AppStore”
这个就这里不详述了,因为太简单.
写个tableview,把自己的app的链接放上去,每次有新版本了,通过这个App就可以下载,之前做过的截图如下:
至此,自动打包IPA的相关工作开发完成,可以投入使用了...
6 扩展-Debug/Release
6.1 背景:
目前的脚本打包都是默认打debug包的,但是如果我打Release包怎么办?如果我Debug包不同网络域名想切换怎么办?
6.2 Debug/Release
Okey,我们依次来解决这些问题:
其实很简单,xcodebuild指令有个-configuration, 我们使用这个决定是打Debug还是Release
那这个打出来的Debug和Release有什么区别呢?
这个相当于编译的时候是按照Debug模式或者Release模式来打包,从编译角度来说,区别挺大的,比如Debug会在某些堆栈环节预留更多的空间(我之前就遇到过同样的代码,但是Debug没问题,Release会有bug的情况,如果不知道这个的话,很难查出来或者理解这些bug,具体细节有空专门细说吧,这里不详述了)
所以,解决方案:定义一个CONGRUATION,根据传入的参数,决定打Debug还是打Release
# $1表示传入的第一个参数,启动脚本传入Debug或者Release就可以
CONGRUATION=$1
# 在xcodebuild和xrun的地方都使用$CONGRUATION即可
xcodebuild \
-workspace $SOURCEPATH/xxx.xcworkspace \
-scheme $SCHEMENAME \
-configuration $CONGRUATION \
-derivedDataPath $IPAPATH \
xcrun -sdk iphoneos PackageApplication \
-v $IPAPATH/Build/Products/$CONGRUATION-iphoneos/$SCHEMENAME.app \
-o $IPAPATH/$IPANAME
6.2 Different Host 域名切换
这个其实解决方案很多:
方案A:
代码里写个Debug页面,里面设置可以切换不同的域名即可,
以前公司的APP就是这么做的,因为我们测算环境非常多,比如:
QA01,QA02,QA03...QA10...
所以直接在APP里就可以进行切换不同域名.
polen提醒:
1> 切换后一定要让用户退出,避免发生不必要的逻辑混乱
2> 需要设置个宏开关,确保Release版本用户100%走线上域名,Debug版本可以随意加各种逻辑判断.
务必避免如果代码不建壮,发生线上用户误入测试环境的悲剧 (当然,这个肯定属于bug了,严格是不应该发生的了,但是难免意外,没有东西是100%绝对的,这里只是提醒...)
方案B:
如果你们的测试环境没那么多,可能就2-3个,可以在打包的时候,通过脚本决定用哪个环境,具体怎么做:
CTO说怎么样能让脚本给代码传个值进入,然后代码在编译的时候根据这个值决定用哪个测试环境.
思考了很多方法,包括用脚本去改代码(硬改,用sed进入到.m文件修改,但这样太危险,而且很不友好)
后来学习到使用PREPROCESSOR_DEFINITIONS,就是xcode在打包前会先从 一个config中读取一些配置信息,这些在xcode-building里面可以直接设置,自然也可以通过xcodebuild指定穿进去,传入的就是GCC_PREPROCESSOR_DEFINITIONS
所以就是:
设定一个SERVER_TYPE宏定义,如果#ifdef SERVER_TYPE的话,使用传入的参数,否则使用代码自定义的逻辑
# $serverType是启动脚本传入的参数值
if [ $CONGRUATION = Debug ]; then
PREPROCESSOR_DEFINITIONS="COCOAPODS=1 SERVER_TYPE=$serverType DEBUG=1"
else
PREPROCESSOR_DEFINITIONS="COCOAPODS=1 SERVER_TYPE=$serverType"
fi
xcodebuild \
-workspace $SOURCEPATH/xxx.xcworkspace \
-scheme $SCHEMENAME \
GCC_PREPROCESSOR_DEFINITIONS="$PREPROCESSOR_DEFINITIONS" \
-configuration $CONGRUATION \
-derivedDataPath $IPAPATH \
具体代码中如何使用:
//HOST_TYPE决定最终的域名
#ifdef SERVER_TYPE
#define HOST_TYPE SERVER_TYPE
#else
#ifdef DEBUG
#define HOST_TYPE HOST_TYPE_CANARY
#else
#define HOST_TYPE HOST_TYPE_PRODUCTION
#endif
#endif
okey,以上就解决了Debug和Release相关的问题
7.遇到的一些疑难杂症
br>
7.1 SDK不匹配导致编译失败
7.1.1 背景
下午(2016.8.2)正在忙,突然测试同学跑过来,大喊:“不好了,不好了...”
我以为要说台风来了,(做好了可以下班的准备..😄)
结果,来了句:
“打包脚本失败了,失败了!!”
我擦,啥情况,用了几个月了好好的,突然就失败了...
7.1.1 原因
后来确认下来是这么个情况,iOS10发布了,之前一直拖延,最近不是很忙了,于是有同事下载了xcode8-beta3和iOS10的安装profile。
然而iOS10的SDK更新,导致我们有个代码逻辑无法实现,于是Fix了一下,然后继续交给测试同学打包测试,结果就打包失败了,我看了下失败原因:
error:
property 'firstAnchor' not found on object of type 'NSLayoutConstraint *'
object_setIvar(constraint.firstAnchor, referenceItem, view);
屏幕快照 2016-08-02 下午5.44.54.png
然后查了下代码,代码逻辑应该是没问题的。而找不到这个property是因为这个代码使用的iOS10的SDK,用旧版本自然是找不到,然后问了下测试,直接用xcode编译可以么?回答说可以。
那分析下来就应该是路径的问题了.
去她位置上看了下,(因为我自己没装xcode8,所以自己没法调试).一看发现问题了,她目前是两个xcode都在,但一个叫Xcode,一个叫Xcode-beta,脚本跑起来的时候,SDK肯定还跑得是以前的xcode的啊,自然编不过,于是改了一下名字,把原来的Xcode改为Xcode7,新的Xcode-beta改为Xcode.
然后,问题解决!😄
如果后续脚本有扩展,我会继续更新此文.
Notes: 有些地方应该是iOS,而非ios,懒得改了,就这样吧,嗯.
网友评论
xxx就是你的target的名字
unable to find utility "PackageApplication", not a developer tool or in PATH
DATE=$(DATE +%Y%m%d_%H%M)
fir login -T c525718a775b954882xxxxxxxx
fir publish $IPAPATH/Develop/xxx.ipa
你注册了fir账号之后会有token,且他们的官网也有很详细的介绍文档。
1. xcodebuild 方法需要加入-archivePath xx/xxx/xxx.archive archive
2. 将archive转为ipa包:xcodebuild -exportArchive -archivePath xx/xxx/xxx.archive - -exportOptionsPlist xxx/xx.plist -exportPath xxx/xx
最近因为太忙,所以没时间更新文章,你直接搜一下相关的方法或者接口,用新的方式。
error: Specified application doesn't exist or isn't a bundle directory : '/Users/****/Desktop/****.app'
CODE_SIGN_IDENTITY = 发布证书
PROVISIONING_PROFILE = adhoc配置文件
CONFIGURATION = "Release"
SDK = "iphoneos"
谢谢