# 为 iOS 构建自定义 Fastlane 配置

> Configure Fastlane settings for iOS builds to control build behavior.

**Build Automation** 依赖 [Fastlane](https://fastlane.tools/) 来构建 Xcode 项目。本文档介绍了如何使用名为 [Gymfile](https://docs.fastlane.tools/actions/gym/#gymfile) 的 Fastlane 文件格式来修改 Fastlane 的行为。所有配置选项都与 Fastlane 高度相关，因此建议对工具集有一定的基本了解。

本节以一个基本示例来说明当前如何配置项目以使用多个配置文件进行构建。以下示例假设您正在构建一个应用程序标识符为 `"com.unity3d.buildautomation.base"` 的应用程序和一个应用程序标识符为 `"com.unity3d.buildautomation.base.stickers"` 的贴纸扩展。

## 添加所需文件##add-required-files

此设置目前需要向代码仓库中添加三个额外的文件：

### 文件 1：一个 JSON 选项文件，用于控制自定义 Fastfile/Gymfile 的路径以及应该运行 Fastfile 中的哪些[通道](https://docs.fastlane.tools/advanced/lanes/#lanes)。##file-1-a-json-options-file-that-controls-the-paths-to-your-custom-fastfilegymfile-and-which-laneshttpsdocsfastlanetoolsadvancedlaneslanes-in-the-fastfile-should-be-run

此文件可以放在代码仓库中的任何位置，但在此示例中放在 `Assets/ucb_xcode_fastlane.json`。该文件支持以下属性（所有这些属性都是可选的，如果不使用某个属性，可将其完全移除）：fastfile - 自定义 Fastfile 相对于代码仓库根目录的路径。gymfile - 自定义 Gymfile 相对于代码仓库根目录的路径。lanes - 上述 Fastfile 中的通道。pre\_build - 在实际 Xcode 构建步骤之前运行的通道。post\_build - 在实际 Xcode 构建步骤之后运行的通道。

这些路径均相对于项目的根目录。

示例 `"Assets/ucb_xcode_fastlane.json"`：

```js
(JavaScript)

{
    "fastfile": "Assets/ucb/Fastfile",
    "gymfile": "Assets/ucb/Gymfile",
    "lanes": {
        "pre_build": "use_stickers_profile",
        "post_build": ""
    }
}
```

### 文件 2：一个自定义 Fastfile，用于安装资源调配配置文件并更新 Xcode 项目中的资源调配设置。##file-2-a-custom-fastfile-to-install-the-provisioning-profile-and-update-the-provisioning-settings-in-the-xcode-project

此文件可以放在代码仓库中的任何位置，但需要与上面 `ucb_xcode_fastlane.json` 中指定的“fastfile”路径相匹配。

如果您配置了 pre\_build/post\_build 通道，当这些通道运行时，会向其传递一个选项哈希，其中包含：project\_dir - 此项目构建所基于代码仓库的根目录。build\_target - 此构建的构建目标标识符。output\_directory - Xcode 项目将构建至的最终输出目录。

在此示例中，我们在代码仓库的“Assets/ucb/Stickers.mobileprovision”处安装资源调配配置文件，并更新 Xcode 项目中的“Unity-iPhone-Stickers”目标以使用该配置文件。

示例 `Assets/ucb/Fastfile`：

```rb
(Ruby)

lane :use_stickers_profile do |options|
    profile_path = File.join(options[:project_dir], 'Assets/ucb/Stickers.mobileprovision')
    FastlaneCore::ProvisioningProfile.install(profile_path)
    update_project_provisioning(
        xcodeproj: 'Unity-iPhone.xcodeproj',
        target_filter: 'Unity-iPhone-Stickers',
        profile: profile_path
    )
end
```

### 文件 3：一个自定义的 Gymfile，用于在自定义导出选项时定义应用程序标识符到资源调配配置文件的映射。##file-3-a-custom-gymfile-to-define-the-application-identifier-to-provisioning-profile-mapping-as-part-of-customizing-the-export-options

此文件可以放在代码仓库中的任何位置，但需要与上述 ucb\_xcode\_fastlane.json 中的“gymfile”路径相匹配。有关可用选项，请参阅 [Fastlane 文档](https://docs.fastlane.tools/actions/gym/)。

在此示例中，“com.unity3d.buildautomation.base.stickers”应用程序标识符应映射到 `1e33640e-9a55-4357-a632-ca6c48a53a96` 的 UUID（即 `Assets/ucb/Stickers.mobileprovision` 处资源调配配置文件的 UUID）。

示例 `Assets/ucb/Gymfile`：

```rb
(Ruby)

export_options(
    provisioningProfiles: {
        "com.unity3d.buildautomation.base.stickers" => "1e33640e-9a55-4357-a632-ca6c48a53a96"
    }
)
```

## 更新高级设置##update-advanced-settings

最后，在 Dashboard 中更新构建目标的高级设置。

1. 导航到 [Unity Dashboard](https://cloud.unity.com/)
2. 选择 **DevOps** > **Build Automation** > **Configurations（配置）**。
3. 导航到 iOS 构建目标。
4. 选择 **Edit（编辑）**。
5. 导航到 Edit Advanced Options（编辑高级选项）选项卡。
6. 在 **Platform specific settings (iOS)（针对特定平台的设置 (iOS)）** 选项下，设置 Custom Fastlane Configuration Path（自定义 Fastlane 配置路径），该路径相对于项目的根目录。在此示例中，该路径设置为“Assets/ucb\_xcode\_fastlane.json”。


**为 iOS 构建自定义 Fastlane 配置:**
!["为 iOS 构建自定义 Fastlane 配置"](/api/media?file=/build-automation/media/images/build-automation/fastlane-config.png "UBA - 设置 Gymfile - Fastlane 配置")
