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.cn/post/684490…]】
  • 方式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
複製代碼

參考

相關文章
相關標籤/搜索