5

I have a very serious problem with implementing Google sign in with Firebase. I can log in with my emulator Android 7.0(SDK built for x86_64), but if I upload my project to my phone (Android 6.0) I cannot log in with Google. The only thing I see, is that the app closes. In rare situations, it will log in if I double-tap login button quickly, but won't get credentials for further use. What might be the issue?

The code in LoginActivity

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == FirebaseConstants.LOG_IN_REQUEST){

        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        Intent i = new Intent(LoginActivity.this, MainActivity.class);
        startActivity(i);

    }
    if(requestCode == FirebaseConstants.LOG_OUT_REQUEST){
        TastyToast.makeText(this, "Logged out successfully", TastyToast.LENGTH_LONG, TastyToast.SUCCESS);
    }
}

Here is the code that is in the onCreate method (button click):

    btnGoogleSignIn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(NetworkUtil.isNetworkAvailable(LoginActivity.this)){
                googleLogin.logIn(LoginActivity.this);
            }else{
                TastyToast.makeText(LoginActivity.this, "Network is unavailable", TastyToast.LENGTH_LONG, TastyToast.ERROR);
            }
        }
    });

The google login instance

    public class GoogleLogin extends Observable implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{

        private final GoogleApiClient googleApiClient;
        private final WeakReference<Activity> activityWeakReference;
        private static GoogleLogin googleLogin;

        public GoogleLogin(final Activity activity) {
            activityWeakReference = new WeakReference<>(activity);
            // Configure Google Sign In
            GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestIdToken(activity.getString(R.string.default_web_client_id))
                    .requestEmail()
                    .build();
            googleApiClient = new GoogleApiClient.Builder(activity)
                    .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                    .build();
        }

        @Override
        public void onConnected(@Nullable Bundle bundle) {
        }

        @Override
        public void onConnectionSuspended(int i) {

        }

        @Override
        public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        }

        public static GoogleLogin getInstance(Activity activity) {
            if (null == googleLogin) {
                googleLogin = new GoogleLogin(activity);
            }

            return googleLogin;
        }


        public void logIn(Activity activity){
            Context c = activityWeakReference.get().getApplicationContext();
            googleApiClient.connect();
            if(googleApiClient.isConnected()){
                Intent i = new Intent(c, MainActivity.class);
                googleApiClient.connect();
                activity.startActivity(i); // this wont start an activity
            }else if(!googleApiClient.isConnected()){
                Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient);
                googleApiClient.connect();
// this wont start an activity either
                activity.startActivityForResult(signInIntent, FirebaseConstants.LOG_IN_REQUEST);



            }
        }

        public void signOut(final Activity activity){
            if(googleApiClient.isConnected()) {
                Auth.GoogleSignInApi.signOut(googleApiClient).setResultCallback(
                        new ResultCallback<Status>() {
                            @Override
                            public void onResult(Status status) {
                                Intent i = new Intent(activity, LoginActivity.class);
                                activity.startActivityForResult(i, FirebaseConstants.LOG_OUT_REQUEST);
                            }
                        });
            }
        }

        public void disconnect(Activity activity){
            activity.getApplicationContext();
            if(googleApiClient.isConnected()){
                googleApiClient.disconnect();
                Intent i = new Intent(activity, LoginActivity.class);
            }
        }


    }

I cannot fix this problem without your help guys. I really need to get it working!

The dependencies in build.gradle

compile 'com.google.firebase:firebase-messaging:9.6.1'
compile 'com.google.firebase:firebase-database:9.6.1'
compile 'com.google.firebase:firebase-core:9.6.1'
compile 'com.google.firebase:firebase-auth:9.6.1'
compile 'com.google.android.gms:play-services-auth:9.6.1'
compile 'com.google.firebase:firebase-ads:9.6.1'

At the bottom of build.gradle

apply plugin: 'com.google.gms.google-services'
Cristan
  • 12,083
  • 7
  • 65
  • 69
anduplats
  • 885
  • 2
  • 14
  • 24
  • 1
    Your code differs in a number of ways from what is contained in the [Firebase QuickStart App](https://github.com/firebase/quickstart-android/blob/master/auth/app/src/main/java/com/google/firebase/quickstart/auth/GoogleSignInActivity.java). Rather than debug your approach, it might be easier to update your code to match the sample app. – Bob Snyder Dec 08 '16 at 03:49
  • 1
    did you manage to find the solution? Im facing the same problem – George2456 Jan 10 '17 at 18:08
  • unfortunately no i made my own login system :) – anduplats Jan 10 '17 at 18:56

1 Answers1

5

If your debug version does not throw any errors when logging into Firebase using Google Sign-in, it is likely a certificate signing problem.

Step 1 (optional)

Create a Toast message in all catch blocks, so you can see what the error code is. Build and sign your app as normal; then release it as a closed or open track for testing. Search online for the error code. (don't rely on CommonStatusCodes descriptions only, and search other resources).

Step 2

Check what the SHA-1 certificate fingerprint is for your release apk/bundle:

A) Go to Gradle/:app/Tasks/android/signingReport in the Android Studio and generate a report. Copy SHA1 fingerprint from the release variant.

B) If your app is signed by Google Play, then go to Release management/App signing in the Google Play console, and copy SHA-1 certificate fingerprint from App signIn certificate

Then go to Project Settings in the Firebase console and add the new fingerprint, so that you've got both debug and release fingerprints.

Step 3

Download the google-services.json file and replace the existing one in your build with the one you've just downloaded. Build and sign a new release.

Younes
  • 462
  • 7
  • 15
Pitos
  • 659
  • 6
  • 23