使用 Fastlane 实现 iOS 跟 Android 自动打包脚本

对于一个iOS APP的发布上线,一般来说都需要经历: 编译打包
-> 截图
-> 填写一些说明文字
-> 上传ipa到itunes connect
-> 提交供审核
。每次都要进行这么多“繁琐”的步骤,对于某些步骤可能一次还不能执行成功需要等着界面提示上传错误然后手动重新再来一次(想想都觉得可怕)。
在日常开发中,打包也是最后上线不可缺少的环节,如果需要生成ipa文件通常需要在Xcode里点击Product
-> Archive
,然后在弹出来的 Organizer
中选择导出什么类型( ad hoc
/ enterprise
)的包。对于大项目来说动辄编译十分钟以上的来说,一天打几个包就差不多过去了。
为了解决这些问题, Felix Krause
大神写了一个工具集 fastlane
。fastlane 是一套使用Ruby写的自动化工具集,用于 iOS 和 Android 的自动化打包、发布等工作。

前言

最近用shell打包ipa发现终端总是提示:

shell error: exportArchive: "***.app" requires a provisioning profile.
把配置文件删除重新下载也一样,所以索性重新换用另一种脚本工具来打包ipa,发现这个还是挺好用的

介绍

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

  • Github: github.com/fastlane/fa…

    官网:fastlane.tools

    文档:docs.fastlane.tools

配置环境

  • 首先要安装正确的 Ruby 版本。在终端窗口中用下列命令来确认: ruby -v

  • 如果没有安装,则输入命令安装 gym: sudo gem install gym

  • 确保Xcode命令行工具安装最新版本,使用如下命令进行安装: xcode-select --install

  • 以上依赖配置好之后就可以通过 rubygem 进行安装 fastlane: sudo gem install fastlane

  • 完成安装

fastlane实战

初始化

  • 打开终端,cd到你的工程目录,然后执行 fastlane init 命令开始初始化

  • 在执行的过程中会要求填写一些项目的资料,如 Apple ID 等,fastlane 会自动检测当前目录下项目的 App Name 和 App Identifier,可以选择自行输入这些信息。初始化完成会在当前目录下面生成一个fastlane的文件夹。

  • 最重要的两个文件就是 Appfile 和 Fastfile,主要的说明如下

  • Appfile里面存放了App的基本信息包括 app_identifier、apple_id、team_id 等,如果在 init 的时候输入了正确的 apple_id 和密码会自动获取 team_id。

  • Fastfile 是最重要的一个文件,在这个里面可以编写和定制我们的自动化脚本,所有的流程控制功能都写在这个文件里面。

fastfile 文件

FASTFILE 管理你所创建的 LANE ,了解详情。它的格式是这样的:

<pre class="prettyprint" style="box-sizing: inherit; font-family: Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 16px; overflow: auto; max-width: 100%; margin: 0px 0px 30px; padding: 20px 25px; white-space: pre-wrap; word-wrap: break-word; border: 0px; border-radius: 3px; background-color: rgb(244, 246, 249); line-height: 1.875; color: rgb(112, 112, 123); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">desc "企业版"
lane :inHouse do
gym(scheme: "XXX",
export_method:"enterprise",
output_directory "./build", # 打包后的 ipa 文件存放的目录
output_name "XXX" # ipa 文件名
)
end
复制代码</pre>

我的用法

fastfile 文件里主要修改四个地方内容

<pre class="prettyprint" style="box-sizing: inherit; font-family: Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 16px; overflow: auto; max-width: 100%; margin: 0px 0px 30px; padding: 20px 25px; white-space: pre-wrap; word-wrap: break-word; border: 0px; border-radius: 3px; background-color: rgb(244, 246, 249); line-height: 1.875; color: rgb(112, 112, 123); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">platform :ios do
desc "ad_Hoc 版本"
lane :beta do
gym(scheme: “项目名称”, export_method:"app-store",output_directory: "./build",)
</pre>

我一般用gym语法操作

<pre class="prettyprint" style="box-sizing: inherit; font-family: Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 16px; overflow: auto; max-width: 100%; margin: 0px 0px 30px; padding: 20px 25px; white-space: pre-wrap; word-wrap: break-word; border: 0px; border-radius: 3px; background-color: rgb(244, 246, 249); line-height: 1.875; color: rgb(112, 112, 123); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">gym(scheme: scheme_name, clean: true, export_method:'appstore', configuration: configuration, output_directory: output_directory, output_name: output_name)
复制代码</pre>

结果

这次我只是用来打包测试,fastlane里的源码:

