Catch up on highlights from Firebase at Google I/O 2023. Learn more

通知測試人員有關新版本的信息

可選的 Firebase App Distribution iOS 和 Android SDK 讓您可以在您的應用程序的新版本可供安裝時向您的測試人員顯示應用程序內警報。本指南介紹瞭如何使用 App Distribution iOS 和 Android SDK 為您的測試人員創建和自定義新的構建警報。

在你開始之前

如果您還沒有,請將 Firebase 添加到您的 Android 項目中

第 1 步:啟用 App Distribution Tester API

  1. Google Cloud Console中選擇您的項目。

  2. 在 Firebase App Testers API 下,點擊Enable

第 2 步:將 App Distribution 添加到您的應用

App Distribution Android SDK 由兩個庫組成:

  • firebase-appdistribution-api - 僅限 API 的庫,您可以將其包含在所有構建變體中。
  • firebase-appdistribution - 完整的 SDK 實現(可選)。

API-only 庫允許您的代碼調用 SDK。如果不存在完整的 SDK 實現,調用將無效。

在您的模塊(應用級)Gradle 文件(通常app/build.gradle )中聲明 App Distribution Android SDK 的依賴項。為避免在您的 Play 構建中包含完整的 SDK 實現的自我更新功能,請將 API-only 庫依賴項添加到所有構建變體。僅將完整的 SDK 實現添加到專門用於預發布測試的變體:

Kotlin+KTX

dependencies {
    // ADD the API-only library to all variants
    implementation 'com.google.firebase:firebase-appdistribution-api-ktx:16.0.0-beta08'

    // ADD the full SDK implementation to the "beta" variant only (example)
    betaImplementation 'com.google.firebase:firebase-appdistribution:16.0.0-beta08'
}

Java

dependencies {
    // ADD the API-only library to all variants
    implementation 'com.google.firebase:firebase-appdistribution-api:16.0.0-beta08'

    // ADD the full SDK implementation to the "beta" variant only (example)
    betaImplementation 'com.google.firebase:firebase-appdistribution:16.0.0-beta08'
}

第 3 步:配置應用內提醒

App Distribution Android SDK 提供了以下方法來為您的測試人員設置應用內構建警報:

  • 一個基本的警報配置,帶有預構建的應用程序更新和登錄對話框,以顯示給測試人員。
  • 一種高級警報配置,允許您自定義自己的用戶界面。

如果您是第一次使用 App Distribution Android SDK,我們建議您使用Basic Configuration

基本配置

使用updateIfNewReleaseAvailable向尚未啟用警報的測試人員顯示預構建的啟用警報對話框,然後檢查新版本是否可用。調用時,該方法執行以下序列:

  1. 檢查測試人員是否啟用了警報。如果測試人員尚未啟用警報,該方法會提示測試人員使用其 Google 帳戶登錄 App Distribution。

  2. 檢查新的可用構建以供測試人員安裝。

  3. 顯示預建警報,提示測試人員進行更新。

  4. 如果新版本是 Android App Bundle (AAB),則將測試人員重定向到 Google Play 以完成更新過程。

    如果新構建是 Android 應用程序包 (APK),則 SDK 在後台下載新構建並在下載完成時提示測試人員安裝。 SDK 使用NotificationManager向用戶發送下載進度通知。您還可以通過將onProgressUpdate處理程序附加到updateIfNewReleaseAvailable任務來添加自己的進度指示器。

您可以在應用中的任何位置調用updateIfNewReleaseAvailable 。例如,您可以在應用程序主活動的onResume方法期間調用updateIfNewReleaseAvailable

以下示例檢查測試人員是否啟用了警報並有權訪問新版本。如果滿足這些條件,則會在構建可供安裝時顯示一個對話框:

Kotlin+KTX

// Copy and paste this into any part of your app - for example, in your main
// activity's onResume method.
val firebaseAppDistribution = FirebaseAppDistribution.getInstance()
firebaseAppDistribution.updateIfNewReleaseAvailable()
    .addOnProgressListener { updateProgress ->
      // (Optional) Implement custom progress updates in addition to
      // automatic NotificationManager updates.
    }
    .addOnFailureListener { e ->
      // (Optional) Handle errors.
      if (e is FirebaseAppDistributionException) {
        when (e.errorCode) {
          Status.NOT_IMPLEMENTED -> {
            // SDK did nothing. This is expected when building for Play.
          }
          else -> {
            // Handle other errors.
          }
        }
      }
    }

Java

