如果在為 App Check 註冊您的應用程序後,您希望在 App Check 通常不會歸類為有效的環境中運行您的應用程序,例如開發期間的模擬器,或來自持續集成 (CI) 環境,您可以創建您的應用程序的調試版本,使用 App Check 調試提供程序而不是真正的證明提供程序。
在模擬器中使用調試提供程序
要在模擬器中以交互方式運行您的應用程序時(例如在開發期間)使用調試提供程序,請執行以下操作:
在您的模塊(應用級)Gradle 文件(通常是
app/build.gradle
)中,聲明 App Check Android 庫的依賴項:Kotlin+KTX
dependencies { implementation 'com.google.firebase:firebase-appcheck-debug:17.0.1' }
Java
dependencies { implementation 'com.google.firebase:firebase-appcheck-debug:17.0.1' }
在您的調試版本中,將 App Check 配置為使用調試提供程序工廠:
Kotlin+KTX
Firebase.initialize(context = this) val firebaseAppCheck = FirebaseAppCheck.getInstance() firebaseAppCheck.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部分,從應用的溢出菜單中選擇Manage debug tokens 。然後,註冊您在上一步中登錄的調試令牌。
註冊令牌後,Firebase 後端服務會將其視為有效。
由於此令牌允許在沒有有效設備的情況下訪問您的 Firebase 資源,因此將其保密至關重要。不要將其提交到公共存儲庫,如果註冊的令牌遭到破壞,請立即在 Firebase 控制台中將其撤銷。
在 CI 環境中使用調試提供程序進行單元測試
要在持續集成 (CI) 環境中使用調試提供程序進行單元測試,請執行以下操作:
在 Firebase 控制台的App Check部分,從應用的溢出菜單中選擇Manage debug tokens 。然後,創建一個新的調試令牌。您將在下一步中需要該令牌。
由於此令牌允許在沒有有效設備的情況下訪問您的 Firebase 資源,因此將其保密至關重要。不要將其提交到公共存儲庫,如果註冊的令牌遭到破壞,請立即在 Firebase 控制台中將其撤銷。
將您剛剛創建的調試令牌添加到 CI 系統的安全密鑰存儲(例如,GitHub Actions 的加密機密或 Travis CI 的加密變量)。
如有必要,配置您的 CI 系統,使您的調試令牌在 CI 環境中作為環境變量可用。將變量命名為
APP_CHECK_DEBUG_TOKEN_FROM_CI
之類的名稱。在您的模塊(應用級)Gradle 文件中(通常
app/build.gradle
):為 App Check Android 調試庫聲明測試依賴:
Kotlin+KTX
dependencies { androidTestImplementation 'com.google.firebase:firebase-appcheck-debug-testing:17.0.1' }
Java
dependencies { androidTestImplementation 'com.google.firebase:firebase-appcheck-debug-testing:17.0.1' }
將以下內容添加到 CI 構建變體的配置中:
testInstrumentationRunnerArgument "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 後端服務將接受它發送的有效令牌。