通過 Crashlytics 構建和上傳多種口味的 Fastfile
這是一個多風味應用程式的 Fastfile 設定示例。它為你提供了構建和部署所有口味或單一口味的選項。部署之後,它向 Slack 報告部署狀態,並通過 Crashlytics 測試人員組向 Beta 測試人員傳送通知。
要構建和部署所有口味,請使用:
fastlane android beta
要構建單個 APK 並部署使用:
fastlane android beta app:flavorName
使用單個 Fastlane 檔案,你可以管理 iOS,Android 和 Mac 應用程式。如果你只為一個應用程式使用此檔案,則不需要 platform
。
這個怎麼運作
android
論證告訴 fastlane 我們將使用:android
平臺。- 在
:android
平臺內,你可以擁有多個車道。目前,我只有:beta
車道。上面命令的第二個引數指定了我們想要使用的通道。 options[:app]
- 有兩個 Gradle 任務。首先,它執行
gradle clean
。如果你提供了app
鍵的味道,fastfile 執行gradle assembleReleaseFlavor
。否則,它會執行gradle assembleRelease
來構建所有構建版本。 - 如果我們為所有風格構建,則生成的 APK 檔名陣列儲存在
SharedValues::GRADLE_ALL_APK_OUTPUT_PATHS
中。我們使用它來迴圈生成的檔案,並通過 Crashlytics 將它們部署到 Beta 。notifications
和groups
欄位是可選的。它們用於通知 Crashlytics 在 Beta 上註冊該應用的測試人員。 - 如果你熟悉 Crashlytics,你可能知道要在門戶中啟用應用程式,你必須先在裝置上執行它並首先使用它。否則,Crashlytics 將假定應用程式處於非活動狀態並丟擲錯誤。在這種情況下,我捕獲它並向 Slack 報告失敗,因此你將知道哪個應用程式處於非活動狀態。
- 如果部署成功, fastlane 將向 Slack 傳送成功訊息。
#{/([^\/]*)$/
.match(apk)}
這個正規表示式用於從 APK 路徑獲取風味名稱。如果它不適合你,你可以將其刪除。get_version_name
和get_version_code
是兩個 Fastlane 外掛,用於檢索應用程式版本名稱和程式碼。如果要使用,則必須安裝這些寶石,或者可以刪除它們。在此處閱讀有關外掛的更多資訊- 如果你正在構建和部署單個 APK,則將執行
else
語句。我們沒有向 Crashlytics 提供apk_path
,因為我們只有一個應用程式。 - 最後的
error do
塊用於在執行期間出現任何其他錯誤時收到通知。
注意
不要忘記用自己的憑據替換 SLACK_URL
,API_TOKEN
,GROUP_NAME
和 BUILD_SECRET
。
fastlane_version "1.46.1"
default_platform :android
platform :android do
before_all do
ENV["SLACK_URL"] = "https://hooks.slack.com/servic...."
end
lane :beta do |options|
# Clean and build the Release version of the app.
# Usage `fastlane android beta app:flavorName`
gradle(task: "clean")
gradle(task: "assemble",
build_type: "Release",
flavor: options[:app])
# If user calls `fastlane android beta` command, it will build all projects and push them to Crashlytics
if options[:app].nil?
lane_context[SharedValues::GRADLE_ALL_APK_OUTPUT_PATHS].each do | apk |
puts "Uploading APK to Crashlytics: " + apk
begin
crashlytics(
api_token: "[API_TOKEN]",
build_secret: "[BUILD_SECRET]",
groups: "[GROUP_NAME]",
apk_path: apk,
notifications: "true"
)
slack(
message: "Successfully deployed new build for #{/([^\/]*)$/.match(apk)} #{get_version_name} - #{get_version_code}",
success: true,
default_payloads: [:git_branch, :lane, :test_result]
)
rescue => ex
# If the app is inactive in Crashlytics, deployment will fail. Handle it here and report to slack
slack(
message: "Error uploading => #{/([^\/]*)$/.match(apk)} #{get_version_name} - #{get_version_code}: #{ex}",
success: false,
default_payloads: [:git_branch, :lane, :test_result]
)
end
end
after_all do |lane|
# This block is called, only if the executed lane was successful
slack(
message: "Operation completed for #{lane_context[SharedValues::GRADLE_ALL_APK_OUTPUT_PATHS].size} app(s) for #{get_version_name} - #{get_version_code}",
default_payloads: [:git_branch, :lane, :test_result],
success: true
)
end
else
# Single APK upload to Beta by Crashlytics
crashlytics(
api_token: "[API_TOKEN]",
build_secret: "[BUILD_SECRET]",
groups: "[GROUP_NAME]",
notifications: "true"
)
after_all do |lane|
# This block is called, only if the executed lane was successful
slack(
message: "Successfully deployed new build for #{options[:app]} #{get_version_name} - #{get_version_code}",
default_payloads: [:git_branch, :lane, :test_result],
success: true
)
end
end
error do |lane, exception|
slack(
message: exception.message,
success: false,
default_payloads: [:git_branch, :lane, :test_result]
)
end
end
end