如果您在為 App Check 註冊您的應用程序後,想要在 App Check 通常不會歸類為有效的環境中運行您的應用程序,例如開發期間的模擬器,或者在持續集成 (CI) 環境中,您可以創建應用程序的調試版本,該版本使用 App Check 調試提供程序而不是真正的證明提供程序。
在模擬器中使用調試提供程序
要在模擬器中以交互方式運行應用程序時(例如在開發期間)使用調試提供程序,請執行以下操作:
在模塊(應用程序級)Gradle 文件(通常
<project>/<app-module>/build.gradle.kts
或<project>/<app-module>/build.gradle
)中,添加 App Check 的依賴項安卓庫。我們建議使用Firebase Android BoM來控制庫版本控制。Kotlin+KTX
dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:32.3.1")) // 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")
implementation("com.google.firebase:firebase-appcheck-ktx") }通過使用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.0.1")
implementation("com.google.firebase:firebase-appcheck-ktx:17.0.1") }Java
dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:32.3.1")) // 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.0.1") }
在調試版本中,配置 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());
啟動應用程序並觸發對 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 控制台的“應用檢查”部分中,從應用的溢出菜單中選擇“管理調試令牌” 。然後,註冊您在上一步中登錄的調試令牌。
註冊令牌後,Firebase 後端服務會將其視為有效。
由於此令牌允許在沒有有效設備的情況下訪問您的 Firebase 資源,因此保持其私密性至關重要。不要將其提交到公共存儲庫,如果註冊的令牌遭到洩露,請立即在 Firebase 控制台中撤銷它。
使用調試提供程序在 CI 環境中進行單元測試
要在持續集成 (CI) 環境中使用調試提供程序進行單元測試,請執行以下操作:
在 Firebase 控制台的“應用檢查”部分中,從應用的溢出菜單中選擇“管理調試令牌” 。然後,創建一個新的調試令牌。您在下一步中將需要該令牌。
由於此令牌允許在沒有有效設備的情況下訪問您的 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
)中,添加 App Check 的依賴項安卓庫。我們建議使用Firebase Android BoM來控制庫版本控制。Kotlin+KTX
dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:32.3.1")) // Add the dependency for the App Check library // When using the BoM, you don't specify versions in Firebase library dependencies androidTestImplementation("com.google.firebase:firebase-appcheck-debug-testing") }
通過使用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 androidTestImplementation("com.google.firebase:firebase-appcheck-debug-testing:17.0.1") }
Java
dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:32.3.1")) // Add the dependency for the App Check library // When using the BoM, you don't specify versions in Firebase library dependencies androidTestImplementation("com.google.firebase:firebase-appcheck-debug-testing") }
通過使用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 androidTestImplementation("com.google.firebase:firebase-appcheck-debug-testing:17.0.1") }
將以下內容添加到 CI 構建變體的配置中:
testInstrumentationRunnerArguments["firebaseAppCheckDebugSecret"] = System.getenv("APP_CHECK_DEBUG_TOKEN_FROM_CI") ?: ""
在您的測試類中,使用
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 後端服務將接受其發送的有效令牌。