You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

集成Firestore的Android应用崩溃:GooglePlayServices error 9问题

解决Firestore应用崩溃:GooglePlayServices not available due to error 9

问题原因

错误码9对应SERVICE_MISSING,说明你的设备或模拟器未安装Google Play服务,或是版本不兼容——Firebase Firestore依赖Google Play Services才能正常运行。

修复步骤

  • 实体设备处理:

    1. 打开Google Play商店,搜索「Google Play服务」,直接安装或更新至最新版本。
    2. 若为国内无Google服务框架的机型,需先适配安装对应设备型号和系统版本的完整Google服务框架与Google Play服务。
  • 模拟器处理:

    1. 创建模拟器时,选择带Google Play的系统镜像(如「Google APIs」或「Google Play」开头的镜像,避免纯Android镜像)。
    2. 若现有模拟器无Google Play,删除后重新创建带Google Play的版本,并确保模拟器内Google Play服务为最新状态。

额外检查项

  1. 确认项目依赖配置正确,确保Firebase与Google Play Services版本兼容:
// 项目级build.gradle
dependencies {
    classpath 'com.google.gms:google-services:4.4.1'
}

// 模块级build.gradle
dependencies {
    implementation 'com.google.firebase:firebase-firestore:24.10.3'
    implementation 'com.google.android.gms:play-services-base:18.2.0'
}
  1. 无需额外添加Manifest配置,Firebase插件会自动处理Google Play服务相关声明。

代码优化建议

给Firestore监听增加更精准的错误日志,方便调试:

documentReference.addSnapshotListener((documentSnapshot, e) -> {
    if (e != null) {
        Log.e("FirestoreError", "数据获取失败", e);
        Toast.makeText(MainActivity.this, "数据库连接失败:" + e.getMessage(), Toast.LENGTH_SHORT).show();
        return;
    }
    if (documentSnapshot != null && documentSnapshot.exists()) {
        String value = documentSnapshot.getString("updatedValue");
        ret.setText(value != null ? value : "无数据");
    } else {
        ret.setText("目标文档不存在");
    }
});

内容的提问来源于stack exchange,提问作者Jasil Ek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 04:54:24