// Copy and paste this into any part of your app - for example, in your main
// activity's onResume method.
FirebaseAppDistribution firebaseAppDistribution = FirebaseAppDistribution.getInstance();
firebaseAppDistribution.updateIfNewReleaseAvailable()
    .addOnProgressListener(updateProgress -> {
      // (Optional) Implement custom progress updates in addition to
      // automatic NotificationManager updates.
    })
    .addOnFailureListener(e -> {
      // (Optional) Handle errors.
      if (e instanceof FirebaseAppDistributionException) {
        switch (((FirebaseAppDistributionException)e).getErrorCode()) {
          case NOT_IMPLEMENTED:
            // SDK did nothing. This is expected when building for Play.
            break;
          default:
            // Handle other errors.
            break;
        }
      }
    });

高級配置

高級登錄配置

方法signInTesterisTesterSignedIn使您可以更靈活地自定義測試人員的登錄體驗,以便測試人員的體驗可以更好地匹配您的應用程序的外觀。

以下示例檢查測試人員是否已登錄其 App Distribution 測試人員帳戶。這讓您可以選擇僅向尚未登錄的測試人員顯示您的登錄用戶界面 (UI)。測試人員登錄後,您可以調用updateIfNewReleaseAvailable來檢查測試人員是否有權訪問新版本。

Kotlin+KTX

// Only show sign-in UI if this is the "beta" variant (example).
if (BuildConfig.BUILD_TYPE == "beta" && !firebaseAppDistribution.isTesterSignedIn) {
    // Start your sign-in UI here.
}

// Only check for updates if the tester is already signed in (do not prompt).
if (firebaseAppDistribution.isTesterSignedIn) {
    firebaseAppDistribution.updateIfNewReleaseAvailable().addOnFailureListener {
        // Handle failed update.
    }
}

Java

// Only show sign-in UI if this is the "beta" variant (example).
if (BuildConfig.BUILD_TYPE == "beta" && !firebaseAppDistribution.isTesterSignedIn()) {
    // Start your sign-in UI here.
}

// Only check for updates if the tester is already signed in (do not prompt).
if (firebaseAppDistribution.isTesterSignedIn()) {
    firebaseAppDistribution.updateIfNewReleaseAvailable().addOnFailureListener( e -> {
        // Handle failed update.
    });
}

在您的登錄 UI 中,當測試人員選擇繼續時,調用signInTester()

Kotlin+KTX

firebaseAppDistribution.signInTester().addOnSuccessListener {
  // Handle successful sign-in.
}.addOnFailureListener {
  // Handle failed sign-in.
});

Java

firebaseAppDistribution.signInTester().addOnSuccessListener( unused -> {
  // Handle successful sign-in.
}).addOnFailureListener(e -> {
  // Handle failed sign-in.
});

高級更新配置

checkForNewReleaseupdateApp方法讓您可以更靈活地自定義何時提示您的測試儀更新。您還可以自定義預構建的更新對話框和下載進度指示器,以便它們可以更好地匹配您的應用程序的外觀和感覺。

請注意, updateApp不提供下載進度指示。這意味著您需要使用NotificationManager 、某種應用內狀態顯示或其他方法來實現您自己的進度指示。

以下示例檢查是否有新版本可用,然後顯示自定義 UI。在調用checkForNewReleaseupdateApp之前,請確保測試人員已使用高級登錄配置登錄

Kotlin+KTX

firebaseAppDistribution.checkForNewRelease().addOnSuccessListener { release ->
    if (release != null) {
        // New release available. Start your update UI here.
    }
}.addOnFailureListener {
    // Handle failed check for new release. Fails with Status#NOT_IMPLEMENTED
    // if built for Play.
}

Java

firebaseAppDistribution.checkForNewRelease().addOnSuccessListener(release -> {
    if (release != null) {
        // New release available. Start your update UI here.
    }
}).addOnFailureListener(e -> {
    // Handle failed check for new release. Fails with Status#NOT_IMPLEMENTED
    // if built for Play.
});

當測試人員選擇從您的更新 UI 繼續更新時,調用updateApp()

Kotlin+KTX

firebaseAppDistribution.updateApp()
    .addOnProgressListener { updateState ->
      // Use updateState to show update progress.
    }

Java

firebaseAppDistribution.updateApp()
    .addOnProgressListener(updateState -> {
      // Use updateState to show update progress.
    });

第 4 步:構建和測試您的實施

通過使用 Firebase 控制台將構建分發給測試人員來構建您的應用並測試您的實施。

請訪問App Distribution Troubleshooting 指南以獲取有關常見問題的幫助,例如:

  • 測試人員未收到應用內提醒
  • 系統多次提示測試人員登錄 Google