使用Fastlane实现自动化打包

栏目: 服务器 · 发布时间: 6年前

内容简介:上一篇文章讲解了利用python封装脚本进行自动化打包,这两天又看了看github上很火的自动化打包工具Fastlane是一套使用Ruby写的自动化工具集,用于iOS和Android的自动化打包、发布等工作,可以节省大量的时间。 官方文档看切换项目目录到包含xxx.xcodeproj的项目目录下输入:

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

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

安装

  • 安装Xcode命令行工具:
xcode-select --install
  • 查看ruby版本,要求2.0及以上版本
ruby -v

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

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

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

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

初始化

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

fastlane init

输出:

[15:21:56]: What would you like to use fastlane for?
1.     Automate screenshots
2. :woman:‍:airplane:  Automate beta distribution to TestFlight
3. :rocket:  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。文件生成的时候会定义好格式,按格式填写即可。

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

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

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的使用可以看这篇文章。

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

# 指定 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文件:

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

Fastfile文件:

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包就在该文件夹中。

fastlane custom_lane

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

fastlane custom_lane version:1.1.0

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Hacking

Hacking

Jon Erickson / No Starch Press / 2008-2-4 / USD 49.95

While other books merely show how to run existing exploits, Hacking: The Art of Exploitation broke ground as the first book to explain how hacking and software exploits work and how readers could deve......一起来看看 《Hacking》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具