Firebase OTP验证在Google Play应用中失效,Debug/Release模式正常
OTP验证异常:Google Play发布版本仅测试号码可用,Debug模式正常
问题详情
OTP验证在Google Play发布的应用中表现与Debug模式不一致,仅测试号码可正常使用。已配置本地生成的SHA1、SHA256密钥,模拟器Debug运行完全正常;但Google Play版本运行时,日志仅输出无关系统信息,无有效错误提示。
相关代码(checkOtp函数)
@override Future checkOtp(String verificationId, String smsCode) async{ try{ showLoadingDialog(); PhoneAuthCredential credential = PhoneAuthProvider.credential(verificationId: verificationId, smsCode: smsCode); var result = await _auth.signInWithCredential(credential) .then((value) { var user = value.user; if (kDebugMode) { print(user); print(value); } if (value.additionalUserInfo?.isNewUser == true) { Get.offNamed( RouteName.infoScreen,arguments: { "user":user }); } else { Get.offNamed( RouteName.homeScreen,arguments: { "user":user }); Utils.snackBar("Successfull", "Congrats on your successfully registration"); } return value; }); return result; } catch(e){ hideLoadingDialog(); handler.handleFirebaseLoginErrors(e); bool result = false; if(e.toString()=="[firebase_auth/invalid-verification-code] The verification code from SMS/TOTP is invalid. Please check and enter the correct verification code again.") { Utils.snackBar("Invalid Otp", "Please Try with another otp."); result = true; } return result; } }
Google Play版本运行日志
07:43:12.501 12725-12806 mali_winsys com.updevelop.wellness_z_mvvm D EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, EGLBoolean) returns 0x3000 2023-11-18 07:43:14.466 12725-12806 Surface com.updevelop.wellness_z_mvvm D Surface::disconnect(this=0x7a2bece000,api=1) 2023-11-18 07:43:14.469 12725-12725 View com.updevelop.wellness_z_mvvm D [Warning] assignParent to null: this = android.widget.FrameLayout{65c5d03 V.E...... ......ID 0,0-690,137} 2023-11-18 07:43:14.469 1332-1332 WindowManager system_server V mCurrentFocus: Window{ae17056 u0 com.updevelop.wellness_z_mvvm/com.updevelop.wellness_z_mvvm.MainActivity} newFocus Window{ae17056 u0 com.updevelop.wellness_z_mvvm/com.updevelop.wellness_z_mvvm.MainActivity}
解决思路与步骤
补全Google Play签名密钥到Firebase
Google Play App Signing会替换发布版本的签名密钥,你手动添加的本地SHA值只对Debug版本生效。打开Google Play控制台,进入应用 > 应用签名,复制Google生成的SHA1和SHA256值,添加到Firebase控制台的项目设置 > 应用 > 签名证书指纹中。优化异常日志捕获
当前代码只匹配特定错误字符串,且Debug模式外不打印关键信息。修改异常处理逻辑,打印完整异常详情:catch(e){ hideLoadingDialog(); print("[OTP_VERIFY_ERROR] ${e.toString()}"); // 强制打印错误日志 handler.handleFirebaseLoginErrors(e); bool result = false; // 用类型判断替代字符串匹配,更可靠 if(e is FirebaseAuthException && e.code == "invalid-verification-code") { Utils.snackBar("无效验证码", "请输入正确的验证码重试。"); result = true; } return result; }同时在应用初始化时开启Firebase Auth调试日志:
FirebaseAuth.instance.setLoggingEnabled(true);检查混淆规则
发布版本的代码混淆可能破坏Firebase Auth的核心逻辑,在proguard-rules.pro中添加以下规则:-keep class com.google.firebase.auth.** { *; } -keep class com.google.firebase.** { *; } -keep class com.google.android.gms.auth.** { *; }验证生产环境SMS服务可用性
Firebase测试号码是预配置的免SMS通道,生产号码需要确保应用所在地区支持Google的SMS验证服务,且应用已申请INTERNET权限。
内容的提问来源于stack exchange,提问作者Simarpreet Singh
相关产品推荐
相关产品推荐

