Fastlane:入门与实战
![](https://img.haomeiwen.com/i629857/c5080dad552ec205.png)
Fastlane Github地址
Fastlane Tools
1. Fastlane简介:
The easiest way to automate building and releasing your iOS and Android apps.
Fastlane 化繁为简,只需一条命令就可实现从 Xcode 项目到 编译\打包\构建\提交审核。
2. Fastlane 安装
官网安装教程直达链接
首先,确保你已经安装了最新的 Xcode command line tools
xcode-select --install
由于 masOS 新的文件权限限制,如果在网上搜到的安装命令不成功,尝试添加-n /usr/local/bin
$ sudo gem install -n /usr/local/bin fastlane
Password:
Successfully installed fastlane-2.35.1
Parsing documentation for fastlane-2.35.1
Done installing documentation for fastlane after 10 seconds
1 gem installed
检查 Fastlane 版本
➜ ~ fastlane --version
fastlane installation at path:
/usr/local/Cellar/ruby/2.4.1/lib/ruby/gems/2.4.0/gems/fastlane-2.35.1/bin/fastlane
-----------------------------
fastlane 2.35.1
安装成功
3. 开始使用 Fastlane
在终端中 cd 到你的工程的根目录,然后输入命令
fastlane init
如果是第一次使用 fastlane ,会要求输入你的苹果开发者账号
cd 项目目录
fastlane init
[17:29:05]: Your Apple ID (e.g. fastlane@krausefx.com): 你的开发者AppleID
[17:29:40]: Verifying that app is available on the Apple Developer Portal and iTunes Connect...
[17:29:40]: Starting login with user 'xxxxx'
+----------------+--------------------------------------+
| Detected Values |
+----------------+--------------------------------------+
| Apple ID | xxxx |
| App Name | xxxx |
| App Identifier | xxxxx |
| Workspace | xxxxx |
+----------------+--------------------------------------+
[17:29:52]: Please confirm the above values (y/n)
...
// init 成功
[17:31:26]: Successfully finished setting up fastlane
//如果已经 init 过,就是下面的内容
[17:24:56]: fastlane is already set up at path ./fastlane/
- 期间会让你输入 Apple ID 账号密码(这个信息会存在钥匙串中,后续使用无需再输入密码)
- 会检测当前的 app identifier 是否在 Apple Dev Center 中
- 会检测当前 app 是否在 iTunes Connect 中
- 如果已经在 Apple Dev Center 和 iTunes Connect 中创建相应的信息,那么过程会很顺利
成功之后,会在你工程的根目录下创建fastlane
文件夹,里面都是从 iTunes Connect 获取到的当前 App 的相关信息。
你最感兴趣的文件应该是fastlane/Fastfile
,因为它包含了你分发你的 APP 的所有信息。
3.1 使用 fastlane 部署 iOS 测试
3.1.2 打包 APP
通过 fastlane 的工具 gym
实现自动打包,修改之前提到的文件fastlane/Fastfile
,添加以下内容
lane :beta do
gym(scheme: "MyApp")
end
另外,你也可以添加一些打包选项,如:
lane :beta do
gym(scheme: "MyApp",
workspace: "Example.xcworkspace",
include_bitcode: true)
end
通过运行下面的命令执行上面的lane
,实现自动打包
fastlane beta
如果一切 OK,会在当前目录下创建[ProductName].ipa
了解更多gym
用法,通过命令
fastlane action gym
或者访问官网 fastlane/gym
3.1.2 使用 Fastlane 上传 App 到蒲公英
官网有一篇教程,可是我按照这个教程没有成功过,Google 了大半天还是没搞定,就在从入门到放弃的时候,看到了蒲公英在 Github 上的介绍,终于解决了问题。我的解决办法如下:
- 首先在终端中
cd
到你的Project
根目录,然后通过以下命令为你的Project
安装蒲公英Fastlane
插件
fastlane add_plugin pgyer
[11:13:24]: Make sure to commit your Gemfile, Gemfile.lock and Pluginfile to version control
Installing plugin dependencies...
Successfully installed plugins
- 成功后就可以修改
Fastlane
文件,关键代码如下:
default_platform :ios
platform :ios do
before_all do
# ENV["SLACK_URL"] = "https://hooks.slack.com/services/..."
cocoapods(use_bundle_exec: false)
end
desc " lane 描述信息:通过蒲公英 fastlane 插件实现上传 APP 到蒲公英"
lane :beta do
gym(scheme:"xxx",
workspace: "xxx.xcworkspace",
export_method: "ad-hoc")
pgyer(api_key: "从蒲公英项目详情中获取的 apikey",
user_key: "从蒲公英项目详情中获取的 userkey",
update_description: "本次测试更新的文字说明")
end
- 最后,运行下面命令,实现自动打包上传 APP 到蒲公英。
fastlane beta
- 成功的信息如下
[11:23:58]: Successfully exported and compressed dSYM file
# 成功打包
[11:23:58]: Successfully exported and signed the ipa file:
[11:23:58]: /Users/xxxx/Documents/ComponyProject/xxx/xxx/trunk/xxx/xxx.ipa
[11:23:58]: -------------------
[11:23:58]: --- Step: pgyer ---
[11:23:58]: -------------------
[11:23:58]: The pgyer plugin is working.
[11:23:58]: build_file: /Users/xxx/Documents/ComponyProject/xxx/xxxx/trunk/xxxx/xxx.ipa
[11:23:58]: Start upload /Users/xxx/Documents/ComponyProject/xxx/xxx/trunk/xxx/xxx.ipa to pgyer...
# 成功上传到蒲公英,打开下载连接查看
[11:24:02]: Upload success. Visit this URL to see: https://www.pgyer.com/xxxx
+------+---------------------+-------------+
| fastlane summary |
+------+---------------------+-------------+
| Step | Action | Time (in s) |
+------+---------------------+-------------+
| 1 | Verifying required | 0 |
| | fastlane version | |
| 2 | default_platform | 0 |
| 3 | cocoapods | 4 |
| 4 | gym | 148 |
| 5 | pgyer | 4 |
+------+---------------------+-------------+
[11:24:02]: fastlane.tools finished successfully 🎉
总结:
- 自己犯得第一个错误是没有安装蒲公英插件到当前工程,而是直接在终端中输入了
fastlane add_plugin pgyer
,以为这个插件是全局的。官方的文档也没有很明确的说出这一点,难道是这个git:(master)
?
[图片上传失败...(image-a17539-1511503365894)]
GitHub 倒是很明确的说明了
This project is a fastlane plugin. To get started with fastlane-plugin-pgyer
, add it to your project by running:
fastlane add_plugin pgyer
- 官网教程是通过
bundle
实现的。但是通过命令bundle exec
老是卡死在cocoapods
上,Google 了半天也没解决(自己对Ruby
不熟悉,Gemfile
,Gemfile.lock
添加了gem 'cocoapods'
,gem 'fastlane'
还是无效,懒得深入了)
Fastlane failed to run pod install
解决方法是:在Fastlane
文件修改如下:
before_all do
# ENV["SLACK_URL"] = "https://hooks.slack.com/services/..."
# 如果是 cocoapods 尝试改为下面内容
cocoapods(use_bundle_exec: false)
end
- 蒲公英的进阶用法请参考官网,比如加密发布。
参考: