Loading... ## 背景 在实际的项目开发过程中,每次开发完成,发布测试包或者正式包的时候都需要一个一个去打包,打包完成后还需要上传到各类的分发平台。如果上传到App Store之类的可能还好一点。这边主要讲解下如何利用fastlane来一条命令构建两个包,并且上传到fir分发平台 ## 什么是fastlane `fastlane`是为您的`iOS`和`Android`应用程序自动化测试部署和发布的最简单方法。🚀 它可以处理所有繁琐的任务,例如生成屏幕截图、处理代码签名和发布您的应用程序。(来自官网[docs.fastlane.tools/](https://link.juejin.cn/?target=https%3A%2F%2Fdocs.fastlane.tools%2F))。 ## 初始化fastlane ### iOS 进入到Flutter项目的iOS目录文件夹中,执行`fastlane init` ![](https://minio-api.open.yantao.wiki:16666/blog-image/2024/08/28/66ce78d74731e.png) 首次会询问你用来做什么,我们这边选择`4`,手动配置,等他执行完成后,我们先等待。 ### Android Android这边有点不太一样,因为不需要上传到Google商店,因此初始化过程中可能会出现报错,但不影响 ![](https://minio-api.open.yantao.wiki:16666/blog-image/2024/08/28/66ce795b2f7e0.png) 分为两步骤 * 询问你PackageName,按照真实项目的地址填写 * SecretJson文件,这个没有,因为不上传Google Play,因此直接回车(注意,这里回车后会出现一些错误信息,忽略即可) ## 构造fastlane脚本 ### iOS #### 目录架构 ![](https://minio-api.open.yantao.wiki:16666/blog-image/2024/08/28/66ce79d0ad2c5.png) 如果缺少文件的话后面会说道,我们一个文件一个文件的讲解 #### 设置Appfile ![](https://minio-api.open.yantao.wiki:16666/blog-image/2024/08/28/66ce7a06ca21a.png) * app\_identifier:bundle identifier * apple\_id:Your Apple email address * team\_id:团队开发者证书ID #### Match证书配置 ```csharp fastlane match init ``` ![](https://minio-api.open.yantao.wiki:16666/blog-image/2024/08/28/66ce7a8d34916.png) 选择证书管理方式,我这边选择了1,当然你可以选择其他的方式。等运行完成后,会出现`Macthfile`文件 下面的信息根据你自己的实际情况来修改 <pre><code id="codeBlock1-1728105605923" class="hljs language-bash">git_url("git@gitlab.xxxxxx.com:it/xiaoku-xxxxxx.git") storage_mode("git") gitlab_host("https://gitlab.xxxxx.com") type("development") # The default type, can be: appstore, adhoc, enterprise or development app_identifier(["com.xxxxx"]) # username("user@fastlane.tools") # Your Apple Developer Portal username # For all available options run `fastlane match --help` # Remove the # in the beginning of the line to enable the other options # The docs are available on https://docs.fastlane.tools/actions/match </code></pre> #### 生成开发者证书 ```lua fastlane match development ``` fastlane会自动去你的iOS开发者账号生成相关的证书,并且上传到git仓库中,方便其他同学的证书管理 #### 生成正式环境证书 ```lua fastlane match enterprise ``` 我这边生成的是企业证书,你如果有其他需求可以生成`adhoc`和`appstore`根据你自己的情况来 #### 构建fastfile脚本 <pre><code id="codeBlock4-1728105605924" class="hljs language-php"># 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 default_platform(:ios) platform :ios do before_all do ENV["MATCH_PASSWORD"] = "MATCH_PASSWORD" end lane :dev do version = get_version_number(xcodeproj: "Runner.xcodeproj", target: "Runner") matchCert( type: "development", ) gym( workspace:"Runner.xcworkspace", scheme:"Runner", export_method:"development", configuration:"Development", clean:true, output_directory:"build_ipa_development", output_name:"xiaoku.ipa", export_options:{ manifest: { appURL: "xxxxx.ipa", displayImageURL: "xxxx", fullSizeImageURL: "xxxxx", }, } ) ipa_dir = "../build_ipa_development" ipa_name = 'xiaoku.ipa' # 上传至 fir.im firim(firim_api_token: "xxxxxxx") UI.success("Successfully uploaded version #{version} to fir.im!") end desc "打包,同时生成manifset文件,上传至OSS中" lane :app do version = get_version_number(xcodeproj: "Runner.xcodeproj", target: "Runner") matchCert( type: "enterprise", ) gym( workspace:"Runner.xcworkspace", scheme:"Runner", export_method:"enterprise", configuration:"Release", clean:true, output_directory:"build_ipa_release", output_name:"xiaoku.ipa", export_options:{ manifest: { appURL: "xxxxx.ipa", displayImageURL: "xxxx", fullSizeImageURL: "xxxxx", }, } ) UI.success("Successfully finished deployment") UI.success("iOS install url: itms-services://?action=download-manifest&url=xxxxx/#{version}/manifest.plist") end desc "拉取git仓库的证书" lane :matchCert do |options| match( app_identifier: ["com.xxxx.xxx.xxx"], type: options[:type], force_for_new_devices: options[:force_for_new_devices], readonly: is_ci, keychain_password: "keychain_password", ) end end</code><div class="expand-done"><i class="ri-arrow-up-double-line"></i></div></pre> 主要修改脚本内的以下内容 * ipa名称 * displayImageURL地址 * fullSizeImageURL地址 * manifest.plist的下载地址 * keychain\_password * MATCH\_PASSWORD * firim\_api\_token(如果不需要上传到fir,可以把这一段删除) #### Fastane firim插件安装(如果不需要,则忽略) ```undefined fastlane add_plugin firim ``` ### Android 安卓的稍微简单一点,没有证书管理这么一套东西 #### 设置Appfile <pre><code id="codeBlock6-1728105605924" class="hljs language-bash">json_key_file("") # Path to the json secret file - Follow https://docs.fastlane.tools/actions/supply/#setup to get one package_name("com.krausefx.app") # e.g. com.krausefx.app</code></pre> #### 构建fastfile脚本 <pre><code id="codeBlock7-1728105605924" class="hljs language-php"># 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 default_platform(:android) platform :android do desc "Deploy a new version to the Google Play" lane :app do gradle( task: 'assemble', build_type: 'Release', properties: { "android.injected.signing.store.file" => Dir.pwd + "/../keystore.keystore", "android.injected.signing.store.password" => "password", "android.injected.signing.key.alias" => "alias", "android.injected.signing.key.password" => "password", }, ) sh ("mv ../../build/app/outputs/apk/release/app-release.apk ../build/xiaoku.apk") firim( apk:"./build/xiaoku.apk", platform:"android", firim_api_token: "firim_api_token", app_changelog: "Release", gradle_file: "./app/build.gradle", app_name:"xiaoku" ) end end</code><div class="expand-done"><i class="ri-arrow-up-double-line"></i></div></pre> 主要修改以下内容 * 证书:keystore.keystore * password:证书密码 * alias:证书别名 * firim\_api\_token:(不需要则把这一段删除) #### Fastane firim插件安装(如果不需要,则忽略) ```undefined fastlane add_plugin firim ``` ## 准备Shell脚本 ### Release脚本 <pre><code id="codeBlock9-1728105605925" class="hljs language-bash">#!/bin/bash # 定义颜色 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[0;33m' NC='\033[0m' # 没有颜色 # 进入ios目录并执行fastlane dev cd ios && fastlane app if [ $? -ne 0 ]; then echo -e "${RED}❌ iOS部分执行失败!请检查错误日志。${NC}" exit 1 else echo -e "${GREEN}✅ iOS部分执行成功!${NC}" fi # 进入android目录并执行fastlane app cd ../android && fastlane app if [ $? -ne 0 ]; then echo -e "${RED}❌ Android部分执行失败!请检查错误日志。${NC}" exit 1 else echo -e "${GREEN}✅ Android部分执行成功!${NC}" fi echo -e "${YELLOW}🎉 所有任务都已成功完成!${NC}"</code><div class="expand-done"><i class="ri-arrow-up-double-line"></i></div></pre> ### Dev脚本 <pre><code id="codeBlock10-1728105605925" class="hljs language-bash">#!/bin/bash # 定义颜色 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[0;33m' NC='\033[0m' # 没有颜色 # 进入ios目录并执行fastlane dev cd ios && fastlane dev if [ $? -ne 0 ]; then echo -e "${RED}❌ iOS部分执行失败!请检查错误日志。${NC}" exit 1 else echo -e "${GREEN}✅ iOS部分执行成功!${NC}" fi # 进入android目录并执行fastlane app cd ../android && fastlane app if [ $? -ne 0 ]; then echo -e "${RED}❌ Android部分执行失败!请检查错误日志。${NC}" exit 1 else echo -e "${GREEN}✅ Android部分执行成功!${NC}" fi echo -e "${YELLOW}🎉 所有任务都已成功完成!${NC}"</code><div class="expand-done"><i class="ri-arrow-up-double-line"></i></div></pre> ## 大功告成 以上操作全部完成之后,后期的打包动作大家应该都知道是啥了吧。就是执行上面的两个脚本就全部打包完成,并且同时上传到了fir上,用户可以直接利用fir链接进行安装 ### Release打包 ```undefined sh build_release.sh ``` ### Dev打包 ```undefined sh build_dev.sh ``` 最后修改:2024 年 10 月 05 日 © 允许规范转载 赞 如果觉得我的文章对你有用,请随意赞赏