一、安装Fastlane
需要先安装了 brew,打开终端,输入以下命令
brew install fastlane
如果是 M1 电脑,输入上面的命令有可能没办法安装,需要按照提示输出以下命令,非 M1 电脑可以忽略这段
arch -arm64 brew install fastlane
安装完毕后,查看版本号确认已安装完成
fastlane -v
fastlane installation at path:
/opt/homebrew/Cellar/fastlane/2.182.0/libexec/gems/fastlane-2.184.0/bin/fastlane
-----------------------------
[✔] 🚀
fastlane 2.184.0
二、初始化 Fastlane
类似于 cocoapods,我们需要在每个项目中初始化 fastlane
在终端进入到项目所在文件夹中
fastlane init
接下来会出现4个选项,需要我们根据自己需求选择,这里我选择 4:手动设置,因为是发布到蒲公英。
[✔] 🚀
[✔] Looking for iOS and Android projects in current directory...
[15:14:57]: Created new folder './fastlane'.
[15:14:57]: Detected an iOS/macOS project in the current directory: 'moyu.xcodeproj'
[15:14:57]: -----------------------------
[15:14:57]: --- Welcome to fastlane 🚀 ---
[15:14:57]: -----------------------------
[15:14:57]: fastlane can help you with all kinds of automation for your mobile app
[15:14:57]: We recommend automating one task first, and then gradually automating more over time
[15:14:57]: 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
?
安装成功提示
[15:21:57]: --------------------------------------------------------
[15:21:57]: --- ✅ Successfully generated fastlane configuration ---
[15:21:57]: --------------------------------------------------------
之后我们会在项目文件夹中发现 fastlane 文件夹 和 Gemfile,说明初始化成功了。
如果初始化过程中,一直卡在
[15:17:36]: ------------------------------------------------------------
[15:17:36]: --- Setting up fastlane so you can manually configure it ---
[15:17:36]: ------------------------------------------------------------
[15:17:36]: Installing dependencies for you...
[15:17:36]: $ bundle update
control+C 取消当前操作,然后打开 Gemfile 文件,将
source "https://rubygems.org"
修改为:
source "https://gems.ruby-china.com"
然后在终端输入下面命令重新运行,等待更新
bundle update
三、配置 Fastlane 文件
打开 Fastlane 文件,根据自己项目的需要编辑相关内容
default_platform(:ios)
platform :ios do
desc "打包上传至蒲公英"
lane :build_to_pgyer do
# 需要打包的 target 名字
scheme = "xxxxx"
# 总共有4种打包方式选择,根据自己需求选择
# app-store :上架App Store
# ad-hoc :内部测试
# development :内部测试
# enterprise :企业证书-内部测试
export_method = "enterprise"
# 允许访问钥匙串(有可能会用到)
export_xcargs = "-allowProvisioningUpdates"
# 获取版本号、build,方便自动命名包名(根据需求添加)
version = get_info_plist_value(path: "./#{scheme}/Info.plist", key: "CFBundleShortVersionString")
build = get_info_plist_value(path: "./#{scheme}/Info.plist", key: "CFBundleVersion")
# ipa 包名称,可以根据自己需求改变下面的组合
output_name = "#{scheme}_#{version}_#{build}_#{Time.now.strftime('%Y%m%d%H%M%S')}.ipa"
# ipa 包导出路径
output_directory = "/Users/xxxx/Desktop/Fastlane"
# 蒲公英账号的 api_key 和 user_key
pgyer_api_key = "xxxxxxxxxxxxxxxxxxxx"
pgyer_user_key = "xxxxxxxxxxxxxxxxxxx"
# 打包配置
build_app(
scheme: scheme,
export_method: export_method,
export_xcargs: export_xcargs,
output_directory: output_directory,
output_name: output_name,
export_options: {
# provisioningProfiles是手动配置证书,如果项目种选择了自动选择证书,则不需要添加这段
# 如果项目中包含了 extension ,需要额外配置证书的才会用到
provisioningProfiles: {
"com.xxxx.xxx" => "证书名称",
"com.xxxx.xxx.xxxx" => "证书名称"
}
}
)
# 上传到蒲公英
pgyer(api_key: pgyer_api_key, user_key: pgyer_user_key)
end
end
四、添加蒲公英插件:pgyer
注意,必须要在项目文件夹路径下添加,否则打包成功后,会上传失败。
中间会要求修改 Gemfile,输入 y 即可。
fastlane add_plugin pgyer
[✔] 🚀
[16:23:53]: fastlane detected a Gemfile in the current directory
[16:23:53]: However, it seems like you didn't use `bundle exec`
[16:23:53]: To launch fastlane faster, please use
[16:23:53]:
[16:23:53]: $ bundle exec fastlane add_plugin pgyer
[16:23:53]:
[16:23:53]: Get started using a Gemfile for fastlane https://docs.fastlane.tools/getting-started/ios/setup/#use-a-gemfile
[16:23:55]: Plugin 'fastlane-plugin-pgyer' was added to './fastlane/Pluginfile'
[16:23:55]: It looks like fastlane plugins are not yet set up for this project.
[16:23:55]: fastlane will modify your existing Gemfile at path '/Users/xxxx/Desktop/moyu/Gemfile'
[16:23:55]: This change is necessary for fastlane plugins to work
[16:23:55]: Should fastlane modify the Gemfile at path
'/Users/xxxx/Desktop/moyu/Gemfile' for you? (y/n)
y
[16:23:58]: Successfully modified '/Users/xxxx/Desktop/moyu/Gemfile'
看到上面的信息,说明已经成功安装了pgyer 插件
五、准备工作完毕,开始打包
输入以下命令,这里的 build_to_pgyer
就是 fastlane 文件中的方法名,之后可以根据自己的需求,多添加几个不同的方法,用于打包不同名字的、不同环境的target。
fastlane build_to_pgyer
这个时候喝口水,就可以看到下面的信息
[16:35:29]: Successfully exported and compressed dSYM file
[16:35:29]: Successfully exported and signed the ipa file:
[16:35:29]: /Users//Desktop/Fastlane/moyu_1.0_1_20210528163516.ipa
[16:35:29]: -------------------
[16:35:29]: --- Step: pgyer ---
[16:35:29]: -------------------
[16:35:29]: The pgyer plugin is working.
[16:35:29]: build_file: /Users/xxxx/Desktop/Fastlane/moyu_1.0_1_20210528163516.ipa
[16:35:29]: Start upload /Users/xxxx/Desktop/Fastlane/moyu_1.0_1_20210528163516.ipa to pgyer...
[16:35:35]: Upload success. Visit this URL to see: https://www.pgyer.com/xxx
+------+----------------------+-------------+
| fastlane summary |
+------+----------------------+-------------+
| Step | Action | Time (in s) |
+------+----------------------+-------------+
| 1 | default_platform | 0 |
| 2 | get_info_plist_value | 0 |
| 3 | get_info_plist_value | 0 |
| 4 | build_app | 12 |
| 5 | pgyer | 6 |
+------+----------------------+-------------+
[16:35:35]: fastlane.tools finished successfully 🎉
至此,我们的打包已经完成。
到输出文件下,可以看到2个文件,一个是dSYM文件,一个是ipa包。
在上面的信息也可以获取到蒲公英的下载地址。
六、其他问题
一般会卡主的地方出现在证书选择上,特别是项目比较复杂包含了extension的,证书需要额外配置,这一步会卡比较久。
另外就是蒲公英插件的添加位置不要弄错。
对于项目种有多个target,或者多个不同包名、不同环境的,建议每个target对应一个 lane 方法,打包时可以分别打包,这样比较方面。
基本上 fastlane 可以做到一次配置,多次使用,非常的方便。