Creating separate google-services.json files for Preview and Production environments in an Expo project and resolving eas build error

I am developing an application using Expo. The application is registered on the Google Play Console, and currently, I have two different environments, Preview and Production. I need to create separate google-services.json files and set them up correctly for each of these environments. Also I use react-native-map but not firebase stuff.

Is there a recommended way to create and manage different google-services.json files for Preview and Production environments? Also, I would appreciate guidance on how to utilize these files in my Expo project and how to set them up on the Google Play Console.

Additionally, when I run the command eas build -e preview -p android --local, I encounter the following error:

FAILURE: Build failed with an exception.
[RUN_GRADLEW] * What went wrong:
[RUN_GRADLEW] Execution failed for task ':app:processReleaseGoogleServices'.
[RUN_GRADLEW] > Malformed root json

app.config.js

import 'dotenv/config'
import dayjs from 'dayjs'
const IS_DEV = process.env.APP_VARIANT === 'development'
const IS_PREVIEW = process.env.APP_VARIANT === 'preview'

export default {
  expo: {
    name: IS_PREVIEW ? 'a Dev' : 'a',
    slug: 'a',
    scheme: 'a',
    version: '1.0.1',
    orientation: 'portrait',
    icon: './assets/splash.png',
    userInterfaceStyle: 'light',
    runtimeVersion: {
      policy: 'sdkVersion'
    },
    splash: {
      image: './assets/splash.png',
      resizeMode: 'contain',
      backgroundColor: '#ffffff'
    },
    assetBundlePatterns: [
      '**/*'
    ],
    extra: {
      eas: {
        projectId: ''
      }
    },
    owner: 'a',
    ios: {
      supportsTablet: true,
      // jsEngine: 'jsc',
      bundleIdentifier: IS_PREVIEW ? 'a.dev' : 'a',
      associatedDomains: ['applinks:a.dev'],
      buildNumber: dayjs().format('YYYYMMDDHHmm')
    },
    android: {
      package: IS_PREVIEW ? 'a.dev' : 'a',
      // jsEngine: 'jsc',
      googleServicesFile: IS_PREVIEW ? './google-services-dev.json' : './google-services.json',
      adaptiveIcon: {
        foregroundImage: './assets/splash.png',
        backgroundColor: '#ffffff'
      },
      config: {
        googleMaps: {
          apiKey: IS_PREVIEW ? 'a' : 'b'
        }
      },
      intentFilters: [
        {
          action: 'VIEW',
          autoVerify: true,
          data: [
            {
              scheme: 'https',
              host: 'a.dev',
              pathPrefix: '/a'
            }
          ],
          category: ['BROWSABLE', 'DEFAULT']
        }
      ]
    },
    web: {
      favicon: './assets/favicon.png'
    },
    plugins: [
      [
        '@stripe/stripe-react-native',
        {
          merchantIdentifier: '',
          enableGooglePay: false
        }
      ]
    ],
    updates: {
      url: 'https://a'
    },
    jsEngine: 'hermes'
  }
}

If there are any solutions or information related to this error, I would appreciate it if you could share that as well.

I also have a feeling that creating a new project on the Google Play Console might be the only way to resolve this issue, but I would like to know if there are any other methods.

Thank you for your assistance.

the approach you’re using there should work fine. it sounds like the json file that you’re providing is invalid though.

Thanks. So I should create a new google-service.json through Firebase console(new Android project) or Google Play console(new project)? I tried to make a new Android project through Firebase console. And I used it as google-service-dev.json. But it didn’t work.

did you get that same error message? can you use a json validator on your file?

Yeah. I get the same error. Of course, because I just made another Android project which is not related to my original expo app. When I don’t separate preview and production in app.config.js, the eas build was no error. So I think it’s not the matter of json validator or something. Just I want to know is how google-service.json is created. I don’t use firebase stuff in my project.

When I don’t separate preview and production in app.config.js, the eas build was no error

i suspect the file you are pointing to is missing or invalid. make sure it’s included in your build archive: https://expo.fyi/eas-build-archive - or if you are using another approach to include it, then verify the contents.

the file is copied to the native project using a very simple config plugin: https://github.com/expo/expo/blob/fa94d55014faa0f3f943963581f93d5482d11138/packages/%40expo/config-plugins/src/android/GoogleServices.ts#L46-L85