react-nativeexpoenvironment-variablesandroid-buildeas

How to make EXPO_PUBLIC_API_URL (currently in .env file) configurable post-build in Expo v52 preview APK for local testing?


I have built an Android application using Expo v52 with React Native and now I want to generate a preview APK. In my project, I use an environment variable like: EXPO_PUBLIC_API_URL=http://192.168.57.53:3000/api . I want to include this in the APK such that I can update it later without rebuilding the entire app. Is there a way to change it post-build in a preview APK for local testing? like there is option in react website deployment, we set key.

Currently, I have defined EXPO_PUBLIC_API_URL in the .env file, but I read that it can also be specified in eas.json. Is there a way to use EAS secrets to make this variable dynamic?

I haven't built any APK for React Native Expo yet, so please help. I couldn’t find any relevant documentation on this.


Solution

  • If you want to use environment variables inside your eas.json file, you will need to add env objects inside development, preview & production. This is my eas.json for example.

    {
      ...
      "build": {
        "development": {
          "developmentClient": true,
          "distribution": "internal",
          "ios": {
            "simulator": true
          },
          "env": {
            "API_URL": "https://www.google.com/"
          },
          ...
        },
        "preview": {
          "distribution": "internal",
          "ios": {
            "simulator": true
          },
          "android": {
            "buildType": "apk"
          },
          "env": {
            "API_URL": "https://www.facebook.com/"
          },
          ...
        },
        "production": {
          "env": {
            "API_URL": "https://www.stackoverflow.com/"
          }
          ...
        }
      },
      "submit": {
        "production": {}
      }
    }