搭配 Android 偵錯供應商使用 App Check

只要在應用程式註冊 App Check 後, 應用程式所在的環境中,App Check 通常不會歸類為有效應用程式。 例如開發期間的模擬器,或從持續整合 (CI) 擷取而來 您就能為應用程式建立偵錯版本,該應用程式會使用 App Check 偵錯供應器,而非真正的認證提供者。

在模擬器中使用偵錯提供者

以互動方式在模擬器中執行應用程式時,使用偵錯提供者 (例如在開發期間) 執行下列步驟:

  1. 模組 (應用程式層級) Gradle 檔案中 (通常為 <project>/<app-module>/build.gradle.kts<project>/<app-module>/build.gradle)、 新增 Android 適用的 App Check 程式庫依附元件。建議您使用 Firebase Android BoM 管理程式庫版本管理

    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:33.1.2"))
    
        // Add the dependencies for the App Check libraries
        // When using the BoM, you don't specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-appcheck-debug")
    }
    

    使用 Firebase Android BoM 應用程式一律會使用相容的 Firebase Android 程式庫版本。

    (替代做法) 新增 Firebase 程式庫依附元件,「不使用」BoM

    如果選擇不使用 Firebase BoM,則須指定各個 Firebase 程式庫版本 都屬於依附元件行

    請注意,如果您在應用程式中使用多個 Firebase 程式庫,強烈建議您 建議使用 BoM 管理程式庫版本 相容。

    dependencies {
        // Add the dependencies for the App Check libraries
        // When NOT using the BoM, you must specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-appcheck-debug:18.0.0")
    }
    
    在尋找 Kotlin 專用的程式庫模組嗎?距離開始還有 2023 年 10 月 (Firebase BoM 32.5.0),Kotlin 和 Java 開發人員都能 依附於主要程式庫模組 (詳情請參閱 這項計畫的常見問題)。

  2. 在偵錯版本中,將 App Check 設為使用偵錯提供者 工廠:

    Kotlin+KTX

    Firebase.initialize(context = this)
    Firebase.appCheck.installAppCheckProviderFactory(
        DebugAppCheckProviderFactory.getInstance(),
    )

    Java

    FirebaseApp.initializeApp(/*context=*/ this);
    FirebaseAppCheck firebaseAppCheck = FirebaseAppCheck.getInstance();
    firebaseAppCheck.installAppCheckProviderFactory(
            DebugAppCheckProviderFactory.getInstance());
  3. 啟動應用程式,並觸發對 Firebase 後端服務的呼叫。當地 當 SDK 嘗試傳送要求給 後端。例如:

    D DebugAppCheckProvider: Enter this debug secret into the allow list in
    the Firebase Console for your project: 123a4567-b89c-12d3-e456-789012345678
  4. 在「App Check」(應用程式檢查) 部分 前往 Firebase 控制台,然後在應用程式的「管理偵錯權杖」中選擇「管理偵錯權杖」 溢位選單接著,註冊您在先前記錄中登入的偵錯權杖 採用

    「管理偵錯權杖」選單項目的螢幕截圖

註冊權杖後,Firebase 後端服務會接受該權杖的有效性。

因為這組權杖可讓您在沒有 有效的裝置,請務必確保該裝置的私密性。請勿將程式碼部署至 公開存放區,如果註冊權杖曾遭駭,請撤銷該組權杖 設定流程

使用偵錯供應商在 CI 環境中進行單元測試

