Flutter集成Firebase PhoneAuth遇SafetyNet Token错误及崩溃问题
Hey there! Let's get that frustrating PhoneAuth crash sorted out for you right away. Looking at your error logs, the root cause jumps out immediately:
java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/browser/customtabs/CustomTabsIntent$Builder;
Firebase Auth uses Recaptcha verification for phone authentication, which relies on the androidx.browser library to load the verification page. Your project is missing this critical dependency, which is why the app crashes when you call verifyPhoneNumber().
Step 1: Add the Missing Dependency
Open your app-level build.gradle file (usually located at app/build.gradle) and add this line to the dependencies block:
implementation "androidx.browser:browser:1.6.0"
You can replace 1.6.0 with the latest stable version of the library if you prefer. After adding it, click "Sync Now" to let Gradle download and integrate the dependency into your project.
Step 2: Clean Up Duplicate Code
Looking at your provided code, you've defined the verifyPhoneNumber method twice. This can lead to unexpected runtime behavior, so delete one of the duplicate definitions to keep your code clean and consistent.
Step 3: Address the SafetyNet Warning (Optional but Recommended)
Your logs also show a warning:
E/zza (12829): Problem retrieving SafetyNet Token: 7:
This typically means your app isn't set up for SafetyNet Attestation, which Firebase PhoneAuth uses to prevent abuse. To fix this:
- Open the Firebase Console for your project
- Navigate to Project Settings > App Check
- Follow the setup steps to enable SafetyNet for your Android app (you'll need your app's SHA-256 fingerprint added in Firebase settings first)
Verify the Fix
After adding the dependency and cleaning up your code, rebuild your app and test the phone auth flow again. The crash should be resolved, and the Recaptcha verification will load properly via CustomTabs.
内容的提问来源于stack exchange,提问作者Jerome Marshall

