0

When the app is downloaded from Google Play, signing in on the LoginActivity does not launch the MainActivity. It works when deployed directly onto the device from Android Studio, though. I'm not sure what the problem is.

Here is the code for the LoginActivity:

public class LoginActivity extends AppCompatActivity {

    @BindView(R.id.sign_in_button) SignInButton signInButton;

    public GoogleSignInClient mGoogleSignInClient;
    private int RC_SIGN_IN;
    private static final String TAG = "Login Activity Error";
    GoogleSignInOptions gso;
    SharedPreferences sharedPreferences;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        sharedPreferences = getSharedPreferences(MainActivity.MY_PREFERENCES, Context.MODE_PRIVATE);

        ButterKnife.bind(this);
        signInButton.setSize(SignInButton.SIZE_STANDARD);

        gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .requestScopes(new Scope("https://www.googleapis.com/auth/calendar.events"))
                .build();

        mGoogleSignInClient = GoogleSignIn.getClient(getApplicationContext(), gso);

        findViewById(R.id.sign_in_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent signInIntent = mGoogleSignInClient.getSignInIntent();
                startActivityForResult(signInIntent, RC_SIGN_IN);

            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();

        if (!sharedPreferences.getBoolean(MainActivity.SIGNED_IN, false)) {
            signOut();
        }
        GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
        updateUI(account);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RC_SIGN_IN) {
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            handleSignInResult(task);
        }
    }

    private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
        try {
            GoogleSignInAccount account = completedTask.getResult(ApiException.class);

            updateUI(account);
        } catch (ApiException e) {
            updateUI(null);
        }
    }

    private void updateUI(GoogleSignInAccount account) {
        if (account != null) {
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putBoolean(MainActivity.SIGNED_IN, true);
            editor.apply();

            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            intent.putExtra(MainActivity.PERSON_NAME, account.getDisplayName());
            intent.putExtra(MainActivity.PERSON_EMAIL, account.getEmail());
            intent.putExtra(MainActivity.PERSON_PHOTO, account.getPhotoUrl());
            startActivity(intent);
        }
    }

    private void signOut() {
        mGoogleSignInClient.signOut()
                .addOnCompleteListener(this, new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        // ...
                    }
                });
    }

}

Here is the relevant code for the MainActivity:

protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);

    setSupportActionBar(bottomAppBar);

    ActionBar actionbar = getSupportActionBar();
    actionbar.setDisplayHomeAsUpEnabled(true);
    actionbar.setHomeAsUpIndicator(R.drawable.ic_menu);

    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = sharedPreferences.edit();

    Intent intent = getIntent();
    if (intent.getStringExtra(PERSON_NAME) != null) {
        editor.putString("name", intent.getStringExtra(PERSON_NAME));
        editor.apply();
    }
    if (intent.getParcelableExtra(PERSON_PHOTO) != null) {
        editor.putString("photo", intent.getParcelableExtra(PERSON_PHOTO).toString());
        editor.apply();
    }

    personName = sharedPreferences.getString("name", "");
    personPhoto = Uri.parse(sharedPreferences.getString("photo", ""));

    View headerView = navigationView.getHeaderView(0);
    TextView navName = headerView.findViewById(R.id.nav_name);
    CircleImageView navPicture = headerView.findViewById(R.id.nav_picture);
    navigationView.setNavigationItemSelectedListener(
            new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                    menuItem.setChecked(true);
                    drawerLayout.closeDrawers();

                    switch (menuItem.getItemId()) {
                        case R.id.sign_out:
                            signOut();
                            break;
                    }

                    return true;
                }
            }
    );

    if (personName != null) {
        navName.setText(personName);
    }

    if (personPhoto != null) {
        Glide.with(this)
                .load(personPhoto)
                .into(navPicture);
    }
}
private void signOut() {
    SharedPreferences sharedPreferences = getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean(SIGNED_IN, false);
    editor.commit();

    Intent intent = new Intent(MainActivity.this, LoginActivity.class);
    startActivity(intent);
}
Roshan
  • 55
  • 1
  • 6
  • Any known error messages? Try to catch the exception otherwise it will be hard to find the cause. It could be anything. Have you added the released SHA key in the firebase? Try to get the error and you can do it by using crahlytics or showing the error hint in a toast directly for published app for now. – Niraj Niroula Jan 14 '19 at 06:28

1 Answers1

0

The problem is due to the Signing Certificate and the SHA-1 certificate fingerprint. Add the following SHA-1 certificates into your Google API credentials. there are 2 cases

1.If you are running in debug mode add the SHA-1 fingerprint generated by the following

"C:\Program Files\Java\jre1.8.0_101\bin\keytool" -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android

2.If you have configured Signing Config then use SHA-1 fingerprint generated by following

"C:\Program Files\Java\jre1.8.0_101\bin\keytool" -list -v -keystore "[youKeyPath]\youKey.jks"

i recommend you to add both the SHA-1 fingerprints in your googleApi credentials

sudesh regmi
  • 536
  • 4
  • 12