🔧 Programmierung 🕛 vor 2 Monaten 9 Min Lesezeit
0

Stop Uninstalling Your App to Test Staging and Production: A Proper Multi-Environment Setup for Mobile Development

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht




reactnative #expo #mobile #frontend



If you've worked with a web app before, you know how convenient it is to run staging/development and production side by side. Mobile apps are a different story — by default you have to uninstall the production app before you can test a staging or preview build. Anyone who's shipped a React Native app knows this pain, especially during active development: you test against a dev API, QA needs a staging build, and production must point to a live server — all from the same codebase. Hardcoding URLs and switching them manually before each build is a recipe for disaster.



The good news is you can have the same workflow you're used to on the web. You can set up separate staging/development and production environments, and even install both the preview and production builds on the same device at the same time.



In this article, I'll walk you through how to set up separate environments — development, preview (staging), and production — in an Expo React Native project, so each environment gets its own:




  • App name and icon

  • Bundle ID/package name

  • API URL, Socket URL, and third-party keys

  • Firebase config file (google-services.json / GoogleService-Info.plist)

  • EAS build profile and OTA update channel



By the end, you'll be able to run one command and get a build that's correctly wired for the environment you're targeting — no manual switching, and you (or your QA team) can install staging and production side by side on the same device.



NB: If you haven't read my previous articles, you can check them out )

  • Basic familiarity with app.json, app.config.js, and eas.json











  • Step 6: Validate Environment Variables at Runtime with Zod



    Reading process.env directly across the app is fragile — a missing variable silently becomes undefined. I use Zod to validate all environment variables at app startup, so the app fails loudly if something is missing.



    Here's env.ts:




    CODE
    import * as z from 'zod';

    const createEnv = () => {
    const EnvSchema = z.object({
    API_URL: z.string(),
    SOCKETIO_URL: z.string(),
    APP_VARIANT: z.string(),
    TAWK_KEY: z.string(),
    });

    const envVars = {
    API_URL: process.env.EXPO_PUBLIC_API_URL,
    SOCKETIO_URL: process.env.EXPO_PUBLIC_SOCKETIO_URL,
    APP_VARIANT: process.env.EXPO_PUBLIC_APP_VARIANT,
    TAWK_KEY: process.env.EXPO_PUBLIC_TAWK_KEY,
    };

    const parsedEnv = EnvSchema.safeParse(envVars);

    if (!parsedEnv.success) {
    throw new Error(
    `Invalid env provided.
    The following variables are missing or invalid:
    ${Object.entries(parsedEnv.error.flatten().fieldErrors)
    .map(([k, v]) => `- ${k}: ${v}`)
    .join('\n')}
    `
    ,
    );
    }

    return parsedEnv.data ?? {};
    };

    export const env = createEnv();







    Note: if you add extra keys to EnvSchema (like an Agora app ID), remember to add a matching EXPO_PUBLIC_* entry to envVars and to your .env.local / EAS environment — otherwise Zod will throw at startup.




    Now anywhere in the app, I import a single typed env object:




    CODE
    import axios from 'axios';
    import { env } from '@/configs/env';

    const api = axios.create({
    baseURL: env.API_URL,
    });






    No more process.env.EXPO_PUBLIC_API_URL scattered across files — and a missing variable crashes in dev, not in production.









    Step 7: Add Convenience Scripts to package.json



    To avoid typing long EAS commands, I added scripts for each environment:




    CODE
    {
    "scripts": {
    "dev": "npx expo start",
    "build:dev:android": "eas env:pull development --non-interactive && eas build --profile development --platform android --local",
    "build:preview:android": "eas env:pull preview --non-interactive && eas build --profile preview --platform android --local",
    "build:prod:android": "eas env:pull production --non-interactive && eas build --profile production --platform android --local",
    "update:preview:android": "eas env:pull preview --non-interactive && eas update --channel preview --platform android --environment preview",
    "update:prod:android": "eas env:pull production --non-interactive && eas update --channel production --platform android --environment production"
    }
    }






    Each script:




    1. Pulls the latest env vars from EAS for that environment.

    2. Runs the build (or OTA update) against the matching profile and channel.



    So to build a staging APK, I just run:




    CODE
    yarn build:preview:android






    And to build a dev client APK:




    CODE
    yarn build:dev:android






    The result: development and preview builds installed side by side on the same phone.



    Screenshot of a phone home screen showing two apps after build setup









    Step 8: Firebase Config per Environment



    Push notifications and analytics need separate Firebase projects per environment. In my project root, I keep four files:




    CODE
    google-services.json          # production Android
    google-services-dev.json # dev + preview Android
    GoogleService-Info.plist # production iOS
    GoogleService-Info-dev.plist # dev + preview iOS






    app.config.ts picks the right one based on APP_VARIANT (see Step 3). Only the production files are referenced as defaults in app.json.









    Step 9: Putting It All Together



    Here's the full flow when I want a staging build:





    1. APP_VARIANT=preview is set by the preview EAS profile.


    2. app.config.ts reads it → the app becomes Bayt (Preview) with bundle ID com.jocanola.bayttaalim.preview.


    3. eas env:pull preview injects the EXPO_PUBLIC_* variables into the build.


    4. env.ts validates them with Zod.

    5. The app boots with the staging API URL, socket URL, and Firebase config.

    6. The build is published to the preview OTA channel.



    One command, one environment, zero manual switching.









    Final Thoughts



    Setting up multiple environments in Expo takes a bit of upfront work, but it pays off every single day. Once it's in place:




    • You can install dev, staging, and prod builds on the same phone.

    • You'll never accidentally ship a build pointing at the wrong API.

    • QA gets reproducible staging builds.

    • OTA updates stay scoped to the right audience.

    • New team members can build for any environment with a single command.



    The combination of eas.json profiles, a dynamic app.config.ts, EAS environment variables, and Zod validation gives you a setup that's both flexible and safe.



    Thanks for reading till the end. Next time, I'll cover how to automate these builds with GitHub Actions so every push to staging or main produces the right build automatically. Stay tuned!

    Vollständiger Original-Artikel
    Den kompletten Beitrag mit allen Details direkt auf dev.to lesen.
    ↗ Original-Artikel auf dev.to lesen
    Wie bewertest du diesen Beitrag?
    1 Klick Feedback
    Teilen mit Netzwerk & Team:

    Community-Analysen & Experten-Meinungen 0

    Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
    Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
    Community Pulse: Relevanz-Einschätzung
    1 Klick Experten-Votum
    🔴 Akute Relevanz 0%
    🟡 In Evaluierung 0%
    🟢 Keine Auswirkung 0%
    Spannende Innovation 0%
    Verwandte Story-Cluster & Quellen (Vektor-KI)
    Port 8095 Engine
    6 Quellen
    CVE-2022-44255 | TOTOLINK LR350 9.3.5u.6369_B20220309 buffer overflow (EUVD-2022-47204)
    2 Quellen
    CVE-2026-68426 | Linux Kernel up to 6.18.41/7.1.5/7.2-rc3 xfrm validate_xmit_skb_list use after free (Nessus ID 346426)
    1 Quelle
    Windows 11 Probleme mit gültiger Domänenanmeldung nach September-Update [Workaround]
    Ähnliche Beiträge
    🔍 Verwandte News

    Auch interessante Nachrichten Stop Uninstalling Your App to Test Staging and Production: A Proper Multi-Environment Setup for Mobile Development

    Thematisch verwandte Begriffe: Stop, Uninstalling, Your, Test · 6 Treffer

    Laden...

    Beiträge werden geladen ...

    Laden...

    Videos werden geladen ...

    Laden...

    Beiträge werden geladen ...

    Laden...

    Videos werden geladen ...

    Laden...

    Beiträge werden geladen ...

    Laden...

    Videos werden geladen ...

    Laden...

    Beiträge werden geladen ...

    Laden...

    Videos werden geladen ...