Course contentsShow
← React Native
Lesson 78 of 1,5551. The React Native ArchitectureFree lesson

Platform-Specific Build Configuration

You will be able to define build variants for different environments using native configuration files.

In this lesson, you will learn how to create distinct "build variants" so your application can behave differently depending on the environment—like having separate configurations for staging and production.

Understanding Build Variants

When you develop a React Native app, you often need to point to different backend APIs or change app icons based on the environment. Instead of manually editing your code every time you switch, we use native build configurations.

By defining build variants, the native side of your app (the android and ios folders) treats these as unique apps. This allows you to have a "Staging" app and a "Production" app installed on the same device simultaneously, each with its own internal settings.

Implementing Variants

In the native layer, you define these variants using built-in build tools:

  • For Android: You use buildTypes inside your android/app/build.gradle file. You can define a debug, release, and a custom staging variant.
  • For iOS: You use Build Configurations and Schemes inside Xcode. By duplicating the base configuration, you can assign different Info.plist values or different API keys for each build.

For example, in your build.gradle, you might define a staging environment like this:

buildTypes {
    debug { ... }
    release { ... }
    staging {
        initWith release
        resValue "string", "app_name", "My App (Staging)"
        buildConfigField "String", "API_URL", "\"https://staging.api.example.com\""
    }
}

Once defined, you can run commands like npx react-native run-android --variant=staging. The native build system automatically packages the correct settings, and the JavaScript layer can access these native constants using react-native-config or similar bridge utilities.

By separating these at the build level, you ensure that your production code is never accidentally exposed to staging credentials, and your testing process becomes consistent across every member of your team. It shifts the burden of configuration from your manual memory to the robust native build pipeline.

Key Takeaway: Build variants allow you to generate unique app instances for different environments by leveraging native build tools, ensuring your code remains clean and your configurations remain environment-specific.