KnightSama‘s Blog

vuePress-theme-reco KnightSama    2021
KnightSama‘s Blog KnightSama‘s Blog

Choose mode

  • dark
  • auto
  • light
首页
分类
  • iOS
  • 集锦
  • JavaScript
  • Github
  • Python
标签
时间线
GitHub
author-avatar

KnightSama

27

文章

14

标签

首页
分类
  • iOS
  • 集锦
  • JavaScript
  • Github
  • Python
标签
时间线
GitHub
  • 使用 Fastlane 自动打包应用

    • 安装
      • 使用
        • 初始化
        • 配置
        • 执行
      • Lane
        • 参数传递
        • 调用
        • 返回值
        • before_all 与 before_each
        • after_all 与 after_each
        • error
      • Action
        • cocoapods
        • gym
      • Plugin
        • 上传至 fir.im
        • 上传到蒲公英

    使用 Fastlane 自动打包应用

    vuePress-theme-reco KnightSama    2021

    使用 Fastlane 自动打包应用


    KnightSama 2021-01-10 Fastlane

    Fastlane 是一套自动化打包工具,通过配置文件组合不同的工具实现自动化的打包发布流程,极大地节省了应用打包发布流程的时间

    # 安装

    通过 Homebrew 安装 Fastlane

    brew install fastlane
    
    1

    通过 RubyGems 安装 Fastlane

    sudo gem install fastlane -NV
    
    1

    # 使用

    # 初始化

    进入工程根目录,执行下面的指令

    fastlane init
    
    1

    运行后按照提示操作完成后会在根目录下创建下面的文件

    Root
    ├── fastlane
    │   ├── Appfile
    │   └── Fastfile
    └── Gemfile
    
    1
    2
    3
    4
    5
    • Gemfile - Ruby 的依赖管理文件,用来配置 fastlane 用到的三方组件如 CocoaPods
    • Appfile - 记录项目的关键信息,如包名、Apple ID、Team ID 等
    • Fastfile - 管理 lane 的文件,我们所有的操作都在这个文件中配置

    # 配置

    我们在工程目录下初始化 fastlane 后,需要根据需求来配置 Fastfile 文件完成自动化打包的操作,其中最重要的就是 lane 与 action。简单来说 lane 可以看做一个函数,里面包含打包所需要的操作,每个操作都可以看做一个 action,在执行时 fastlane 会依次调用 lane 中配置的 action 来完成打包等操作。

    lane :debug do
      # action1
      # action2
      # ...
    end
    
    1
    2
    3
    4
    5

    上面是一个简单的 lane 的结构,其中 :debug 是 lane 的名字,我们可以根据需要进行定义,一个 Fastfile 中可以包含多个这样的 lane,运行时通过名字来区分。

    # 执行

    当我们配置好 Fastfile 后就可以通过在工程目录下执行 fastlane [lane名称] 来运行指定的 lane 打包。例如上面的示例结构我们可以通过下面的脚本执行

    fastlane debug
    
    1

    # Lane

    # 参数传递

    在执行 lane 时我们还可以通过指令来传递参数

    # fastlane [lane] key1:value1 key2:value2 
    fastlane debug version:1.0.0 cocoapods:true
    
    1
    2

    在 lane 中我们通过 options 来接收使用传入的参数

    lane :debug do |options|
      # options[:key]
      version = options[:version] # 1.0.0
      if options[:cocoapods]
         # 只有 cocoapods 为 true 时才会执行这里
         ...
      end
    end
    
    1
    2
    3
    4
    5
    6
    7
    8

    # 调用

    跟函数之间的调用类似,一个 lane 可以调用其他的 lane。

    lane :release do |options|
      build(release: true)  # 调用 build 并传入参数
    end
    
    lane :debug do |options|
      build  # 调用 build 默认无参数
    end
    
    lane :build do |options|
      # 用指定的方式编译 app
      build_ios_app(scheme: (options[:release] ? "Release" : "Debug"))
    end
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12

    # 返回值

    lane 中最后一行代码的结果会被作为返回值,并且在一个 lane 中我们可以调用其他的 lane

    lane :debug do |options|
      result = add(value: 1)  # result 值为 2
    end
    
    lane :add do |options|
      options[:value] + 1  # 结果会作为返回值返回
    end
    
    1
    2
    3
    4
    5
    6
    7

    # before_all 与 before_each

    before_all 会在 lane 执行前执行一次

    before_all do |lane, options|
      # ...
    end
    
    1
    2
    3

    before_each 会在任意 lane 执行前执行。与 before_all 只执行一次不同,如果在一个 lane 中多次调用了其他的 lane,则其他的 lane 执行前都会执行 before_each 。

    before_each do |lane, options|
      # ...
    end
    
    1
    2
    3

    before_all 会先于 before_each 执行且仅执行一次,许多通用操作如从 git 拉取代码等可以放在 before_all 或 before_each 中

    # after_all 与 after_each

    after_all 会在 lane 执行完后执行一次

    after_all do |lane, options|
      # ...
    end
    
    1
    2
    3

    after_each 会在任意 lane 执行完后执行。同样如果在一个 lane 中多次调用了其他的 lane,则其他的 lane 执行完后都会执行 after_each 。

    after_each do |lane, options|
      # ...
    end
    
    1
    2
    3

    after_all 会在 after_each 执行完后执行且仅执行一次,许多通用操作如发送打包成功的通知等可以放在 after_all 或 after_each 中。

    注意

    如果执行过程中出现错误则会退出并执行 error, after_all 与 after_each 将不会执行

    # error

    error 会在 lane 执行出错后执行,在这里可以进行异常处理

    error do |lane, exception, options|
      # ...
    end
    
    1
    2
    3

    # Action

    除了通过 Ruby 代码在自定义 lane 中实现各种功能,fastlane 已经内置了许多写好的独立方法库也就是 action。每一个 action 都是一个 Ruby 脚本,它们是 fastlane 的最小执行单元。这些 action 基本上已经涵盖了打包过程中的各项操作。下面是 iOS 打包中几个常用 action

    # cocoapods

    cocoapods 会帮我们执行 pod install ,如果工程中使用了 CocoaPods 管理三方库会经常用到。使用 cocoapods 前需要在 Gemfile 中添加

    gem "cocoapods"
    
    1

    在 lane 中使用时直接调用

    lane :debug do
      cocoapods  # 执行 pod install
    end
    
    1
    2
    3

    # gym

    gym 可以根据配置参数编译 iOS 应用并生成 ipa 包。下面是 gym 中的常用参数

    参数名 描述 默认值
    workspace workspace 文件路径,使用 cocoapods 后需要使用
    project project 文件路径,若有 workspace 此项可忽略
    clean 是否在编译前 clean 工程 false
    configuration 编译设置,Release、Debug、自定义
    export_method 包输出类型 app-store、ad-hoc、package、enterprise、development
    output_directory 包输出路径
    output_name 包名称
    include_symbols 是否集成调试符号,若为 false 则会单独生成符号文件
    include_bitcode 是否开启 bitcode
    lane :debug do
      # ...
      gym(
          workspace: "app.xcworkspace",
          scheme: "app",
          # 打包前clean
          clean: true,
          # Release、Debug、自定义
          configuration: "Release",
          # app-store, ad-hoc, package, enterprise, development
          export_method: "ad-hoc",
          # 文件输出路径
          output_directory: "/Users/user/Desktop/",
          # ipa名称
          output_name: "app.ipa",
          # 是否包含调试符号
          include_symbols: true,
          # 是否开启bitcode
          include_bitcode: false,
        )
        # ...
    end
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22

    # Plugin

    除了 fastlane 内置的 action,我们还可以通过安装插件的方式引入第三方的 action 来实现更强大的功能。要安装插件只需要在 fastlane 所在目录运行下面的脚本

    fastlane add_plugin [插件名]
    
    1

    首次安装插件会生成 Pluginfile 文件,里面包含了我们安装的所有三方依赖,同时会在 Gemfile 中添加

    plugins_path = File.join(File.dirname(__FILE__), 'fastlane', 'Pluginfile')
    eval_gemfile(plugins_path) if File.exist?(plugins_path)
    
    1
    2

    之后就可以在 lane 中使用这个 action 了

    # 上传至 fir.im

    fir.im 是一个常用的 app 分发平台,它提供了 fastlane 插件 fir_cli 可以自动将 app 上传

    # 安装

    fastlane add_plugin fir_cli
    
    1

    # 参数

    参数名 描述 默认值
    api_token 注册账号后获得 token
    changelog 设置更新日志
    skip_update_icon 跳过图标更新,使用上次的图标 false
    open 是否开放所有人下载 true
    password 设置下载密码

    更多参数前往 fir_cli 查看

    # 使用

    lane :debug do
      # ...
      fir_cli(
        api_token: ".....",
        changelog: "测试更新",
        skip_update_icon: true,
        open: false,
        password: "123456",
      )
    end
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    # 上传到蒲公英

    蒲公英是常用的 app 分发平台,它提供了 fastlane 插件 pgyer 可以自动将 app 上传

    # 安装

    fastlane add_plugin pgyer
    
    1

    # 参数

    参数名 描述 默认值
    api_key 注册账号后获得 必填
    user_key 注册账号后获得 必填
    update_description 设置更新描述
    password 设置下载密码
    install_type 下载类型,1 - 公开安装,2 - 密码安装,3 - 邀请安装 1

    # 使用

    lane :debug do
      # ...
      pgyer(
        api_key: "...", 
        user_key: "...", 
        password: "123456", 
        install_type: "2",
        update_description: "全新的版本"
      )
    end
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    欢迎来到 KnightSama‘s Blog
    看板娘