GoogleSignIn.getLastSignedInAccount() returns 发布版本为空

GoogleSignIn.getLastSignedInAccount() returns null on release build

我正在尝试将 google 登录添加到我的 android 应用程序。在调试版本上一切正常。但是当我在 Google Play 上推送用于内部测试的 apk 时,它抛出 Google 登录 API 异常 10。我应该向我的控制台添加任何额外的东西吗?

到目前为止,我已经做了以下事情,

  1. 创建了新的 Firebase 项目

  2. 已将 SHA-1 添加到 Firebase 控制台。

  3. 已从 firebase 下载 google-services.json 并复制到应用 文件夹。

  4. 在我的 https://console.cloud.google.com/apis/credentials 页面的所有内容都由 firebase 自动填充。所以,我没有做 那里有什么。

  5. 将所有需要的库添加到 android 项目

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_login);
            SignInButton signInButton = findViewById(R.id.sign_in_button);
            signInButton.setSize(SignInButton.SIZE_WIDE);
    
            GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestIdToken(getString(R.string.default_web_client_id))
                    .requestEmail()
                    .build();
    mGoogleSignInClient = GoogleSignIn.getClient(this, so);
    }
    

    @Override public void onActivityResult(int requestCode, int resultCode, Intent 数据) { super.onActivityResult(requestCode, resultCode, 数据);

        // Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            // The Task returned from this call is always completed, no need to attach
            // a listener.
            // Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            try {
                // Google Sign In was successful, authenticate with Firebase
                GoogleSignInAccount account = task.getResult(ApiException.class);
                Log.e(TAG, "firebaseAuthWithGoogle:" + account.getId());
                //firebaseAuthWithGoogle(account.getIdToken());
            } catch (ApiException e) {
                // Google Sign In failed, update UI appropriately
                Log.e(TAG, "Google sign in failed", e);
            }
            handleSignInResult(task);
        }
    }
    

处理登录结果;

 private void handleSignInResult(Task<GoogleSignInAccount> 
   completedTask) {
                String personName = "", personEmail = "", aid = "";
                 Uri personPhoto = Uri.parse("");
                // GoogleSignInAccount acct = 
    GoogleSignIn.getLastSignedInAccount(this);
                  GoogleSignInAccount acct = completedTask.getResult();
                  if (acct != null) {
                     personName = acct.getDisplayName();
                     personEmail = acct.getEmail();
                     personPhoto = acct.getPhotoUrl();
                     aid = acct.getId();
                     Log.e("ID_TOKEN", acct.getIdToken() + "");
                 } 
      }

在我从 Google Play console 下的 Release -> Setup -> App integrity 添加 SHA-1 后,它现在可以工作了,正如@lasagnakid77 在他的评论中提到的那样。