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

EAS构建Android应用后无法连接Firestore数据库求助

EAS构建后Firestore无法连接的修复方案

针对切换到EAS构建后Firestore无法连接、但Firebase Auth功能正常的问题,可按以下步骤排查修复:

1. 修正Firestore初始化代码

当前初始化代码存在冗余冲突,需确保仅通过initializeFirestore创建并导出实例,避免混用getFirestore:

import { initializeApp } from 'firebase/app';
import { initializeFirestore } from 'firebase/firestore';

const firebaseConfig = { /* 你的Firebase配置信息 */ };
const app = initializeApp(firebaseConfig);

// 正确初始化并导出配置后的Firestore实例
export const db = initializeFirestore(app, {
  experimentalForceLongPolling: true,
  useFetchStreams: false,
});

注意:不要在初始化后调用getFirestore(app),这会生成未应用自定义配置的新实例,导致连接异常。

2. 添加Android网络权限

EAS构建的APK默认可能缺失必要网络权限,需在app.json的android字段中补充:

"android": {
  "adaptiveIcon": {
    "foregroundImage": "./assets/images/app_logo_square.png",
    "backgroundColor": "#FFFFFF"
  },
  "package": "com.packgage.appname",
  "permissions": ["INTERNET", "ACCESS_NETWORK_STATE"]
}

3. 验证Firebase配置一致性

  • 确认EAS构建时注入的Firebase配置与开发环境完全一致,若使用.env文件需确保构建过程能正确读取环境变量
  • 登录Firebase控制台,检查应用包名com.packgage.appname已正确注册,并添加EAS构建生成的SHA-1证书指纹(可通过eas credentials命令查看)

4. 检查Firestore安全规则

确保Firestore安全规则允许当前认证用户访问数据,避免因规则限制导致连接失败:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth != null; // 根据业务需求调整规则
    }
  }
}

5. 清理缓存后重新构建

缓存可能导致配置未生效,执行以下命令清理缓存并重新构建:

eas build --clear-cache --platform android

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 20:24:23