Expo SDK48多环境配置react-native-firebase的googleServicesFile问题
解决Expo SDK48多环境Google Services配置问题
1. 修正eas.json的环境变量配置
确保每个构建profile的env字段正确设置APP_VARIANT,同时按需配置平台构建模式:
{ "build": { "dev": { "env": { "APP_VARIANT": "dev" }, "android": { "buildType": "development" }, "ios": { "buildConfiguration": "Debug" } }, "test": { "env": { "APP_VARIANT": "test" }, "android": { "buildType": "release" }, "ios": { "buildConfiguration": "Release" } }, "uat": { "env": { "APP_VARIANT": "uat" }, "android": { "buildType": "release" }, "ios": { "buildConfiguration": "Release" } }, "prod": { "env": { "APP_VARIANT": "prod" }, "android": { "buildType": "release" }, "ios": { "buildConfiguration": "Release" } } } }
2. 编写动态app.config.js配置
从环境变量读取APP_VARIANT,根据变量加载对应平台的Google Services文件(示例中文件存放在项目根目录config文件夹下):
module.exports = () => { // 优先取EAS注入的变量,本地默认使用dev环境 const appVariant = process.env.APP_VARIANT || 'dev'; return { expo: { name: `MyApp-${appVariant}`, slug: `myapp-${appVariant}`, // 其他基础配置项... android: { package: `com.myapp.${appVariant}`, googleServicesFile: `./config/google-services-${appVariant}.json` }, ios: { bundleIdentifier: `com.myapp.${appVariant}`, googleServicesFile: `./config/GoogleService-Info-${appVariant}.plist` } } }; };
3. 规避环境变量冲突与缓存问题
- 不要依赖本地
.env文件:如果本地存在.env,确保其中没有硬编码APP_VARIANT,否则会覆盖eas.json中的配置。 - 切换构建环境时清除缓存:每次更换profile构建,添加
--clear-cache参数避免旧配置残留:
eas build --profile dev --clear-cache eas build --profile prod --clear-cache
4. 验证配置有效性
- 在app.config.js中添加日志,确认变量读取正确:
module.exports = () => { const appVariant = process.env.APP_VARIANT || 'dev'; console.log(`Current APP_VARIANT: ${appVariant}`); // 构建时控制台会打印该信息 // ...其余配置内容 };
- 运行本地预构建测试:
eas build:configure
查看控制台输出的APP_VARIANT是否与指定profile一致,同时检查生成的原生项目是否加载了对应环境的Google Services文件。
5. 关键注意事项
- 确保各环境的Google Services文件(如
google-services-dev.json、GoogleService-Info-test.plist)存在于指定路径,文件名与代码拼接逻辑完全匹配。 - Android的
buildType和iOS的buildConfiguration仅控制构建模式(调试/发布),与Google Services文件的选择无关,不要混淆两者。 - 构建时必须明确指定profile,比如
eas build --profile uat,否则会使用默认profile(通常为production),导致变量不匹配。
内容的提问来源于stack exchange,提问作者DawidZacher
相关产品推荐
相关产品推荐

