一、前语

react native打包是一件比较复杂的事情,相关于原生android,ios来讲,首要你或许需求在每次打包之前要从头生成bundlejs文件,否则或许新修正的代码不会生效,其次,在生成jsbundle的进程中或许会生成一些无用的node开头的资源,打包之前需求删去。

别的需求分别对ios和android进行打包,ios打包完结后还需求上传到三方保管平台,供测验扫码下载,随着项目的不断迭代,打包耗时越来越长,进程繁杂,假如能把这些进程整合,主动化顺次履行,一行指令打包上传到蒲公英或许fir等保管平台,岂不美哉,好巧不巧,Fastlane就能够做到。

二、基础运用

运用 Fastlane 是一种主动化打包和布置 React Native 项目的好方式。Fastlane 是一个用 Ruby 编写的东西集,能够简化移动运用的构建、测验和布置进程。下面是一个运用 Fastlane 完结 React Native 项目一键打包和布置的简略进程:

1.环境装置

装置Xcode command line tools
xcode-select --install
装置ruby
brew install ruby
装置fastlane
sudo gem install fastlane -NV

2.初始化项目

cd到项目目录,履行fastlane init指令

fastlane init

我在项目根目录实际操作中发现会报找不到ios目录的问题,假如你也有此问题,或许你能够cd到my-project/ios目录下履行fastlane init指令,实践来看,在ios目录下履行后,把fastlane生成的文件移动到项目根目录即可。

假如在ios目录履行,或许需求你输入apple开发账号等信息,根据提示输入即可。

这个时候项目根目录下会出现fastlane文件夹,里边会有AppfileFastfile文件。

fastlane init是Fastlane供给的一个指令行东西,能够帮助开发人员快速创立Fastfile文件和其他必要的装备文件,从而简化Fastlane的装备进程。

  • Fastfile:这是Fastlane的中心装备文件,包含了使命定义和装备信息。运用fastlane init指令创立的Fastfile包含了一些示例使命和装备信息,能够根据需求进行修正和扩展。
  • Appfile:这个文件包含了运用程序的相关信息,例如Bundle ID和Apple Developer账户信息。运用fastlane init指令创立的Appfile会询问用户输入这些信息,并将其保存到文件中。
  • Deliverfile:这个文件包含了运用Deliver上传运用程序到App Store的装备信息。运用fastlane init指令创立的Deliverfile包含了一些示例装备信息,能够根据需求进行修正和扩展。
  • Gymfile:这个文件包含了运用Gym构建运用程序的装备信息。运用fastlane init指令创立的Gymfile包含了一些示例装备信息,能够根据需求进行修正和扩展。
  • Matchfile:这个文件包含了运用Match办理代码签名和证书的装备信息。运用fastlane init指令创立的Matchfile包含了一些示例装备信息,能够根据需求进行修正和扩展。
  • Screengrabfile:这个文件包含了运用Screengrab进行运用程序截图的装备信息。运用fastlane init指令创立的Screengrabfile包含了一些示例装备信息,能够根据需求进行修正和扩展。
  • Supplyfile:这个文件包含了运用Supply上传运用程序到Google Play的装备信息。运用fastlane init指令创立的Supplyfile包含了一些示例装备信息,能够根据需求进行修正和扩展。

3. 修正 Fastfile 文件,以下是个模板,可根据自身项目进行简略修正

# 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
#
pyger_api_key = "xxxxx" # 替换成你蒲公英账号的api_key
before_all do
  # 在lane履行前运转
