Fastlane打包iOS工程小结

使用条件

  • 确保xcode命令行工具已安装
xcode-select --install
复制代码
  • 两种安装fastlane的方法
# Using RubyGems
sudo gem install fastlane -NV

# Alternatively using Homebrew
brew install fastlane
复制代码

第一步

  • 到项目中初始化fastlane
//执行完这条,你的项目就会有fastlane的几个文件,具体有什么文件,看初始化时的配置
fastlane init 

// swift项目使用这条
fastlane init swift
复制代码

必备功能

  • 对我来讲必备功能就是自动打包,而后上传到fir或者蒲公英,至于那些截图,或者用match来管理证书等都是辅助功能,因此我放到辅助功能中讲解。

签名

  • 方式1:使用match自动管理ios

    • 我这里不去用这个,由于会要revoke掉以前的证书
    • 使用match方法【展现不用,由于须要revoke已存在的证书,若是你须要使用match来管理证书,能够参考这篇文章:[juejin.im/post/5bd81e…]】
  • 方式2:使用cert和sigh方法git

    • 第一步:配置好项目
    • 手动管理签名的参考配置-推荐(If you don't use match, we recommend defining a mapping of app target to provisioning profile in your Fastfile. By defining those profiles, you can guarantee reproducible builds every time you run it.)
    • 自动管理签名的参考配置-可能会有问题(You can also use Xcode’s Automatically Manage Signing feature. By default, automatic signing via xcodebuild is disabled. To enable it, pass -allowProvisioningUpdates via the export_xcargs option:)
  • 手动管理签名的参考配置-推荐github

// 官方配置参考
lane :beta do
  build_app(
    export_method: "app-store",
    export_options: {
      provisioningProfiles: { 
        "com.example.bundleid" => "Provisioning Profile Name",
        "com.example.bundleid2" => "Provisioning Profile Name 2"
      }
    }
  )
end

// 本身配置,这里的xbb_dev是证书的名字
// 注意:export_method须要和证书相对应,若是是开发的模式,那么使用开发的证书;若是是appstore的,就须要使用appstore的证书;若是是ad-hoc的,就须要使用ad-hoc的证书。
lane :beta do
  build_app(
    export_method: "app-store",
    export_options: {
      provisioningProfiles: { 
        "com.xbks.test" => "xbks test dis"
      }
    }
  )
end
复制代码
  • 自动管理签名的参考配置-可能会有问题
lane :beta do
  build_app(export_xcargs: "-allowProvisioningUpdates")
end
复制代码

第二步

  • 打包一个ipa包出来,使用的命令,下面的命令能够打包一个ipa包出来,亲测,xbks test dis就是appstore对应的证书文件。
lane :beta do
  build_app(
      scheme: "qmuidemo",
      export_method: "app-store",
      export_options: {
        provisioningProfiles: { 
          "com.xbks.test" => "xbks test dis"
        }
      }
    )
end
复制代码

第三步

  • 安装插件
// 安装蒲公英
fastlane add_plugin pgyer
复制代码

参考指令

  • build_app()括号内能够配置的参数
// 有这些导出的类型,须要有相应的证书配对才能导出成功ipa。
// app-store, ad-hoc, package, enterprise, development, developer-id
export_method:"development"    

// 若是要使用ad-hoc打包, 则需打开此项配置
sigh(adhoc:true)

// 导出的ipa名字
output_name: ouput_name

// 导出的ipa位置
output_directory: '/Users/coderiding/Downloads/Temp'

// 
scheme: "xbb"

// 可省略
workspace: "xbb.xcworkspace"

// 若是使用自动签名,须要配置这句
export_xcargs: "-allowProvisioningUpdates"

// 
export_options: {}

// 是否清空上次打包信息
clean: true

// 配置开发模式仍是发布模式:Debug or Release
configuration: "Debug"
复制代码
  • 其余命令
// 打包命令
fastlane releaseDev descprition:" 优化代码块xx"

// 若是要使用ad-hoc打包, 则需打开此项配置
sigh(adhoc:true)

Time.new.strftime("%Y%m%d%H%M%S")

get_version_number

version = get_version_number(
  xcodeproj: "xbks.xcodeproj",
  target: "xbks"
)

get_build_number(xcodeproj: "xbb.xcodeproj")

increment_build_number

sync_code_signing

build_app

capture_screenshots
  
disable_automatic_code_signing(path: "my_project.xcodeproj")
 
enable_automatic_code_signing(path: "my_project.xcodeproj")
 
upload_to_testflight

upload_to_app_store

// invokes cert ,invokes是调用的意思
get_certificates           

// invokes sigh
get_provisioning_profile 

// 获取推送证书
get_push_certificate
复制代码
  • 命令使用问题
  • 问题1:使用increment_build_number,报错:Apple Generic Versioning is not enabled in this project.《《《《》》》》 解决:在 target -> building setting 中, 搜索 current project version, 出现 versioning system 选择 Apple Generic

辅助功能

功能:截图

  • 利用UITest来截图
    • Create a new UI Test target in your Xcode project (See the top part of this article) :先到项目中添加一个UITest的target,具体的方法百度,若是以前有,直接使用
    • Run " fastlane snapshot init " in your project folder :执行fastlane snapshot init命令,开启截图的功能
    • Add the ./SnapshotHelper.swift file to your UI Test target (You can move the file anywhere you want):项目中会生成SnapshotHelper.swift这个文件,把这个文件放到项目中,我是放到UITest target中的,这个文件的做用就是fasntlane帮你写好的截图方法类,由于它是用swift写,因此若是是oc项目,须要你本身添加桥接头文件,添加桥接头文件的方法就是直接在oc项目中建立一个swift文件,系统就会自动帮你建立桥接头文件。
    • Add a new Xcode scheme for the newly created UI Test target:为UITest target添加一个scheme
    • Edit the scheme:编辑scheme
    • In the list on the left click "Build", and enable the checkbox under the "Run" column for your target.:选择左侧的Build选项,容许右侧咱们的UITest scheme中的Run的选项,勾选。
    • Enable the Shared box of the newly created scheme:开启UITest scheme的Shared选项
    • (Objective C only) Add the bridging header to your test class.:若是是oc项目,须要执行这条,添加桥接头文件
    • #import "xx-Swift.h":在启动UITest的位置,导入swift头文件,这里的xx指的是新建的UITest target的名字
    • The bridging header is named after your test target with -Swift.h appended.
    • In your UI Test class, click the Record button on the bottom left and record your interaction:点击记录按钮,开始测试,会进行截图,前提是在UI测试代码中加入了截图的功能,截图的功能,分为启动和埋点。
    • To take a screenshot, call the following between interactions:这里就是截图功能中的埋点,在须要截图的地方,写下面的代码埋点。
      • Swift: snapshot("01LoginScreen")
      • Objective C: [Snapshot snapshot:@"01LoginScreen" timeWaitingForIdle:10];
    • Add the following code to your setUp() method:下面的是截图功能中的启动方法
//Swift:
let app = XCUIApplication()
setupSnapshot(app)
app.launch()


//Objective C:
XCUIApplication *app = [[XCUIApplication alloc] init];
[Snapshot setupSnapshot:app];
[app launch];
复制代码
  • 单独抽出来作个lane,执行这个lane就是截图和上传
lane :screenshots do
  capture_screenshots
  upload_to_app_store
end

//若是不经过lane来执行,可单独执行每一条指令
fastlane action capture_screenshots
fastlane action upload_to_app_store
复制代码
  • 单独上传到appstore
fastlane deliver
复制代码

  • 带手机框的截图
  • This will only add a device frame around the screenshots
  • If you want to upload the screenshots to the App Store, you have to provide a Framefile.json, with titles and background, otherwise the resolution of the framed screenshots doesn't match the requirements of App Store Connect.
fastlane frameit

// 使用这个功能,还需安装依赖的包
brew install libpng jpeg imagemagick

// 若是安装上面的包报错:mogrify: no decode delegate for this image format `PNG',按下面的再安装 brew uninstall imagemagick; brew install libpng jpeg; brew install imagemagick --build-from-source // 下载最新的frameit fastlane frameit setup // 使用 lane :screenshots do capture_screenshots frame_screenshots(white: true) upload_to_app_store end // 单独使用 fastlane action frame_screenshots // 执行下面的代码会获得一组套黑色手机边框的图片 fastlane frameit // 执行下面的代码会获得白色边框的图片 fastlane frameit silver // 支持的手机框颜色 white\silver\rose_gold\gold // 查询更多用法 fastlane action frameit 复制代码
  • 为截图自定义标题和背景

  • 截图高级功能
// 更新截图helper文件
fastlane snapshot update

复制代码

  • 截图问题
  • 问题1:Your Snapshot Helper file is missing, please place a copy
  • 问题2:上传截图到appstore的时候,报错:Unexpected Error

slack介绍

  • 任务完成后用来发送消息,及时告知你结果
  • 这里用到一个slack网站来实现这个功能
  • xbks.slack.com/apps/new/A0…
  • 直接注册帐号,根据提示获取到WebHook URL,这个须要在slack action中用到
// 单独发送一条消息,其中slack_url就是你上面获取到的WebHook URL

lane :slack_message do
   slack(
       message: "App successfully uploaded to iTunesConnect.",
       success: true,
       slack_url: "https://your slack incoming webhook url"
   )
end
复制代码

参考

相关文章
相关标签/搜索