https://docs.fastlane.tools/ (有详细教程)
查看本机已安装的ruby版本号,要求版本>2.0
$ ruby -v
显示
ruby 2.0.0p648 (2015-12-16 revision 53162) [universal.x86_64-darwin16]
安装rvm
$ curl -L https://get.rvm.io | bash -s stable
使用rvm
$ source /Users/dayku/.rvm/scripts/rvm
列出ruby可安装版本列表
$ rvm list known
安装ruby 2.4
$ rvm install 2.4
安装过程中会检查系统是否有安装Homebrew,按照默认路径安装Homebrew,输入回车键
使用ruby 2.4
$ rvm use 2.4.1 --default
安装Command Line Tools
$ xcode-select --install
如果没有安装则自动安装,否则会显示一下错误
xcode-select: error: command line tools are already installed, use "Software Update" to install updates
①、安装JDK1.8.0
要保持和项目里使用的JDK版本一致
②、配置ANDROID_HOME全局参数
$ vi ~/.bash_profile
ANDROID_HOME=/Users/dayku/Library/Android/sdk
export ANDROID_HOME
export PATH=$PATH:$ANDROID_HOME/tools
export PATH=$PATH:$ANDROID_HOME/platform-tools
$ source ~/.bash_profile
$ [sudo] gem install fastlane -NV
查看安装的fastlane版本
$ fastlane --version
显示
fastlane installation at path:
/Users/xiaonan/.rvm/gems/ruby-2.4.1/gems/fastlane-2.94.0/bin/fastlane
-----------------------------
[✔] 🚀
fastlane 2.94.0
首先在终端进去项目根目录
然后初始化fastlane
$ fastlane init
期间会询问项目的包名
Package Name (com.krausefx.app): ***.***.***
询问上传到Google Play需要的json文件路径,直接回车
Path to the json secret file:
询问是否管理并下载Google Play元数据及截图,选n
Download existing metadata and setup metadata management? (y/n)
之后初始化命令会自动执行bundle update
下载项目需要的依赖库
初始化完成后,项目的根目录会多2个文件:Gemfile、Gemfile.lock,内容和iOS项目创建的类似类型
初始化自动在根目录创建fastlane文件夹及文件
$ cd fastlane && ls
Appfile Fastfile
- Appfile文件记录App的json_key_file,package_name信息。
- Fastfile脚本的核心执行文件.有几个默认的选项,可直接使用。
通过该命令安装插件
$ fastlane add_plugin pgyer
安装程序会自动在./fastlane/
下创建Pluginfile文件,文件内容:
# Autogenerated by fastlane
#
# Ensure this file is checked in to source control!
gem 'fastlane-plugin-pgyer'
安装期间判断项目如果没有设置过plugins,会询问是否允许修改Gemfile文件,选择y 修改之后的Gemfile内容
source "https://rubygems.org"
gem "cocoapods", "1.5.0"
gem "fastlane", "2.94.0"
plugins_path = File.join(File.dirname(__FILE__), 'fastlane', 'Pluginfile')
eval_gemfile(plugins_path) if File.exist?(plugins_path)
在新机器上安装plugins, 用于在从git上拉取包含fastlane配置文件的代码后使用
$ fastlane install_plugins
只有Android项目需要安装
$ fastlane add_plugin analyze_apk
通过该命令查找插件,注意在没有安装过plugins时,会出错
$ fastlane search_plugins
default_platform(:android)
ENV["JG_ANDROID_SDK_DIR"] = %x( echo $ANDROID_HOME ).chomp
platform :android do
desc '构建前的准备工作'
desc '这是一个私有任务,仅供 Fastfile 内部 lane 调用使用'
private_lane :prepare do
# 拉取最新代码
git_pull
end
desc '构建后的准备工作'
desc "钉钉机器人用来发送下载地址通知给测试群"
private_lane :dingdingtalk do |options|
#
app_version = lane_context[SharedValues::ANALYZE_APK_VERSION_NAME]
app_build_version = lane_context[SharedValues::ANALYZE_APK_VERSION_CODE]
app_name = lane_context[SharedValues::ANALYZE_APK_APP_NAME]
UI.message("Start get app information from pgyer...")
if options[:type] == "flavorRelease"
params = {
"_api_key" => ""**************************",",
"appKey" => ""**************************","
}
app_desc = "内测版"
elsif options[:type] == "flavorTest"
params = {
"_api_key" => ""**************************",",
"appKey" => ""**************************","
}
elsif options[:type] == "flavorPro"
app_desc = "预生产版"
params = {
"_api_key" => ""**************************",",
"appKey" => ""**************************","
}
end
UI.message("测试")
# 获取App详细信息
response = Net::HTTP.post_form URI('https://www.pgyer.com/apiv2/app/view'), params
result = JSON.parse(response.body)
UI.message(result)
status_code = result["code"]
status_message = result["message"]
if status_code != 0
UI.error('pgyer error message: ' + status_message)
return
end
UI.success("Successfully get app information from pgyer!")
app_url = "https://www.pgyer.com/" + result["data"]["buildShortcutUrl"]
app_icon = "https://www.pgyer.com/image/view/app_icons/" + result["data"]["buildIcon"] + "/144"
dingTalk_url = "https://oapi.dingtalk.com/robot/send?access_token=2d7f5962a839b2f9503db11c12d6b20c21349206a9e86e8550edcd6d05a43260"
markdown =
{
msgtype: "link",
link: {
text: "已经更新啦,快来试试吧!",
title: "Android #{app_name} #{app_version} (#{app_build_version}) #{app_desc}",
picUrl: "#{app_icon}",
messageUrl: "#{app_url}"
}
}
uri = URI.parse(dingTalk_url)
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
request = Net::HTTP::Post.new(uri.request_uri)
request.add_field('Content-Type', 'application/json')
request.body = markdown.to_json
response = https.request(request)
json = JSON.parse(response.body)
if json["errcode"] != 0
UI.error('ding talk error message: ' + json["errmsg"])
end
UI.success("Successfully send link message to ding talk!")
end
desc "拉取Git最新代码,打包APK,提交到蒲公英"
lane :submit_to_pgy do |options|
if options[:flavor].nil?
UI.error('need flavor options')
end
prepare
gradle(task: "clean")
# gradle(task: "assembleRelease")
gradle(task:'assemble', flavor:options[:flavor], build_type:'Release')
analyze_apk(
android_home: ENV["JG_ANDROID_SDK_DIR"],
build_tools_version: '26.0.3',
apk_path: lane_context[SharedValues::GRADLE_APK_OUTPUT_PATH]
)
pgyer(
api_key: "**************************",
user_key: ""**************************",",
update_description: "android#{lane_context[SharedValues::ANALYZE_APK_APP_NAME]}内部测试版本发布 by fastlane"
)
dingdingtalk(type:options[:flavor])
end
end
$ fastlane android submit_to_pgy flavor:flavorTest
安装之后默认会安装一个命令行工具 fastlane,利用它可以初始化、执行任务、查看任务定义、查看可用的动作和动作的详细定义,甚至可以用它来创建自定义的动作、插件以及一些辅助功能。想了解的话可以先看看它的帮助:
$ fastlane --help
fastlane
CLI for 'fastlane' - The easiest way to automate building and releasing your iOS and Android apps
Run using `fastlane [platform] [lane_name]`
To pass values to the lanes use `fastlane [platform] [lane_name] key:value key2:value2`
Commands:
action Shows more information for a specific command
actions Lists all available fastlane actions
add_plugin Add a new plugin to your fastlane setup
disable_crash_reporting Deprecated: fastlane doesn't use a crash reporter any more
docs Generate a markdown based documentation based on the Fastfile
enable_auto_complete Enable tab auto completion
enable_crash_reporting Deprecated: fastlane doesn't use a crash reporter any more
help Display global or [command] help documentation
init Helps you with your initial fastlane setup
install_plugins Install all plugins for this project
lanes Lists all available lanes and shows their description
list Lists all available lanes without description
new_action Create a new custom action for fastlane.
new_plugin Create a new plugin that can be used with fastlane
run Run a fastlane one-off action without a full lane
search_plugins Search for plugins, search query is optional
trigger Run a sepcific lane. Pass the lane name and optionally the platform first.
update_plugins Update all plugin dependencies
Global Options:
--verbose
-h, --help Display help documentation
-v, --version Display version information
Author:
Felix Krause <[email protected]>
Website:
https://fastlane.tools
GitHub:
https://github.com/fastlane/fastlane
生命周期
执行顺序 | 方法名 | 说明 |
---|---|---|
1 | before_all | 在执行 lane 之前只执行一次 |
2 | before_each | 每次执行 lane 之前都会执行一次 |
3 | lanes | 自定义的任务 |
4 | after_each | 每次执行 lane 之后都会执行一次 |
5 | after_all | 在执行 lane 成功结束之后执行一次 |
6 | error | 在执行上述情况任意环境报错都会中止并执行一次 |
正常情况下你可能只会是用到一种任务方法 lane 但其实它会包含很多中高级用法。
定义任务的方法类似于 rake 的 task,但使用上缺比前者要好用很多,见下表:
定义 | 是否必须 | 说明 | 备注 |
---|---|---|---|
desc | false | 方法描述 | 可多次使用打到换行的目的 |
name | true | 方法名 | 符号化的方法名 |
options | false | 方法参数 | 返回 Hash 类型 |
task | true | 方法主体 | 参考 ruby 的方法代码且支持 ruby 代码 |