一切得从上个版本的打包发布说起。
开发中本人负责了iOS包的版本发布工作。iOS打包:不就是选一下证书,再在Xcode上点几下按钮,IDE全都给你设置好流程了,有必要这么麻烦吗?
诚然,如果只是打包,在不考虑团队协同合作、打包效率、重复工作量的前提下,使用Xcode自带的打包方式当然是没问题的。但实际开发中,每次打包大概包含以下流程:
拉取最新代码(SVN或Git)
→ 编译通过
→ 设置打包环境(开发、测试、生产等)
→ 导出IPA包
→ 上传IPA包(App Store或者企业包上传至指定服务器)
可以看出,其中的很多步骤都是机械重复的,特别当进入测试验收阶段,有时每修复几个bug就要重新打包发布测试,比如上个版本的时候新功能主要是队友在开发,测试到最后频繁地让我打包发布,不停地打断我的工作去重复机械的事情,这简直就是在浪费人生啊!!!
虽然之前为了打包方便,我已经整理了一份脚本打包的流程(传送门:Shell脚本——Xcode脚本打包),但还是不够方便快捷,乘着新版发布后的空档期,撸出 一键打包发布系统 ,功能包括:
自动拉取Git最新代码
→ 自动选择签名证书并打包导出ipa文件
→ Git自动同步代码
→ 自动上传ipa包(我们是企业包,上传至自己的服务器,这一步是可选的)
一键打包
项目链接地址
一键打包资源(ArchiveSource)简介:
- Code文件夹下是打包应用程序源代码
- Source文件夹下是打包资源
Source文件夹:
- Archive.app(打包应用程序)
- ExportOptions.plist(导出ipa包配置文件)
一键打包步骤:
- 进入Source文件夹,启动 Archive 应用程序
- 选择 .xcodeproj 或 .xcworkspace 文件
- 修改打包版本号
启动Xcode,手动选择签名证书(如果默认签名失败的话)
- 点击打包
打包生成的IPA包路径说明:
../项目所在路径/Archiving/(以AppId命名的文件夹)/(App名+版本号命名的文件夹)/(以打包时间命名的文件夹)
示例:
../Archiving/(AppID)/(App名)_3_1_29/2018_04_10_11:18:51
一键打包原理
一键打包发布系统其实很简单:开发一款Mac应用,应用启动时读取本机已有的Certificates和Provisioning Profiles信息,再在应用内调用Shell脚本,主要通过脚本来实现Git同步以及打包的相关操作。
Shell脚本调用
Objective-C中调用Shell脚本可以使用 NSTask 。通过NSTask,我们可以在应用中调用另一个程序或运行一段脚本并获得其执行状态和最终结果,NSTask最为常用的一个场景是为命令行操作提供图形化的界面。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| NSTask *optTask = [[NSTask alloc] init];
optTask.launchPath = shellPath;
optTask.arguments = @[@"-ls"];
NSPipe *outputPipe = [NSPipe pipe]; [optTask setStandardOutput:outputPipe];
NSPipe *errorPipe = [NSPipe pipe]; [optTask setStandardError:errorPipe];
optTask.terminationHandler = ^(NSTask *theTask) { [theTask.standardOutput fileHandleForReading].readabilityHandler = nil; [theTask.standardError fileHandleForReading].readabilityHandler = nil; };
[[errorPipe fileHandleForReading] setReadabilityHandler:^(NSFileHandle *file) { NSData *data = [file availableData]; NSString *errorMsg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"errorMsg = %@",errorMsg); }];
[[outputPipe fileHandleForReading] setReadabilityHandler:^(NSFileHandle *file) { NSData *data = [file availableData]; NSString *msg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"msg = %@",msg); }];
[optTask launch];
[optTask waitUntilExit];
[[outputPipe fileHandleForReading] closeFile]; [[errorPipe fileHandleForReading] closeFile];
|
自动选择签名证书
获取电脑中以iPhone Distribution
和iPhone Developer
命名开头的Certificates:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| + (void)loadCerListBlock:(CerListBlock)listBlock { NSDictionary *options = @{(__bridge id)kSecClass: (__bridge id)kSecClassCertificate, (__bridge id)kSecMatchLimit: (__bridge id)kSecMatchLimitAll}; CFArrayRef certs = NULL; __unused OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)options, (CFTypeRef *)&certs); NSArray *certificates = CFBridgingRelease(certs);
NSMutableArray *tempArray=[NSMutableArray array]; for (int i=0;i<[certificates count];i++) { SecCertificateRef certificate = (__bridge SecCertificateRef)([certificates objectAtIndex:i]); NSString *name = CFBridgingRelease(SecCertificateCopySubjectSummary(certificate));
if ([name hasPrefix:@"iPhone Distribution"]||[name hasPrefix:@"iPhone Developer"]) { [tempArray addObject:name]; } } listBlock(tempArray); }
|
获取电脑中Provisioning Profiles:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| + (NSArray *)getAllProvisioningProfileList{ NSString *path=[NSString stringWithFormat:@"%@/%@", NSHomeDirectory(), kMobileprovisionDirName]; NSFileManager *fileManager=[NSFileManager defaultManager]; NSArray *provisioningProfiles =[fileManager contentsOfDirectoryAtPath:path error:nil]; provisioningProfiles = [provisioningProfiles filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"pathExtension IN %@", @[@"mobileprovision", @"provisionprofile"]]]; return provisioningProfiles; }
YAProvisioningProfile *profile = [[YAProvisioningProfile alloc] initWithPath:path];
|
根据App ID匹配 Profile 描述文件:
1 2 3 4 5 6 7 8 9
| YAProvisioningProfile *selectProfile = nil; for (YAProvisioningProfile *profile in profileArray) { if ([profile.bundleIdentifier isEqualToString:appBundleIdentifier] && ![profile.name hasPrefix:@"XC:"] && profile.newest) { selectProfile = profile; break; } }
|
读写Xcode工程配置
右键 XXX.xcodeproj 文件,显示包内容,可以看到project.pbxproj
。project.pbxproj
存储着 Xcode 工程的各项配置参数,我们可以通过脚本来直接读取或修改其配置,执行效果等同于在Xcode的General、Build Settings中的修改。
读取App Bundler Identifier:
1 2 3 4 5 6 7 8 9 10
| configuration=$(grep -i "PRODUCT_BUNDLE_IDENTIFIER =" ${Pbxproj_Path}) Array=($(echo $configuration))
Bundle_Identifier=${Array[5]} if [ ! -n $Bundle_Identifier ] then Bundle_Identifier=${Array[3]} fi echo "Bundle_Identifier = ${Bundle_Identifier}"
|
修改配置信息:
修改 project.pbxproj 配置可以通过 sed
命令实现,比如将签名类型指定为Manual
1 2 3 4
| key="CODE_SIGN_STYLE" value="Manual;"
sed -i "" "s/$key =.*$/$key = $value/g" $Pbxproj_Path
|
如果程序成功读取到对应的证书签名,那么打包前需要修改的配置包括:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| changeConfiguration "PRODUCT_BUNDLE_IDENTIFIER" "${APP_ID};"
changeConfiguration "PROVISIONING_PROFILE" "\"${Profile_Name}\";"
changeConfiguration "PROVISIONING_PROFILE_SPECIFIER" "${Profile_Specifier};"
changeConfiguration "PRODUCT_NAME" "${Scheme_Name};"
changeConfiguration "\"CODE_SIGN_IDENTITY\[sdk=iphoneos\*\]\"" '"iPhone Distribution";'
changeConfiguration "CODE_SIGN_STYLE" "Manual;"
changeConfiguration "ProvisioningStyle" "Manual;"
changeConfiguration "DEVELOPMENT_TEAM" "${Development_Team};"
changeConfiguration "DevelopmentTeam" "${Development_Team};"
|
修改项目版本号:
Info.plist文件中CFBundleShortVersionString
对应Version号,CFBundleVersion
对应Build号,使用脚本可以直接指定版本号
1 2 3 4 5 6 7 8 9
| InfoPlist_Path="${Project_Path}/${App_Name}/Info.plist"
if [ ! -e "${InfoPlist_Path}" ];then InfoPlist_Path="${Project_Path}/${App_Name}/${App_Name}-Info.plist" fi
/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString ${Version_String}" ${InfoPlist_Path} /usr/libexec/PlistBuddy -c "Set :CFBundleVersion ${Version_String}" ${InfoPlist_Path}
|
打包项目
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| Configuration="Release"
if [[ ${Is_Workspace} == "YES" ]]; then xcodebuild clean -configuration "${Configuration}" -alltargets >> ${Log_path}
if [[ ${Custom_Sign} == "YES" ]]; then xcodebuild archive -workspace "${Workspace_Path}" \ -scheme "${Scheme_Name}" \ -archivePath "${Xcarchive_path}" \ -configuration "${Configuration}" \ PROVISIONING_PROFILE="${Profile_Name}" \ CODE_SIGN_IDENTITY="${Sign_Identity}" \ >> ${Log_path} else xcodebuild archive -workspace "${Workspace_Path}" \ -scheme "${Scheme_Name}" \ -archivePath "${Xcarchive_path}" \ -configuration "${Configuration}" \ >> ${Log_path} fi
else xcodebuild clean -configuration "${Configuration}" -alltargets >> ${Log_path}
if [[ ${Custom_Sign} == "YES" ]]; then xcodebuild archive -project "${Xcodeproj_Path}" \ -scheme "${Scheme_Name}" \ -archivePath "${Xcarchive_path}" \ -configuration "${Configuration}" \ PROVISIONING_PROFILE="${Profile_Name}" \ CODE_SIGN_IDENTITY="${Sign_Identity}"\ >> ${Log_path} else xcodebuild archive -project "${Xcodeproj_Path}" \ -scheme "${Scheme_Name}" \ -archivePath "${Xcarchive_path}" \ -configuration "${Configuration}" \ >> ${Log_path} fi
fi
|
导出IPA包
1
| xcodebuild -exportArchive -archivePath "${Xcarchive_path}" -exportPath "${IPA_Archiving_Path}" -exportOptionsPlist "${ExportOptionsPlistPath}" >> ${Log_path}
|
Xcarchive_path指向上一步打包生成的*.xcarchive
文件路径;IPA_Archiving_Path指向导出的*.ipa
文件所在路径;ExportOptionsPlistPath对应ExportOptions.plist
文件路径,ExportOptions.plist是使用xcodebuild -exportArchive指令导出ipa包时需要指定的配置文件
- compileBitcode:不上架App Store,Xcode是否启用Bitcode重新编译,默认为YES。
- method:归档类型,包括app-store、ad-hoc、
package、enterprise、development以及developer-id。
- provisioningProfiles:打包证书信息,包含的字典信息格式:<key为App ID>:<value为对应的profile文件名>。
- uploadBitcode:上线App Store是否开启Bitcode,默认为YES。
- uploadSymbols:上线App Store,是否开启符号序列化,这是与查crash相关的,默认为YES。
关于更多的xcodebuild指令,可以通过xcodebuild -help
查看。
其他
其他部分还包括Git代码管理、IPA包上传、无效资源删除等,都可以直接通过NSTask执行脚本命令实现,比如拉取Git代码:git pull origin
,当然前提是你本地的Git已经配置为免密操作。
如果你是使用SourceTree进行Git管理,而且是http模式,那么可以这样设置免密操作,第3步将远程仓库路径编辑为 http://用户名:密码@仓库地址
这样的方式;当然如果你是SSH模式,那么本身就支持免密码操作了。
上传IPA包。
我这里打的是企业包,所以只要将ipa文件上传到指定服务器就能够下载了(想了解更多关于企业包的下载信息,可以参照我的另一篇文章iOS如何部署企业包,以供他人下载)。一开始想着将自动上传也集成到打包程序内,但最后发现这涉及到内网间不同服务器以及账号验证过程,太过复杂暂时将这一部分阉割了……
写在最后
以上便是一键打包系统的功能讲解,想了解更多可以查看源码
CJShellDemo 说明:
ArchiveSource 一键打包程序资源
CrashScript 线上crash查找脚本
ReleaseDir Xcode打包脚本
欢迎点赞以及GitHub Star
最新补充
根据反馈发现:不同的Xcode版本构建的项目配置文件project.pbxproj
会存在差异,另外不同用户电脑上的Provisioning Profiles
也存在各种情况(比如团队中不同成员拥有不同名字的有效Profile)。
此时运行一键打包可能会失败!提示 重命名ipa包失败 或者 导出ipa包失败 ,这种情况下请关闭“默认签名”选项,同时打开Xcode在工程文件中手动选择对应打包证书后,再返回Archive.app执行一键打包。
应用打包截图