<pre class="prettyprint" style="box-sizing: inherit; font-family: Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 16px; overflow: auto; max-width: 100%; margin: 0px 0px 30px; padding: 20px 25px; white-space: pre-wrap; word-wrap: break-word; border: 0px; border-radius: 3px; background-color: rgb(244, 246, 249); line-height: 1.875; color: rgb(112, 112, 123); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># This file contains the fastlane.tools configuration

You can find the documentation at https://docs.fastlane.tools

For a list of all available actions, check out``

https://docs.fastlane.tools/actions

For a list of all available plugins, check out

https://docs.fastlane.tools/plugins/available-plugins

Uncomment the line if you want fastlane to automatically update itself

update_fastlane

指定打包所使用的输出方式,目前支持 app-store, package, ad-hoc, enterprise, development, 和developer-id

default_platform(:ios)

platform :ios do
desc "ad_Hoc 版本"
lane :beta do
gym(scheme: "***",
export_method:"ad-hoc",
output_directory: "./build",#文件路径
)
end
end
复制代码</pre>

接下来我们开始进阶教程,将打包好的 ipa 上传到 fir
在Fastfile文件里写入:

<pre class="prettyprint" style="box-sizing: inherit; font-family: Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 16px; overflow: auto; max-width: 100%; margin: 0px 0px 30px; padding: 20px 25px; white-space: pre-wrap; word-wrap: break-word; border: 0px; border-radius: 3px; background-color: rgb(244, 246, 249); line-height: 1.875; color: rgb(112, 112, 123); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># This is the minimum version number required.

Update this, if you use features of a newer version

指定打包所使用的输出方式,目前支持 app-store, package, ad-hoc, enterprise, development, 和developer-id

default_platform :ios

platform :ios do

desc "开始打包-内测版--开发证书 - dev"

内测版--开发证书

lane :adhoc do

开始打包

puts "开始打包-内测版--开发证书 - dev"

gym(
export_method:"ad-hoc",
output_directory:"/Users/weiyuxiang/Desktop/Order/build",# 打包后的 ipa 文件存放的目录
)

使用fir-cli上传ipa

sh "fir publish /Users/weiyuxiang/Desktop/Order/build/***.ipa -T fir的token"
end

desc "开始打包 -- 企业公测版--hoc"
lane :inhoc do
gym(
export_method:"app-store",
output_directory:"/Users/weiyuxiang/Desktop/Order/build",
)

使用fir-cli上传ipa

sh "fir publish /Users/weiyuxiang/Desktop/Order/build/***.ipa -T fir的token"
end

end
复制代码</pre>

在测试的时候用

fastlane adhoc

上架则用

fastlane inhoc

到这里一般没有问题,但是楼主我还会遇到一个问题,最后用fir上传的时候会报这些错误:

<pre class="prettyprint" style="box-sizing: inherit; font-family: Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 16px; overflow: auto; max-width: 100%; margin: 0px 0px 30px; padding: 20px 25px; white-space: pre-wrap; word-wrap: break-word; border: 0px; border-radius: 3px; background-color: rgb(244, 246, 249); line-height: 1.875; color: rgb(112, 112, 123); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">/Users/weiyuxiang/.rvm/rubies/ruby-2.2.4/lib/ruby/gems/2.2.0/gems/fastlane-2.95.0/fastlane_core/lib/fastlane_core/ui/interface.rb:153:in shell_error!': [!] Exit status of command 'fir publish /Users/weiyuxiang/Desktop/Order/build/***.ipa -T a5bd6574b7292220d5c4b44b6' was 1 instead of 0\. (FastlaneCore::Interface::FastlaneShellError) /Users/weiyuxiang/.rvm/gems/ruby-2.2.4@global/gems/bundler-1.16.1/lib/bundler/rubygems_integration.rb:458:inblock in replace_bin_path': can't find executable fir for gem fir-cli. fir-cli is not currently included in the bundle, perhaps you meant to add it to your Gemfile? (Gem::Exception)
from /Users/weiyuxiang/.rvm/gems/ruby-2.2.4@global/gems/bundler-1.16.1/lib/bundler/rubygems_integration.rb:478:in block in replace_bin_path' from /Users/weiyuxiang/.rvm/gems/ruby-2.2.4/bin/fir:22:in<main>'
from /Users/weiyuxiang/.rvm/gems/ruby-2.2.4/bin/ruby_executable_hooks:15:in eval' from /Users/weiyuxiang/.rvm/gems/ruby-2.2.4/bin/ruby_executable_hooks:15:in<main>'
复制代码</pre>

按照错误提示,在 Gemfile 文件里加一句: gem "fir"
即可

Fastlane 能做的事情还有很多,大家可以去好好看看Fastlane文档,研究一些高级的用法吧!

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容