Fastlane iOS

自动打包

因为公司目前的项目需要一个项目打多个包,打包实际上是一个纯体力活,所以为了提升效率,之前同事写了一个利用Fastlane自动打包的脚本,最近项目不忙,所以看看文档总结一下!

安装

确保安装了最新的 Xcode command line tools

1
2
xcode-select --install

安装Fastlane

安装Fastlane

只需要这两步,安装过程就大功告成,下面开始把玩fastlane

初始化Fastlane

进入到你的项目的根目录(与xcodeproj文件同级目录下)

1
fastlane init

期间会让你输入 Apple ID 和密码
输入密码

确认信息
确认信息

会自动下载现存的截图
下载现存的截图

初始化完成后的目录结构
目录结构

至此 fastlane就初始化完成了,再接再厉!!!

截图

每次发布新版本的时候截图是一件比较恶心的事情(虽然16年更新之后我们只需要上传最大尺寸的机型就可以适配),fastlane也可以为我们解决这件事!

初始化

1
fastlane snapshot init

初始化成功之后 我们可以得到

导入文件

导入上一步生成的SnapshotHelper.swift文件到你的UITest目录下
注意:如果你是OC的项目这里要设置一下桥接

桥接

注意 对于 SnapshotHelper.swift

导入头文件

这个头文件是系统默认生成的,不用我们手动创建,我们在拖入Siwft文件之后,直接引用就可以。 没有智能提示,所以不必惊慌,保证#import’target名称-Swift.h’正确就可以了。

录制

1
2
3
(void)setUp: 方法在XCTestCase的测试方法调用之前调用,可以在测试之前创建在test case方法中需要用到的一些对象等
(void)tearDown: 当测试全部结束之后调用tearDown方法,法则在全部的test case执行结束之后清理测试现场,释放资源删除不用的对象等
(void)testExample: 测试代码执行性能

添加截图代码

在setUp方法中添加

1
2
3
XCUIApplication *app = [[XCUIApplication alloc] init];
[Snapshot setupSnapshot:app];
[app launch];

在在录制步骤产生的步骤代码中加入(testExample中)

1
[Snapshot snapshot:@"01LoginScreen" waitForLoadingIndicator:YES];

修改Snapfile文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
要截图的设备 默认所有的设备
# A list of devices you want to take the screenshots from
devices([
"iPhone 7",
"iPhone 7 Plus",
"iPhone 6"
])

支持的语言,对于支持多语言的应用比较有利
languages([
"en-US",
#"de-DE",
#"it-IT",
#["pt", "pt_BR"] # Portuguese with Brazilian locale
])

生成截图

1
fastlane snapshot


添加边框

安装ImageMagick

1
2
brew install libpng jpeg imagemagick

下载最新的边框

1
2
fastlane frameit setup

生成带边框的截图

1
2
fastlane frameit

结果

pem 生成推送证书

命令

1
fastlane pem
  • 1、创建新的签名请求(signing request)
  • 2、创建新的推送证书( push certification)
  • 3、下载这个证书(certificate)
  • 4、在当前工作目录创建新的 .pem 文件,你可以上传到你的服务器

pem不会revoke已经存在的证书,也不会下载已经存在的证书,因为私钥仅在创建的机器上可用

实用命令

如果你已经有一个可用的推送证书(certificate),而且有效期至少还有30天以上,pem就不会创建新的证书。如果你想要创建新的,使用force:

1
fastlane pem --force

还可传参数,如:

1
fastlane pem -a com.krausefx.app -u username

生成开发证书:

1
fastlane pem --development

给p12文件设置密码:

1
fastlane pem -p "MyPass"

可以指定输出名字:

1
fastlane pem -o my.pem

列出可用命令选项:

1
fastlane pem --help
命令 描述
–development [VALUE] Renew the development push certificate instead of the production one (PEM_DEVELOPMENT)
–generate_p12 [VALUE] Generate a p12 file additionally to a PEM file (PEM_GENERATE_P12_FILE)
–active_days_limit STRING If the current certificate is active for less than this number of days, generate a new one. Default value is 30 days (PEM_ACTIVE_DAYS_LIMIT)
–force [VALUE] Create a new push certificate, even if the current one is active for 30 (or PEM_ACTIVE_DAYS_LIMIT) more days (PEM_FORCE)
-s, –save_private_key [VALUE] Set to save the private RSA key (PEM_SAVE_PRIVATEKEY)
-a, –app_identifier STRING The bundle identifier of your app (PEM_APP_IDENTIFIER)
-u, –username STRING Your Apple ID Username (PEM_USERNAME)
-b, –team_id STRING The ID of your Developer Portal team if you’re in multiple teams (PEM_TEAM_ID)
-l, –team_name STRING The name of your Developer Portal team if you’re in multiple teams (PEM_TEAM_NAME)
-p, –p12_password STRING The password that is used for your p12 file (PEM_P12_PASSWORD)

签名

sign可以创建,更新,下载和修复 provisioning profiles(使用一个命令)。
它支持App Store,Ad Hoc,开发和企业配置文件,并支持好的特性,如自动添加所有测试设备。

