在 Android 上使用 App Check 與調試提供者

如果您在為App Check 註冊您的應用程式後,想要在App Check 通常不會歸類為有效的環境中運行您的應用程序,例如開發期間的模擬器,或者在持續整合(CI) 環境中,您可以建立應用程式的偵錯版本,該版本使用 App Check 偵錯提供者而不是真正的證明提供者。

在模擬器中使用調試提供程序

若要在模擬器中以互動方式運行應用程式時(例如在開發期間)使用偵錯提供程序,請執行以下操作:

  1. 模組(應用程式層級)Gradle 檔案(通常<project>/<app-module>/build.gradle.kts<project>/<app-module>/build.gradle )中,新增 App Check 的依賴項Android 的函式庫。我們建議使用Firebase Android BoM來控制函式庫版本控制。

    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:32.8.0"))
    
        // 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 程式庫的相容版本。

    (替代方法)在不使用 BoM 的情況下新增 Firebase 庫依賴項

    如果您選擇不使用 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:17.1.2")
    }
    
    正在尋找 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. 在 Firebase 控制台的「套用檢查」部分中,從應用程式的溢出選單中選擇「管理偵錯令牌」 。然後,註冊您在上一個步驟中記錄的偵錯令牌。

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

註冊令牌後,Firebase 後端服務會將其視為有效。

由於此代幣允許在沒有有效設備的情況下存取您的 Firebase 資源,因此保持其隱私至關重要。不要將其提交到公共儲存庫,如果註冊的令牌遭到洩露,請立即在 Firebase 控制台中撤銷它。

使用調試提供者在 CI 環境中進行單元測試

若要在持續整合 (CI) 環境中使用偵錯提供者進行單元測試,請執行下列操作:

  1. 在 Firebase 控制台的「套用檢查」部分中,從應用程式的溢出選單中選擇「管理偵錯令牌」 。然後,建立一個新的調試令牌。您在下一步中將需要該令牌。

    由於此代幣允許在沒有有效設備的情況下存取您的 Firebase 資源,因此保持其隱私至關重要。不要將其提交到公共儲存庫,如果註冊的令牌遭到洩露,請立即在 Firebase 控制台中撤銷它。

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

  2. 將您剛剛建立的偵錯令牌新增至 CI 系統的安全金鑰儲存(例如 GitHub Actions 的加密金鑰或 Travis CI 的加密變數)。

  3. 如有必要,請設定 CI 系統以使偵錯權杖在 CI 環境中作為環境變數可用。將變數命名為APP_CHECK_DEBUG_TOKEN_FROM_CI之類的名稱。

  4. 模組(應用程式層級)Gradle 檔案(通常<project>/<app-module>/build.gradle.kts<project>/<app-module>/build.gradle )中,新增 App Check 的依賴項Android 的函式庫。我們建議使用Firebase Android BoM來控制函式庫版本控制。

    Kotlin+KTX

    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:32.8.0"))
    
        // 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 程式庫的相容版本。

    (替代方法)在不使用 BoM 的情況下新增 Firebase 庫依賴項

    如果您選擇不使用 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:17.1.2")
    }
    
    正在尋找 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:32.8.0"))
    
        // 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 程式庫的相容版本。

    (替代方法)在不使用 BoM 的情況下新增 Firebase 庫依賴項

    如果您選擇不使用 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:17.1.2")
    }
    
    正在尋找 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 後端服務將接受其發送的有效令牌。