android - 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停止应用程序
解决方案
解决方案是将您的 sha1 添加到您的 firebase 项目中,如下所示:
设置图标 -> 项目设置 -> 常规设置 -> 向下滚动并粘贴您的 sha1 密钥
推荐阅读
- hyperledger-fabric - Hyper Ledge Fabric:背书政策未反映
- .net-core - Confluent Kafka 消费者仅在更改 groupId 后才消费消息
- python - 如何将我自己的模块(函数包)从 GitHub 拉入 Jupyter 笔记本或 .py 文件?
- rust - Rust Diesel:特征绑定 `NaiveDateTime: Deserialize<'_>` 不满足
- javascript - 如何解决推入单个数组的javascript对象而不是将它们全部推入单个数组?
- ios - 来自 AVAudioPlayer 的音频不发出声音
- thingsboard - 如何在 thingsboard 小部件中使用 RxJs
- python - 从给定的输入中打印最大的数字
- javascript - regenerator-runtime npm 包用于什么?
- git - GIT 错误:HTTP 999 curl 22 请求的 URL 返回错误:999 致命:远程端意外挂断 999