如果您已為應用程式註冊 App Check,但想在 App Check 通常不會歸類為有效的環境中執行應用程式 (例如開發期間的模擬器,或來自持續整合 (CI) 環境),可以建立應用程式的偵錯版本,使用 App Check 偵錯供應器,而非實際的認證供應器。
在模擬器中使用偵錯提供者
如要在模擬器中以互動方式執行應用程式時 (例如在開發期間) 使用偵錯供應器,請按照下列步驟操作:
在模組 (應用程式層級) 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:34.0.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:19.0.0") }
在偵錯版本中,將 App Check 設為使用偵錯供應器工廠:
Kotlin
Firebase.initialize(context = this) Firebase.appCheck.installAppCheckProviderFactory( DebugAppCheckProviderFactory.getInstance(), )
Java
FirebaseApp.initializeApp(/*context=*/ this); FirebaseAppCheck firebaseAppCheck = FirebaseAppCheck.getInstance(); firebaseAppCheck.installAppCheckProviderFactory( DebugAppCheckProviderFactory.getInstance());
啟動應用程式,並觸發對 Firebase 後端服務的呼叫。當 SDK 嘗試將要求傳送至後端時,系統會記錄本機偵錯權杖。例如:
D DebugAppCheckProvider: Enter this debug secret into the allow list in the Firebase Console for your project: 123a4567-b89c-12d3-e456-789012345678
在 Firebase 管理中心的「App Check」部分,從應用程式的溢位選單中選擇「管理偵錯權杖」。然後註冊您在上一個步驟中登入的偵錯權杖。
註冊權杖後,Firebase 後端服務就會接受該權杖。
這個權杖可讓您存取 Firebase 資源,不必使用有效裝置,因此請務必妥善保管。請勿將權杖提交至公開存放區,如果註冊的權杖遭到盜用,請立即在 Firebase 控制台中撤銷權杖。
在 CI 環境中,使用偵錯供應器進行單元測試
如要在持續整合 (CI) 環境中,使用偵錯供應器進行單元測試,請按照下列步驟操作:
在 Firebase 管理中心的「App Check」部分,從應用程式的溢位選單中選擇「管理偵錯權杖」。接著,建立新的偵錯權杖。您會在下一個步驟中使用這個權杖。
這個權杖可讓使用者在沒有有效裝置的情況下存取 Firebase 資源,因此請務必妥善保管。請勿將權杖提交至公開存放區,如果註冊的權杖遭到盜用,請立即在 Firebase 控制台中撤銷權杖。
將您剛建立的偵錯權杖新增至 CI 系統的安全金鑰儲存區 (例如 GitHub Actions 的加密密碼或 Travis CI 的加密變數)。
如有必要,請設定 CI 系統,在 CI 環境中以環境變數的形式提供偵錯權杖。將變數命名為
APP_CHECK_DEBUG_TOKEN_FROM_CI
等名稱。在模組 (應用程式層級) Gradle 檔案 (通常是
<project>/<app-module>/build.gradle.kts
或<project>/<app-module>/build.gradle
) 中,加入 Android 適用的 App Check 程式庫依附元件。建議使用 Firebase Android BoM 控制程式庫版本。Kotlin
dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:34.0.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:19.0.0") }
Java
dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:34.0.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:19.0.0") }
在 CI 建構變數的設定中新增下列內容:
testInstrumentationRunnerArguments["firebaseAppCheckDebugSecret"] = System.getenv("APP_CHECK_DEBUG_TOKEN_FROM_CI") ?: ""
在測試類別中,使用
DebugAppCheckTestHelper
包裝任何需要 App Check 權杖的程式碼:Kotlin
@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 後端服務會接受應用程式傳送的權杖,並視為有效。