首页 > 解决方案 > GoogleSignInClient 在物理设备上始终返回 null

问题描述

我发现了很多类似的问题,但我已经实现了一切,但它不起作用。在装有 Android 9、10 和 11 的虚拟设备上,它按预期工作,但在使用 Android 9 和 11 的物理设备上却没有(可能在所有其他物理设备上也是如此)。

主要活动

public class MainActivity extends AppCompatActivity {
    private FirebaseAuth mAuth;
    private GoogleSignInClient mGoogleSignInClient;

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

        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();

        mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
        mAuth = FirebaseAuth.getInstance();
    }

    ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            result -> {
                if (result.getResultCode() == Activity.RESULT_OK) {
                    Intent data = result.getData();
                    /*** process firebase auth with data...*/
                } else {
                    Toast.makeText(MainActivity.this, "Weird error....", Toast.LENGTH_SHORT).show();
                }
            });

    @Override
    protected void onStart() {
        super.onStart();
        if (mAuth.getCurrentUser() != null) {
            //User logged to google
            /*** processing firebase auth etc... ***/
        } else {
            //User not logged to google
            Intent intent = mGoogleSignInClient.getSignInIntent();
            someActivityResultLauncher.launch(intent);
        }

    }
....
}

这是buid.gradle

plugins {
    id 'com.android.application'
    id 'com.google.gms.google-services'
}
android {
    compileSdk 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.xxx.xxxx"
        minSdk 27
        targetSdk 30
        versionCode 2014
        versionName "0.2.14"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    signingConfigs {
        release {
            storeFile file("${System.getenv("AC_ANDROID_KEYSTORE_PATH")}")
            storePassword "${System.getenv("AC_ANDROID_KEYSTORE_PASSWORD")}"
            keyAlias "${System.getenv("AC_ANDROID_ALIAS")}"
            keyPassword "${System.getenv("AC_ANDROID_ALIAS_PASSWORD")}"
            v2SigningEnabled true
            v1SigningEnabled true
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    implementation 'androidx.preference:preference:1.1.1'

    implementation 'com.google.code.gson:gson:2.8.7'
    implementation 'com.squareup.okhttp3:okhttp:5.0.0-alpha.2'

    implementation platform('com.google.firebase:firebase-bom:28.2.1')
    implementation 'com.google.firebase:firebase-analytics:19.0.0'
    implementation 'com.google.firebase:firebase-auth:21.0.1'
    implementation 'com.google.android.gms:play-services-auth:19.2.0'

}

选择 Google 帐户后,在虚拟设备上我已登录并成功连接到 firebase,但在物理设备上我收到 toast 消息,因为result.getResultCode()没有Activity.RESULT_OK,也没有其他结果数据。

google-services.json 已配置,以及与 firebase 有关的所有其他内容。知道如何解决问题吗?

更新:

从 android studio、物理设备或虚拟设备上运行时,应用程序正常工作。

当应用程序安装在物理或虚拟设备上时,作为发布或调试版本,无法正常工作。真的很奇怪。

更新 2:

我已经在虚拟设备上安装了发布应用程序。当然没有按预期工作。

然后我在相同的虚拟设备上从 android studio 运行应用程序,我收到警告说有一个具有不同签名的相同应用程序 (???) 我运行它。有用。

我退出了应用程序,并从虚拟设备运行它,现在它可以工作了。

奇怪²。

我从android停止应用程序

标签: androidfirebase-authenticationgoogle-signin

解决方案


解决方案是将您的 sha1 添加到您的 firebase 项目中,如下所示:

设置图标 -> 项目设置 -> 常规设置 -> 向下滚动并粘贴您的 sha1 密钥


推荐阅读