You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android发布版Google Sign in选账户后返回登录页异常

Android发布版Google登录异常排查方案

问题描述

Google Sign-in在iOS发布版可正常使用,但Android发布版点击登录按钮启动流程、选择账户后直接返回登录页,无后续操作;Android模拟器中运行正常。已检查release-keys的SHA-1和SHA-256并更新google-service.json,使用google_sign_in包,核心实现代码如下:

signWithGoogle() async {
  final googleSignin = GoogleSignIn(scopes: ['email']);
  final GoogleSignInAccount? googleUser =
  await googleSignin.signIn();
  final GoogleSignInAuthentication googleAuth =
  await googleUser!.authentication;
  final GoogleAuthCredential credential = GoogleAuthProvider.credential(
    accessToken: googleAuth.accessToken,
    idToken: googleAuth.idToken,
  ) as GoogleAuthCredential;
  final res = await FirebaseAuth.instance.signInWithCredential(credential);
  await FirebaseFirestore.instance.collection('Users').doc(res.user!.uid).set({
    'name' : res.user!.displayName,
    'email': res.user!.email,
    'uid': res.user!.uid,
    'image': res.user!.photoURL,
  }, SetOptions(merge : true));
  return res.user;
}

排查修复步骤

  • 配置混淆规则
    Android发布版默认开启代码混淆,google_sign_in和Firebase相关类可能被混淆导致功能异常。在android/app/proguard-rules.pro中添加以下规则:

    -keep class com.google.android.gms.auth.api.signin.** { *; }
    -keep class com.google.firebase.auth.** { *; }
    -keep class com.google.android.gms.common.api.** { *; }
    
  • 核对签名密钥一致性
    重新生成release keystore的SHA值并与Google Cloud控制台配置核对,确保打包使用的密钥和云端配置完全一致:

    keytool -list -v -keystore your-release-keystore.jks -alias your-alias
    

    同步SHA值到Firebase项目设置后,重新下载google-service.json替换到android/app目录。

  • 添加异常捕获定位问题
    当前代码未处理异常,登录流程中断可能是因为步骤中抛出错误未被捕获。修改代码添加异常处理,打印错误日志:

    signWithGoogle() async {
      try {
        final googleSignin = GoogleSignIn(scopes: ['email']);
        final GoogleSignInAccount? googleUser = await googleSignin.signIn();
        if (googleUser == null) {
          return null;
        }
        final GoogleSignInAuthentication googleAuth = await googleUser.authentication;
        final GoogleAuthCredential credential = GoogleAuthProvider.credential(
          accessToken: googleAuth.accessToken,
          idToken: googleAuth.idToken,
        );
        final res = await FirebaseAuth.instance.signInWithCredential(credential);
        await FirebaseFirestore.instance.collection('Users').doc(res.user!.uid).set({
          'name' : res.user!.displayName,
          'email': res.user!.email,
          'uid': res.user!.uid,
          'image': res.user!.photoURL,
        }, SetOptions(merge : true));
        return res.user;
      } catch (e) {
        print('Google登录错误: $e');
        return null;
      }
    }
    

    打包发布版后通过adb logcat查看错误信息,定位具体问题。

  • 检查Google Play App Signing配置
    若应用开启了Google Play App Signing,Google会替换签名密钥,需在Google Play控制台的应用签名页面获取Google生成的SHA-1/SHA-256,添加到Firebase项目设置中。

内容的提问来源于stack exchange,提问作者be.fluidly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 19:45:55