23

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();

        }
    }
}
coolamz
  • 377
  • 2
  • 3
  • 11
  • 1
    For signed APK, you need to change your `google-services.json`!! – AndiGeeky Nov 29 '16 at 11:27
  • 4
    Change your Google `SHA-1` hash for using your release keystore and update `google-services.json`. !! – AndiGeeky Nov 29 '16 at 11:28
  • Obtain SHA-1 from release key and update in google console. And get `google-services.json` only if you have changed package name. – Arpit Patel Nov 29 '16 at 11:31
  • yes you need `google-services.json` for signed apk – Rajesh Nov 29 '16 at 11:36
  • Thank you so much for help. I already have SHA1 certificate fingerprints included in my console also. What to change in google-services.json ? – coolamz Nov 29 '16 at 11:36
  • Possible duplicate of [Google sign in not working after publishing in play store](https://stackoverflow.com/questions/39318370/google-sign-in-not-working-after-publishing-in-play-store) – Priyankchoudhary Sep 19 '17 at 19:11

9 Answers9

34

I had the same problem and I fixed the problem using these methods.

  1. Complete app signing on your Play store console using these instructions How to enable Google Play App Signing

  2. 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.

enter image description here

John Ravi
  • 1,190
  • 1
  • 12
  • 21
  • 3
    Took me hours to resolve the issue. This should be included in the firebase "Authenticate Using Google Sign-In on Android" instructions . – Faisal Jul 30 '19 at 11:36
  • 2
    What is the difference between keytool -list -v -keystore and above thing? – LOG_TAG Jun 26 '20 at 05:47
  • This was it for me also. This hash is different than the SHA-1 you get from your own keystore. This SHA-1 needs to be added to your firebase app's allowed fingerprints along with your others so you'll have at least 3 (debug, release, google's). Google's description of this signing cert is "This is the public certificate for the app signing key that Google uses to sign each of your releases. Use it to register your key with API providers. The app signing key itself is not accessible, and is kept on a secure Google server." – notmystyle Jan 27 '21 at 06:07
12

You are having fingerprint certificate in console, but its only for debugging purposes, for signed apk you need a production fingerprint certificate, and you can get one by

 c:\Program Files\Java\jdk1.6.25\bin>keytool -list -v -keystore c:\you_key_here.key
Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77
6

Steps to locate the SHA-1 key: 1) Go to your Google Play Console 2) Select the targeting app 3) Go to "App signing" under "Release management" 4) The SHA-1 key can be found under "App Signing Certificate"

It took me many hours to resolve the issue and I hope this can help someone who runs into the same issue.

Brijpath
  • 73
  • 1
  • 6
6

For Windows:

In Reality:

keytool -list -v -keystore "E:\Google Drive\MeshstocksSyncronize\AndroidKey\BRB\bangaliRussainBusiness.jks"

for Example :

keytool -list -v -keystore "**Here is your path**"

Paste this line in your android studio terminal.

Follow the video for better understanding: https://youtu.be/TYrmT8Emadg

Jerry Okafor
  • 3,710
  • 3
  • 17
  • 26
Sushen Biswas
  • 522
  • 1
  • 6
  • 24
4

Maybe I am a bit late, but for everyone experiencing the issue and not having found a solution, here is mine:

I have hosted my app on Firebase and enabled Firebase Auth for Email and Password.

The app worked fine in debug mode, but as I signed the app and ran it am my phone, NO API worked. I found out that it was due to my api-key in google-cloud being restricted. I found out that I only used the SHA1 from my debug app, however I needed to add the SHA1 release key as well.

Generating your SHA1 key is fairly simple:

  • Follow this tutorial to genereate a key for your app: Firebase Deployment Tutorial

  • generate your SHA1 from that key using:

    keytool -list -v -keystore {keystore_name} -alias {alias_name}
    

If you get an error that the command "keystore" cannot be found, navigate to your Java Runtime Environment (jre) directory and use the command there. An example path can be found here:

C:\"Program Files"\Android\"Android Studio"\jre\bin
Dharman
  • 30,962
  • 25
  • 85
  • 135
Lars
  • 1,250
  • 9
  • 25
4

Firebase Console method With some new updates

There is a really easy method directly from Firebase Console.

Step 01: Just go to Settings > Integrations in Firebase Console

enter image description here

Step 02: Then hit the link button on the Google Play card, to make the SHA-1 and SHA-256 code from your Google Play console directly added to your Firebase console.

enter image description here

Then it will be added to your Firebase console check by going to Settings > Integrations

enter image description here

Step 03: Here you have to manually add the Upload key Certificate SHA-1 from the Google Play console manually to the firebase google play console

enter image description here

Step 04: Then download the new google-services.json and replace it with the one in Android studio that you added when creating the Firebase project.

halfer
  • 19,824
  • 17
  • 99
  • 186
DIVYANSHU SAHU
  • 1,035
  • 11
  • 24
2

for anyone new coming in, the keys are moved to

Play Store Console > your app > release > setup > app integrity

here you will find both SHA-1 and SHA-256 keys, add them in your firebase project and you're good.

1

The SHA-1 Certificate Fingerprint can be found under Release > Setup > App Integrity

Google Play App Signing Key Certificate

Next is to add the SHA-1 Certificate Fingerprint in your Firebase Console. Go to Project Settings > General > Add Fingerprint Firebase General Project Settings

After doing the following, I redownloaded the new google-services.json file and replaced the one in my project.

As for my case. After doing the above steps, the published app still wasn't able to connect to Firebase. What I did is to manually add the SHA-1 Certificate Fingerprint in Google Cloud Platform > API and Services > Credentials then select the Android Key (auto created by Firebase).

Google Cloud Platform API and Services Credentials

Nickr
  • 216
  • 4
  • 8
1

You just need to find debug SHA1 and SHA256 for authentication in debug mode

For Windows:

open terminal in your IDE and type: cd android

and then

gradlew signingReport

and copy debug SHA1 and SHA256 and paste them in firebase console

------------------for signed apk -------------------------------

Open terminal type:

keytool-list-v-keystore"{path of jks file by which you signed apk}" alias {alias of keystore used to sign apk}-storepass{password of keystore}-keypass{password of keystore}

you will get release SHA1 and SHA256 paste both these in firebase console

this will work

CodeChanger
  • 7,953
  • 5
  • 49
  • 80