1
2
3
4
5
6
7
8
下载最新的应用配置文件
更新配置配置文件,当它已过期
修复配置配置文件,当它坏了
创建新的配置配置文件(如果尚不存在)
支持App Store,Ad Hoc和Development Profile
支持多个Apple帐户,将您的凭据安全地存储在钥匙串中
支持多个团队(multiple Teams)
支持企业应用配置(Enterprise Profiles)

进入工程目录后

1
fastlane sigh #默认会在当前文件夹下生成针对appstore的`.mobileprovision`
命令 描述
fastlane sigh App Store profile 或 InHouse profile
fastlane sigh -a com.krausefx.app -u username 指定bundle identifier 和 username(apple id)
fastlane sigh –adhoc 生成Ad Hoc profile
fastlane sigh –development 生成 development profile
fastlane sigh -o “./Certificates/“ 指定输出文件夹
fastlane sigh download_all 下载所有的 provisioning profiles
fastlane sigh –help 列出帮助

高级使用

命令 描述
fastlane sigh –skip_install 默认情况下,sigh将下载的配置文件安装在您的机器上。 如果只想生成配置文件并跳过安装,使用该命令
fastlane sigh -a com.krausefx.app -u username -q “myProfile.mobileprovision” 用特定的名字保存,使用 -q
fastlane sigh –skip_certificate_verification 跳过验证安装在本机上的 code signing identity
fastlane sigh –force 如果您需要更新配置配置文件,而不管其状态如何,请使用–force选项。这给您一个具有最大生命周期的配置文件。 –force还会将所有可用的设备添加到此配置文件中。
fastlane sigh –help 查看帮助:列出可用参数和命令

结合fastlane 使用

1
2
3
4
lane :beta do
cert
sigh(force: true)
end

force:true将确保在每次运行时重新生成配置配置文件。 这将使得sign总是使用正确的签名证书(signing certificate),证书将会被安装到本机上。

所有参数

命令 描述
–adhoc [VALUE] Setting this flag will generate AdHoc profiles instead of App Store Profiles (SIGH_AD_HOC)
–development [VALUE] Renew the development certificate instead of the production one (SIGH_DEVELOPMENT)
–skip_install [VALUE] By default, the certificate will be added to your local machine. Setting this flag will skip this action (SIGH_SKIP_INSTALL)
-f, –force [VALUE] Renew provisioning profiles regardless of its state - to automatically add all devices for ad hoc profiles (SIGH_FORCE)
-a, –app_identifier STRING The bundle identifier of your app (SIGH_APP_IDENTIFIER)
-u, –username STRING Your Apple ID Username (SIGH_USERNAME)
-b, –team_id STRING The ID of your Developer Portal team if you’re in multiple teams (SIGH_TEAM_ID)
-l, –team_name STRING The name of your Developer Portal team if you’re in multiple teams (SIGH_TEAM_NAME)
-n, –provisioning_name STRING The name of the profile that is used on the Apple Developer Portal (SIGH_PROVISIONING_PROFILE_NAME)
–ignore_profiles_with_different_name [VALUE] Use in combination with :provisioning_name - when true only profiles matching this exact name will be downloaded(SIGH_IGNORE_PROFILES_WITH_DIFFERENT_NAME)
-o, –output_path STRING Directory in which the profile should be stored(SIGH_OUTPUT_PATH)
-i, –cert_id STRING The ID of the code signing certificate to use (e.g. 78ADL6LVAA) (SIGH_CERTIFICATE_ID)
-c, –cert_owner_name STRING The certificate name to use for new profiles, or to renew with. (e.g. “Felix Krause”) (SIGH_CERTIFICATE)
-q, –filename STRING Filename to use for the generated provisioning profile (must include .mobileprovision) (SIGH_PROFILE_FILE_NAME)
-w, –skip_fetch_profiles [VALUE] Skips the verification of existing profiles which is useful if you have thousands of profiles (SIGH_SKIP_FETCH_PROFILES)
-z, –skip_certificate_verification [VALUE] Skips the verification of the certificates for every existing profiles. This will make sure the provisioning profile can be used on the local machine(SIGH_SKIP_CERTIFICATE_VERIFICATION)
-p, –platform [VALUE] Set the provisioning profile’s platform (i.e. ios, tvos) (SIGH_PLATFORM)
–template_name STRING The name of provisioning profile template. If the developer account has provisioning profile templates, template name can be found by inspecting the Entitlements drop-down while creating/editing a provisioning profile(SIGH_PROVISIONING_PROFILE_TEMPLATE_NAME)

自动打包

用法:

1
2
3
4
fastlane gym
#fastlane gym --export_method ad-hoc
#fastlane gym --export_method enterprise
#fastlane gym --export_method app-store

当然可以增加一些配置:

1
2
fastlane gym --workspace "Example.xcworkspace" --scheme "AppName" --clean

选择Xcode:

1
2
DEVELOPER_DIR="/Applications/Xcode6.2.app" gym

列出可用参数列表:

1
fastlane gym --help

运行遇到错误,使用verbose模式获取更多信息:

1
2
fastlane gym --verbose

如果您没有上传到App Store或TestFlight,请设置正确的导出方法:

1
2
fastlane gym --export_method ad-hoc

要使用bool参数,确保这么使用gym:

1
2
fastlane gym --include_bitcode true --include_symbols false

获取xcodebuild输出,请到~/Library/Logs/gym查看。

gym 的常用参数

Options for build:

命令 描述
-w, –workspace STRING Path to the workspace file (GYM_WORKSPACE)
-p, –project STRING Path to the project file (GYM_PROJECT)
-s, –scheme STRING The project’s scheme. Make sure it’s marked as Shared (GYM_SCHEME)
-c, –clean [VALUE] Should the project be cleaned before building it? (GYM_CLEAN)
-o, –output_directory STRING The directory in which the ipa file should be stored in (GYM_OUTPUT_DIRECTORY)
-n, –output_name STRING The name of the resulting ipa file (GYM_OUTPUT_NAME)
-q, –configuration STRING The configuration to use when building the app. Defaults to ‘Release’ (GYM_CONFIGURATION)
-a, –silent [VALUE] Hide all information that’s not necessary while building (GYM_SILENT)
-i, –codesigning_identity STRING The name of the code signing identity to use. It has to match the name exactly. e.g. ‘iPhone Distribution: SunApps GmbH’ (GYM_CODE_SIGNING_IDENTITY)
–skip_package_ipa [VALUE] Should we skip packaging the ipa? (GYM_SKIP_PACKAGE_IPA)
-m, –include_symbols [VALUE] Should the ipa file include symbols? (GYM_INCLUDE_SYMBOLS)
-z, –include_bitcode [VALUE] Should the ipa file include bitcode? (GYM_INCLUDE_BITCODE)
-j, –export_method STRING Method used to export the archive. Valid values are: app-store, ad-hoc, package, enterprise, development, developer-id (GYM_EXPORT_METHOD)
–export_options [VALUE] Specifies path to export options plist. Use ‘xcodebuild -help’ to print the full set of available options (GYM_EXPORT_OPTIONS)
–export_xcargs STRING Pass additional arguments to xcodebuild for the package phase. Be sure to quote the setting names and values e.g. OTHER_LDFLAGS=”-ObjC -lstdc++” (GYM_EXPORT_XCARGS)
–skip_build_archive [VALUE] Export ipa from previously built xarchive. Uses archive_path as source (GYM_SKIP_BUILD_ARCHIVE)
–build_path STRING The directory in which the archive should be stored in (GYM_BUILD_PATH)
-b, –archive_path STRING The path to the created archive (GYM_ARCHIVE_PATH)
-f, –derived_data_path STRING The directory where built products and other derived data will go (GYM_DERIVED_DATA_PATH)
-u, –result_bundle [VALUE] Location of the Xcode result bundle (GYM_RESULT_BUNDLE)
-l, –buildlog_path STRING The directory where to store the build log (GYM_BUILDLOG_PATH)
-k, –sdk STRING The SDK that should be used for building the application (GYM_SDK)
–toolchain [VALUE] The toolchain that should be used for building the application (e.g. com.apple.dt.toolchain.Swift_2_3, org.swift.30p620160816a) (GYM_TOOLCHAIN)
-d, –destination STRING Use a custom destination for building the app (GYM_DESTINATION)
-g, –export_team_id STRING Optional: Sometimes you need to specify a team id when exporting the ipa file (GYM_EXPORT_TEAM_ID)
-x, –xcargs STRING Pass additional arguments to xcodebuild for the build phase. Be sure to quote the setting names and values e.g. OTHER_LDFLAGS=”-ObjC -lstdc++” (GYM_XCARGS)
-y, –xcconfig STRING Use an extra XCCONFIG file to build your app (GYM_XCCONFIG)
-r, –suppress_xcode_output [VALUE] Suppress the output of xcodebuild to stdout. Output is still saved in buildlog_path (SUPPRESS_OUTPUT)
–disable_xcpretty [VALUE] Disable xcpretty formatting of build output (DISABLE_XCPRETTY)
–xcpretty_test_format [VALUE] Use the test (RSpec style) format for build output (XCPRETTY_TEST_FORMAT)
–xcpretty_formatter STRING A custom xcpretty formatter to use (XCPRETTY_FORMATTER)
–xcpretty_report_junit STRING Have xcpretty create a JUnit-style XML report at the provided path (XCPRETTY_REPORT_JUNIT)
–xcpretty_report_html STRING Have xcpretty create a simple HTML report at the provided path (XCPRETTY_REPORT_HTML)
–xcpretty_report_json STRING Have xcpretty create a JSON compilation database at the provided path (XCPRETTY_REPORT_JSON)
–analyze_build_time [VALUE] Analyze the project build time and store the output in ‘culprits.txt’ file (GYM_ANALYZE_BUILD_TIME)
–xcpretty_utf [VALUE] Have xcpretty us

至此打包基本完成!!!

参考文档

自动化工具Fastlane笔记三: snapshot自动截图基础+示例
无法录制
fastlane - snapshot 自动截图
小团队的自动化发布-Fastlane带来的全自动化发布
fastlane入门之snapshot
fastlane 之截图自动化
fastlane