在持續整合 (CI) 中使用偵錯供應商進行單元測試 請執行下列步驟:

  1. 在「App Check」(應用程式檢查) 部分 前往 Firebase 控制台,然後在應用程式的「管理偵錯權杖」中選擇「管理偵錯權杖」 溢位選單接著建立新的偵錯權杖。需在 下一步

    因為這項權杖可在沒有專案的情況下存取 Firebase 資源 有效的裝置,請務必確保該裝置的私密性。請勿將程式碼部署至 公開存放區,如果註冊權杖曾遭駭,請撤銷該組權杖 設定流程

    「管理偵錯權杖」選單項目的螢幕截圖

  2. 將剛剛建立的偵錯權杖新增至 CI 系統安全金鑰庫 (例如 GitHub Actions 的已加密密鑰)。 或 Travis CI 的加密變數)。

  3. 必要時,請設定持續整合系統,提供偵錯權杖 做為環境變數為變數命名 例如 APP_CHECK_DEBUG_TOKEN_FROM_CI

  4. 模組 (應用程式層級) Gradle 檔案中 (通常為 <project>/<app-module>/build.gradle.kts<project>/<app-module>/build.gradle)、 新增 Android 適用的 App Check 程式庫依附元件。建議您使用 Firebase Android BoM 管理程式庫版本管理

    Kotlin+KTX

    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:33.1.2"))
    
        // Add the dependency for the App Check library
        // When using the BoM, you don't specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-appcheck-debug")
    }
    

    使用 Firebase Android BoM 應用程式一律會使用相容的 Firebase Android 程式庫版本。

    (替代做法) 新增 Firebase 程式庫依附元件,「不使用」BoM

    如果選擇不使用 Firebase BoM,則須指定各個 Firebase 程式庫版本 都屬於依附元件行

    請注意,如果您在應用程式中使用多個 Firebase 程式庫,強烈建議您 建議使用 BoM 管理程式庫版本 相容。

    dependencies {
        // Add the dependency for the App Check library
        // When NOT using the BoM, you must specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-appcheck-debug:18.0.0")
    }
    
    在尋找 Kotlin 專用的程式庫模組嗎?距離開始還有 2023 年 10 月 (Firebase BoM 32.5.0),Kotlin 和 Java 開發人員都能 依附於主要程式庫模組 (詳情請參閱 這項計畫的常見問題)。

    Java

    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:33.1.2"))
    
        // Add the dependency for the App Check library
        // When using the BoM, you don't specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-appcheck-debug")
    }
    

    使用 Firebase Android BoM 應用程式一律會使用相容的 Firebase Android 程式庫版本。

    (替代做法) 新增 Firebase 程式庫依附元件,「不使用」BoM

    如果選擇不使用 Firebase BoM,則須指定各個 Firebase 程式庫版本 都屬於依附元件行

    請注意,如果您在應用程式中使用多個 Firebase 程式庫,強烈建議您 建議使用 BoM 管理程式庫版本 相容。

    dependencies {
        // Add the dependency for the App Check library
        // When NOT using the BoM, you must specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-appcheck-debug:18.0.0")
    }
    
    在尋找 Kotlin 專用的程式庫模組嗎?距離開始還有 2023 年 10 月 (Firebase BoM 32.5.0),Kotlin 和 Java 開發人員都能 依附於主要程式庫模組 (詳情請參閱 這項計畫的常見問題)。

  5. 將以下內容新增至 CI 建構變數的設定:

    testInstrumentationRunnerArguments["firebaseAppCheckDebugSecret"] =
        System.getenv("APP_CHECK_DEBUG_TOKEN_FROM_CI") ?: ""
    
  6. 在測試類別中,使用 DebugAppCheckTestHelper 納入任何程式碼 需要 App Check 權杖:

    Kotlin+KTX

    @RunWith(AndroidJunit4::class)
    class MyTests {
        private val debugAppCheckTestHelper =
            DebugAppCheckTestHelper.fromInstrumentationArgs()
    
        @Test
        fun testWithDefaultApp() {
            debugAppCheckTestHelper.withDebugProvider {
                // Test code that requires a debug AppCheckToken.
            }
        }
    
        @Test
        fun testWithNonDefaultApp() {
            debugAppCheckTestHelper.withDebugProvider(
                FirebaseApp.getInstance("nonDefaultApp")
            ) {
                // Test code that requires a debug AppCheckToken.
            }
        }
    }
    

    Java

    @RunWith(AndroidJunit4.class)
    public class YourTests {
        private final DebugAppCheckTestHelper debugAppCheckTestHelper =
                DebugAppCheckTestHelper.fromInstrumentationArgs();
    
        @Test
        public void testWithDefaultApp() {
            debugAppCheckTestHelper.withDebugProvider(() -> {
                // Test code that requires a debug AppCheckToken.
            });
        }
    
        @Test
        public void testWithNonDefaultApp() {
            debugAppCheckTestHelper.withDebugProvider(
                    FirebaseApp.getInstance("nonDefaultApp"),
                    () -> {
                        // Test code that requires a debug AppCheckToken.
                    });
        }
    }
    

應用程式在 CI 環境中執行時,Firebase 後端服務會接受 傳送的憑證