从 Android Studio 部署时登录有效,但从 Google Play 下载时无效 [重复]

Posted

技术标签:

【中文标题】从 Android Studio 部署时登录有效,但从 Google Play 下载时无效 [重复]【英文标题】:Sign-in works when deployed from Android Studio, but not when dowloaded from Google Play [duplicate] 【发布时间】:2019-06-08 03:30:34 【问题描述】:

从 Google Play 下载应用时,登录 LoginActivity 不会启动 MainActivity。不过,它可以在从 android Studio 直接部署到设备上时工作。我不确定是什么问题。

这是 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) 
                        // ...
                    
                );
    


这里是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);

【问题讨论】:

任何已知的错误信息?尝试捕捉异常,否则很难找到原因。它可以是任何东西。您是否在 Firebase 中添加了已发布的 SHA 密钥?尝试获取错误,您现在可以使用 crahlytics 或直接在 toast 中显示错误提示来获取已发布应用的错误提示。 【参考方案1】:

问题是由签名证书和 SHA-1 证书指纹引起的。将以下 SHA-1 证书添加到您的 Google API 凭据中。有2个案例

1.如果您在调试模式下运行,请添加以下生成的 SHA-1 指纹

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

2.如果你已经配置了Signing Config,那么使用下面生成的SHA-1指纹

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

我建议您在 googleApi 凭据中添加两个 SHA-1 指纹

【讨论】:

以上是关于从 Android Studio 部署时登录有效,但从 Google Play 下载时无效 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

如何在Android Studio上利用云数据库Mysql实现在线登录注册?

将应用程序从 Android Studio 部署到 iPhone 时出现 Flutter 错误

“找不到此可执行文件的有效配置文件”从 Visual Studio 2019 部署应用程序

多级用户登录 - Android Studio / Firebase

将签名的 apk 从 android studio 部署到设备

使用 ADB 时 Android Studio “找不到文件”,桌面版有效