end
# 打包ios
platform :ios do
  desc "打包ios并上传"
  lane :publish do
    # 从头生成jsbundle包
    sh("yarn bundle-ios")
    # 挑选上传地址,这里是测验包导出,所以挑选ad-hoc
    export_method = "ad-hoc"
    upload = "pgyer"
    upload = UI.select("挑选上传地址:", ["pgyer", "fir"])
    scheme_name = "schemName" # 替换成你的ios项目要打包的scheme称号
    project = "./ios/schemName.xcodeproj"
    # build加1
    increment_build_number(xcodeproj: project)
    # 获取最新的build号码
    build_number = get_build_number(xcodeproj: project)
    # 获取运用版本号
    versoin_number = get_version_number(
      xcodeproj: project,
      target: scheme_name
    )
    # 拼接打包文件称号
    output_name = "#{scheme_name}_#{versoin_number}_#{build_number}_#{Time.now.strftime('%Y%m%d%H%M%S')}.ipa"
    # 打包
    gym( # build_app的别号
      workspace: "./ios/schemName.xcworkspace", 
      scheme: scheme_name, 
      export_method: export_method, 
      output_directory: "./ios/build",
      output_name: output_name,
      clean: true, # 每次打包前整理项目
      suppress_xcode_output: true, # 打包进程中不显示xcode的输出信息
    )
    # 上传到蒲公英或fir
    if upload === "fir"
      fir_cli api_token: "xxxxx",  changelog: "Hello 我是ios包"
      notification(subtitle: "打包成功!", message: "ios打包成功,上传fir成功!")
    else
      pgyer(api_key: pyger_api_key)
      notification(subtitle: "打包成功!", message: "ios打包成功,上传pgyer成功!")
    end
  end
end
# 打包android
platform :android do
  desc "打包android并上传到蒲公英或fir"
  lane :publish do
    upload = "pgyer"
    upload = UI.select("挑选上传地址:", ["pgyer", "fir"])
    # 从头生成jsbundle资源包
    sh("yarn bundle-android")  
    sleep(1)
    desc "------整理无用资源完结-----"
    project_dir = "./android/"
    gradle(task: 'clean', project_dir: project_dir) # 整理
    gradle(
      task: 'assemble', 
      build_type: 'Release', 
      project_dir: project_dir,
      print_command: false,
      # 装备android签名信息
      properties: {
        "android.injected.signing.store.file" => "/path/path/test.keystore",
        "android.injected.signing.store.password" => "keystore_password",
        "android.injected.signing.key.alias" => "aliasName",
        "android.injected.signing.key.password" => "alias_password",
      }
    )
    if upload === "fir" #上传到fir
      fir_cli api_token: "xxxxx",  changelog: "Hello 我是android包"
      notification(subtitle: "打包成功!", message: "android打包成功,上传fir成功!")
    else
      pgyer(api_key: pyger_api_key) # 上传到蒲公英
      notification(subtitle: "打包成功!", message: "android打包成功,上传蒲公英成功!")
    end
  end
end
after_all do
  # 在lane履行后运转
end

在项目根目录履行fastlane android publish和fastlane ios publish既能够分别对android和ios平台进行打包,并主动上传蒲公英或许fir。

【react native】解放双手,fastlane自动化打包,提效200%

4.上传蒲公英或许fir装备

蒲公英上传装备

运转指令,装置蒲公英fastlane插件

fastlane add_plugin pgyer

Fastfile中添加如下代码即可主动上传:

pgyer(api_key: pyger_api_key) # 上传到蒲公英

蒲公英apikey检查:

【react native】解放双手,fastlane自动化打包,提效200%

fir上传装备

运转指令,装置fir fastlane插件

fastlane add_plugin fir_cli

Fastfile中添加如下代码即可主动上传:

fir_cli api_token: "xxxx",  changelog: "Hello 我是android包"

fir api_token检查:

【react native】解放双手,fastlane自动化打包,提效200%

5.注意事项

一旦装备完结,发现打包就是一个指令的事,比起之前节省了不少时刻,本文针对的是react native项目,假如是纯android或原生ios,大家能够自行测验,理论上只要合理运用,关于打包功率都会有所提高。

假如想要删去项目中的某些资源,能够运用如下指令,途径需求完整途径:

sh('rm /Users/zhangsan/Desktop/VSWork/project-name-rn/android/app/src/main/res/drawable-hdpi/test.png')

举一反三,其实很多指令都可在这里履行,比方操作文件,移动文件等等,大家能够自由探究,别的fastfile脚本用的ruby言语,有兴趣的能够自行探究,开发自己的action或许插件运用,以便解放双手。

fastlane社区供给了很多插件和action可用,能够去docs.fastlane.tools/actions和docs.fastlane.tools/plugins/ava…查阅运用。

官方文档:docs.fastlane.tools/