由于缺少 Play 商店,android 个模拟器上的 Firebase 身份验证失败
Firebase Authentication failing on android emulators due to missing Play Store
我一直在尝试让 Firebase 身份验证正常工作,但是当我尝试登录时 Logcat 中一直出现以下错误。
这些似乎表明需要安装 Play Store 才能进行身份验证?有没有办法在模拟器上做到这一点?
我正在使用“API 30 x86 with Google APIs”系统映像进行测试,但每次都会收到此错误。
使用物理设备进行测试似乎按预期工作。但到目前为止,我一直无法找到模拟器的解决方法。
Logcat
2022-03-08 09:38:37.265 3784-3784/<package-name> W/GooglePlayServicesUtil: <package-name> requires the Google Play Store, but it is missing.
2022-03-08 09:38:37.270 3784-3810/<package-name> W/GooglePlayServicesUtil: <package-name> requires the Google Play Store, but it is missing.
2022-03-08 09:38:37.270 3784-3810/<package-name> W/GoogleApiManager: The service for com.google.android.gms.internal.auth-api.zbo is not available: ConnectionResult{statusCode=SERVICE_INVALID, resolution=null, message=null}
2022-03-08 09:38:37.363 3784-3784/<package-name> E/LoginActivity: sign-in failed
com.firebase.ui.auth.FirebaseUiException: Exception serialization error, forced wrapping. Original: com.firebase.ui.auth.FirebaseUiException: Play Services update cancelled, original cause: com.google.android.gms.common.api.ApiException: 9: Error connecting to Google Play services
at com.firebase.ui.auth.KickoffActivity.onFailure(KickoffActivity.java:77)
at com.google.android.gms.tasks.zzk.run(com.google.android.gms:play-services-tasks@@18.0.1:1)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
登录活动
public class LoginActivity extends AppCompatActivity {
private static final String LOG_TAG = LoginActivity.class.getSimpleName();
private Button loginButton;
private ActivityResultLauncher<Intent> signInLauncher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// Register a callback for when the sign in process is complete
signInLauncher =
registerForActivityResult(
new FirebaseAuthUIActivityResultContract(),
(result) -> onSignInResult(result)
);
// Register click listener
loginButton = findViewById(R.id.loginButton);
loginButton.setOnClickListener((view) -> {
// Choose authentication providers
List<AuthUI.IdpConfig> providers = new ArrayList<>();
providers.add(new AuthUI.IdpConfig.EmailBuilder().build());
providers.add(new AuthUI.IdpConfig.GoogleBuilder().build());
// Create sign-in intent
Intent signInIntent =
AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(providers)
.build();
// Launch sign-in activity
signInLauncher.launch(signInIntent);
});
}
private void onSignInResult(FirebaseAuthUIAuthenticationResult result) {
if (result.getResultCode() == RESULT_OK) {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
Log.i(LOG_TAG, "sign-in successful: " + user.getUid());
} else {
IdpResponse response = result.getIdpResponse();
if (response != null) {
FirebaseUiException error = response.getError();
Log.e(LOG_TAG, "sign-in failed", error);
} else {
Log.e(LOG_TAG, "sign-in failed: no response");
}
}
}
}
文档
出乎我的意料,似乎 Play 商店图片仅适用于某些设备。
我通常使用“Nexus S”phone 和“Nexus 7”平板电脑进行测试,因为它们的分辨率较低且resource-intensive。但是 Google 似乎不支持在这些图像中的任何一个上安装 Play 商店。
使用 Play 商店创建模拟器 pre-installed:
- 在 Android Studio 中打开 Android 虚拟设备 (AVD) 管理器。
- 点击“创建虚拟设备...”
- Select 支持 Play 商店的少数 Pixel 手机 之一。
- 现在您可以通过 (Play Store) 下载和使用图像进行测试。
- 狩猎愉快!
我一直在尝试让 Firebase 身份验证正常工作,但是当我尝试登录时 Logcat 中一直出现以下错误。
这些似乎表明需要安装 Play Store 才能进行身份验证?有没有办法在模拟器上做到这一点?
我正在使用“API 30 x86 with Google APIs”系统映像进行测试,但每次都会收到此错误。
使用物理设备进行测试似乎按预期工作。但到目前为止,我一直无法找到模拟器的解决方法。
Logcat
2022-03-08 09:38:37.265 3784-3784/<package-name> W/GooglePlayServicesUtil: <package-name> requires the Google Play Store, but it is missing.
2022-03-08 09:38:37.270 3784-3810/<package-name> W/GooglePlayServicesUtil: <package-name> requires the Google Play Store, but it is missing.
2022-03-08 09:38:37.270 3784-3810/<package-name> W/GoogleApiManager: The service for com.google.android.gms.internal.auth-api.zbo is not available: ConnectionResult{statusCode=SERVICE_INVALID, resolution=null, message=null}
2022-03-08 09:38:37.363 3784-3784/<package-name> E/LoginActivity: sign-in failed
com.firebase.ui.auth.FirebaseUiException: Exception serialization error, forced wrapping. Original: com.firebase.ui.auth.FirebaseUiException: Play Services update cancelled, original cause: com.google.android.gms.common.api.ApiException: 9: Error connecting to Google Play services
at com.firebase.ui.auth.KickoffActivity.onFailure(KickoffActivity.java:77)
at com.google.android.gms.tasks.zzk.run(com.google.android.gms:play-services-tasks@@18.0.1:1)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
登录活动
public class LoginActivity extends AppCompatActivity {
private static final String LOG_TAG = LoginActivity.class.getSimpleName();
private Button loginButton;
private ActivityResultLauncher<Intent> signInLauncher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// Register a callback for when the sign in process is complete
signInLauncher =
registerForActivityResult(
new FirebaseAuthUIActivityResultContract(),
(result) -> onSignInResult(result)
);
// Register click listener
loginButton = findViewById(R.id.loginButton);
loginButton.setOnClickListener((view) -> {
// Choose authentication providers
List<AuthUI.IdpConfig> providers = new ArrayList<>();
providers.add(new AuthUI.IdpConfig.EmailBuilder().build());
providers.add(new AuthUI.IdpConfig.GoogleBuilder().build());
// Create sign-in intent
Intent signInIntent =
AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(providers)
.build();
// Launch sign-in activity
signInLauncher.launch(signInIntent);
});
}
private void onSignInResult(FirebaseAuthUIAuthenticationResult result) {
if (result.getResultCode() == RESULT_OK) {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
Log.i(LOG_TAG, "sign-in successful: " + user.getUid());
} else {
IdpResponse response = result.getIdpResponse();
if (response != null) {
FirebaseUiException error = response.getError();
Log.e(LOG_TAG, "sign-in failed", error);
} else {
Log.e(LOG_TAG, "sign-in failed: no response");
}
}
}
}
文档
出乎我的意料,似乎 Play 商店图片仅适用于某些设备。
我通常使用“Nexus S”phone 和“Nexus 7”平板电脑进行测试,因为它们的分辨率较低且resource-intensive。但是 Google 似乎不支持在这些图像中的任何一个上安装 Play 商店。
使用 Play 商店创建模拟器 pre-installed:
- 在 Android Studio 中打开 Android 虚拟设备 (AVD) 管理器。
- 点击“创建虚拟设备...”
- Select 支持 Play 商店的少数 Pixel 手机 之一。
- 现在您可以通过 (Play Store) 下载和使用图像进行测试。
- 狩猎愉快!