1. 程式人生 > Android開發 >Fastlane打包iOS工程小結

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自動管理

    • 我這裡不去用這個,因為會要revoke掉之前的證書
    • 使用match方法【展示不用,因為需要revoke已存在的證書,如果你需要使用match來管理證書,可以參考這篇文章:[juejin.im/post/5bd81e…]】
  • 方式2:使用cert和sigh方法

    • 第一步:配置好專案
    • 手動管理簽名的參考配置-推薦(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:)
  • 手動管理簽名的參考配置-推薦

// 官方配置參考
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
複製程式碼

參考