文章目录
  1. 1. 安装
  2. 2. 初始化
  3. 3. Appfile
  4. 4. Fastfile

上一篇文章讲解了利用python封装脚本进行自动化打包,这两天又看了看github上很火的自动化打包工具Fastlane

Fastlane是一套使用Ruby写的自动化工具集,用于iOS和Android的自动化打包、发布等工作,可以节省大量的时间。 官方文档看这里

安装

  • 安装Xcode命令行工具:
1
xcode-select --install
  • 查看ruby版本,要求2.0及以上版本
1
2
3
4
5
6
7
8
9
ruby -v

ruby的镜像文件路径改为https://gems.ruby-china.org/

gem sources --remove https://ruby.taobao.org/

gem sources --add https://rubygems.org

gem sources -l
  • 安装:
1
sudo gem install fastlane

初始化

切换项目目录到包含xxx.xcodeproj的项目目录下输入:

1
fastlane init

输出:

1
2
3
4
5
6
[15:21:56]: What would you like to use fastlane for?
1. 📸 Automate screenshots
2. 👩‍✈️ Automate beta distribution to TestFlight
3. 🚀 Automate App Store distribution
4. 🛠 Manual setup - manually setup your project to automate your tasks
?

这四个选项的意思是:

1.自动截屏。这个功能能帮我们自动截取APP中的截图,并添加手机边框(如果需要的话),我们这里不选择这个选项,因为我们的项目已经有图片了,不需要这里截屏。

2.自动发布beta版本用于TestFlight,如果大家有对TestFlight不了解的,可以参考王巍写的这篇文章

3.自动的App Store发布包。我们的目标是要提交审核到APP Store,按道理应该选这个,但这里我们先不选,因为选择了以后会需要输入用户名密码,以及下载meta信息,需要花费一定时间,这些数据我们可以后期进行配置。

4.手动设置。

选择第四个后一路回车即可(等待时间略长),结束后会看到生成了fastlane目录,该目录包含Appfile和Fastfile;同时还生成了两个文件Gemfile和Gemfile.lock,是和fastlane文件夹在同一个目录。

Appfile

Appfile用来存放app_identifier,apple_id和team_id。文件生成的时候会定义好格式,按格式填写即可。

1
2
app_identifier("[[xxxx]]") # The bundle identifier of your app
apple_id("[[xxxx]]") # Your Apple email address

你也可以为每个lane提供不同的 app_identifier, apple_id 和 team_id,例如:

1
2
3
4
5
for_lane :inhouse do
app_identifier "xxxx"
apple_id "xxxx"
team_id "xxxx"
end

这里就是为Fastfile中定义的inhouse这个lane设置单独的信息。

Fastfile

Fastfile管理你所创建的 lane 。

  • scan 自动化测试工具,很好的封装了 Unit Test
  • sigh 针对于 iOS 项目开发证书和 Provision file 的下载工具
  • match 同步团队每个人的证书和 Provision file 的超赞工具
  • gym 针对于 iOS 编译打包生成 ipa 文件
  • deliver 用于上传应用的二进制代码,应用截屏和元数据到 App Store
  • snapshot 可以自动化iOS应用在每个设备上的本地化截屏过程

这里我们主要用gym来打包。

Fastlane内部的工具不是新写的,而是调用mac本身的命令,只不过是实现了自动化而已。比如gym工具只是xcodebuild工具的一个封装,如果你会xcodebuild,那gym对你来说小菜一碟。xcodebuild的使用可以看这篇文章

找了一个比较全的格式,可以参考:

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
# 指定 fastlane 最小版本
fastlane_version "2.20.0"

# 指定当前平台,可以设置为 ios 、android、mac
default_platform :ios

platform :ios do

# 在执行每一个 lane 之前都先执行这个代码
before_all do
end

# 定义一个创建测试包的 lane
# 我们调用的命令就是调用 fastlane 的 lane
lane :buildDebugApp do |op|
# 根据输入的版本设置项目 version number (我们初始化 fastlane 的时候是在 .xcworkspace 目录下, 而我们的项目中 ,.xcworkspace 和 .xcodeproj 不在同一级目录,这里的“increment_version_number”需要检测 .xcodeproj 项目文件,所以需要指定该文件的目录)
increment_version_number({xcodeproj: './HomeMate2_Trunk/HomeMate.xcodeproj', version_number: op[:version]})

# 根据输入的版本设置项目 build number (同上,也是需要指定 .xcodeproj 的目录)
increment_build_number({xcodeproj: './HomeMate2_Trunk/HomeMate.xcodeproj', build_number: op[:version]})

# 最重要的打包命令
gym(
export_method: 'ad-hoc', # 打包的方式,可设置为 appstore(默认),enterprise
scheme: "HomeMate", # 指定需要打那个 scheme 的包
workspace: "HMWorkSpac.xcworkspace", # 指定打包的项目文件
output_name: "HomeMate.ipa", # 打包输出名称
silent: true, # 隐藏不必要信息
clean: true, # 打包前是否 clean 项目
configuration: "Debug", # 配置为 debug 版本
buildlog_path: "./fastlanelog", # 日志输出目录
codesigning_identity: "iPhone Developer: Hailiang He (xxxxxxxxxx)", # 代码签名证书
output_directory: "/Users/xxx/Desktop" # ipa输出目录
)
end

# 在执行每一个 lane 之后执行该功能
after_all do |lane|
end

# 在执行每一个 lane 出错的时候执行该功能
error do |lane, exception|
end

end

上面的代码块包含了日常打包常用的功能,可以参考。

我这边测试打包的时候,写了一个最简单版本的Appfile和Fastfile文件,理解了这个最简单版本,在这个基础上继续增加功能即可,非常便于理解

Appfile文件:

1
2
app_identifier("[[xxx]]") # The bundle identifier of your app
apple_id("[[xxx]]") # Your Apple email address

Fastfile文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
default_platform(:ios)

platform :ios do
desc "Description of what the lane does"
lane :custom_lane do
# add actions here: https://docs.fastlane.tools/actions
gym(scheme: "xxx",
export_method:"ad-hoc",
output_directory:"./build", # 打包后的 ipa 文件存放的目录
output_name:"xxx.ipa" # ipa 文件名
)
end
end

export_method对应的打包类型:app-store、ad-hoc、development、enterprise。

编辑好以上内容,打开终端执行下面的命令,即可看到在当前目录下生成一个build文件夹,ipa包就在该文件夹中。

1
fastlane custom_lane

如果lane中设置了可以接收版本号,则可以执行:

1
fastlane custom_lane version:1.1.0
文章目录
  1. 1. 安装
  2. 2. 初始化
  3. 3. Appfile
  4. 4. Fastfile