问题描述
我正在使用 Firebase Google 登录.它通过 USB 调试完美运行.但是当我生成签名的 APK 时,它停止工作.它无法登录.在 Android 5.1 & 上使用它安卓 6.0.1.此外,如果谷歌播放服务没有更新,它会提示用户更新它,因为用户可能会离开应用程序.如何关闭提示并解决错误?
公共类 MainActivity 扩展 AppCompatActivity 实现 GoogleApiClient.OnConnectionFailedListener,View.OnClickListener {私人按钮跳过;private static final String TAG = "GoogleActivity";私有静态最终 int RC_SIGN_IN = 9001;私人进度条 pb;//[开始declare_auth]私有 FirebaseAuth mAuth;//[结束声明_身份验证]//[开始declare_auth_listener]私有 FirebaseAuth.AuthStateListener mAuthListener;//[结束declare_auth_listener]私人 GoogleApiClient mGoogleApiClient;私有 TextView mStatusTextView;私有 TextView mDetailTextView;@覆盖protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);pb=(ProgressBar)findViewById(R.id.signpro);pb.setVisibility(View.GONE);跳过=(按钮)findViewById(R.id.btnskip);skip.setOnClickListener(new View.OnClickListener() {@覆盖public void onClick(View v) {setContentView(R.layout.activity_home);}});//视图//按钮监听器findViewById(R.id.sign_in_button).setOnClickListener(this);//[开始config_signin]//配置谷歌登录GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestIdToken(getString(R.string.default_web_client_id)).requestEmail().建造();//[END config_signin]mGoogleApiClient = new GoogleApiClient.Builder(this).enableAutoManage(this/* FragmentActivity */, this/* OnConnectionFailedListener */).addApi(Auth.GOOGLE_SIGN_IN_API, gso).建造();//[开始初始化_auth]mAuth = FirebaseAuth.getInstance();//[END initialize_auth]//[开始auth_state_listener]mAuthListener = new FirebaseAuth.AuthStateListener() {@覆盖public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {FirebaseUser 用户 = firebaseAuth.getCurrentUser();如果(用户!= null){//用户已登录Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());setContentView(R.layout.activity_home);} 别的 {//用户已退出Log.d(TAG, "onAuthStateChanged:signed_out");}//[START_EXCLUDE]//[END_EXCLUDE]}};//[END auth_state_listener]}//[开始 on_start_add_listener]@覆盖公共无效 onStart() {super.onStart();mAuth.addAuthStateListener(mAuthListener);}//[END on_start_add_listener]//[开始 on_stop_remove_listener]@覆盖公共无效 onStop() {super.onStop();如果(mAuthListener != null){mAuth.removeAuthStateListener(mAuthListener);}}//[结束 on_stop_remove_listener]//[START onactivityresult]@覆盖public void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);//从 GoogleSignInApi.getSignInIntent(...); 启动 Intent 返回的结果;如果(请求代码 == RC_SIGN_IN){GoogleSignInResult 结果 = Auth.GoogleSignInApi.getSignInResultFromIntent(data);如果(result.isSuccess()){Toast.makeText(this, "正在登录.请稍候...", Toast.LENGTH_LONG).show();//Google 登录成功,使用 Firebase 进行身份验证GoogleSignInAccount 帐户 = result.getSignInAccount();firebaseAuthWithGoogle(帐户);Toast.makeText(this, "登录成功!", Toast.LENGTH_SHORT).show();} 别的 {//Google 登录失败,适当更新 UI//[START_EXCLUDE]Toast.makeText(this, "Google 登录失败.请跳过.", Toast.LENGTH_SHORT).show();pb.setVisibility(View.GONE);//[END_EXCLUDE]}}}//[END onactivityresult]//[开始 auth_with_google]private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());//[START_EXCLUDE 静音]//[END_EXCLUDE]AuthCredential 凭证 = GoogleAuthProvider.getCredential(acct.getIdToken(), null);mAuth.signInWithCredential(凭证).addOnCompleteListener(this, new OnCompleteListener() {@覆盖public void onComplete(@NonNull Task task) {Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());//如果登录失败,向用户显示一条消息.如果登录成功//auth 状态监听器将被通知和逻辑来处理//登录用户可以在监听器中处理.如果 (!task.isSuccessful()) {Log.w(TAG, "signInWithCredential", task.getException());Toast.makeText(MainActivity.this, "身份验证失败.",Toast.LENGTH_SHORT).show();}//[START_EXCLUDE]//[END_EXCLUDE]}});}//[END auth_with_google]//[开始登录]私人无效登录(){pb.setVisibility(View.VISIBLE);意图 signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);startActivityForResult(signInIntent, RC_SIGN_IN);}//[结束登录]@覆盖public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {//发生了无法解决的错误,Google API(包括登录)将不会//能得到的.Log.d(TAG, "onConnectionFailed:" + connectionResult);Toast.makeText(this, "Google Play Services error.", Toast.LENGTH_SHORT).show();}@覆盖public void onClick(View v) {int i = v.getId();如果(我 == R.id.sign_in_button){登入();}}}
我遇到了同样的问题,我使用这些方法解决了这个问题.
使用这些说明在 Play 商店控制台上完成应用签名
I'm using Firebase Google Sign in. It works perfectly via USB debugging. But when I generate signed APK, it stops working. Its not able to sign in. Using it on Android 5.1 & Android 6.0.1. Also, if the Google play services is not updated, it gives user prompt to update it because of which user may leave the app. How can I turn off the prompt and solve the error?
public class MainActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener, View.OnClickListener { private Button skip; private static final String TAG = "GoogleActivity"; private static final int RC_SIGN_IN = 9001; private ProgressBar pb; // [START declare_auth] private FirebaseAuth mAuth; // [END declare_auth] // [START declare_auth_listener] private FirebaseAuth.AuthStateListener mAuthListener; // [END declare_auth_listener] private GoogleApiClient mGoogleApiClient; private TextView mStatusTextView; private TextView mDetailTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pb=(ProgressBar)findViewById(R.id.signpro); pb.setVisibility(View.GONE); skip=(Button)findViewById(R.id.btnskip); skip.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { setContentView(R.layout.activity_home); } }); // Views // Button listeners findViewById(R.id.sign_in_button).setOnClickListener(this); // [START config_signin] // Configure Google Sign In GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestIdToken(getString(R.string.default_web_client_id)) .requestEmail() .build(); // [END config_signin] mGoogleApiClient = new GoogleApiClient.Builder(this) .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */) .addApi(Auth.GOOGLE_SIGN_IN_API, gso) .build(); // [START initialize_auth] mAuth = FirebaseAuth.getInstance(); // [END initialize_auth] // [START auth_state_listener] mAuthListener = new FirebaseAuth.AuthStateListener() { @Override public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { FirebaseUser user = firebaseAuth.getCurrentUser(); if (user != null) { // User is signed in Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid()); setContentView(R.layout.activity_home); } else { // User is signed out Log.d(TAG, "onAuthStateChanged:signed_out"); } // [START_EXCLUDE] // [END_EXCLUDE] } }; // [END auth_state_listener] } // [START on_start_add_listener] @Override public void onStart() { super.onStart(); mAuth.addAuthStateListener(mAuthListener); } // [END on_start_add_listener] // [START on_stop_remove_listener] @Override public void onStop() { super.onStop(); if (mAuthListener != null) { mAuth.removeAuthStateListener(mAuthListener); } } // [END on_stop_remove_listener] // [START onactivityresult] @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...); if (requestCode == RC_SIGN_IN) { GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); if (result.isSuccess()) { Toast.makeText(this, "Signing you in. Please Wait...", Toast.LENGTH_LONG).show(); // Google Sign In was successful, authenticate with Firebase GoogleSignInAccount account = result.getSignInAccount(); firebaseAuthWithGoogle(account); Toast.makeText(this, "Sign In Successful!", Toast.LENGTH_SHORT).show(); } else { // Google Sign In failed, update UI appropriately // [START_EXCLUDE] Toast.makeText(this, "Google Sign In failed. Please Skip.", Toast.LENGTH_SHORT).show(); pb.setVisibility(View.GONE); // [END_EXCLUDE] } } } // [END onactivityresult] // [START auth_with_google] private void firebaseAuthWithGoogle(GoogleSignInAccount acct) { Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId()); // [START_EXCLUDE silent] // [END_EXCLUDE] AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null); mAuth.signInWithCredential(credential) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful()); // If sign in fails, display a message to the user. If sign in succeeds // the auth state listener will be notified and logic to handle the // signed in user can be handled in the listener. if (!task.isSuccessful()) { Log.w(TAG, "signInWithCredential", task.getException()); Toast.makeText(MainActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show(); } // [START_EXCLUDE] // [END_EXCLUDE] } }); } // [END auth_with_google] // [START signin] private void signIn() { pb.setVisibility(View.VISIBLE); Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); startActivityForResult(signInIntent, RC_SIGN_IN); } // [END signin] @Override public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { // An unresolvable error has occurred and Google APIs (including Sign-In) will not // be available. Log.d(TAG, "onConnectionFailed:" + connectionResult); Toast.makeText(this, "Google Play Services error.", Toast.LENGTH_SHORT).show(); } @Override public void onClick(View v) { int i = v.getId(); if (i == R.id.sign_in_button) { signIn(); } } }
解决方案I had the same problem and I fixed the problem using these methods.
Complete app signing on your Play store console using these instructions How to enable Google Play App Signing
Go to your Firebase console Settings > General > Your apps > (Select your project) then add the SHA-1 from App signing certificate from your Play store. You can get SHA-1 by going to Release management -> App signing as show in this image.
这篇关于Firebase 身份验证在签名的 APK 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!