Android Product Flavors for App Environments
Android Product Flavors let you build distinct app variants (like dev, staging, prod) from one codebase, each with its own configuration. Use this for separate builds or white-labeling.
WHY IT EXISTS: Your app needs to connect to a different backend for development, staging, and production. Hardcoding these values or using conditional logic is brittle and error-prone. You need a systematic way to build different versions of your app with the correct configuration for each environment.
THE MENTAL MODEL: Think of Product Flavors as build-time recipes for your app. You have one shared codebase, but you can define different "flavors" (like dev, staging, prod) that swap out resources, configuration values, and even code at compile time. This creates distinct app packages for each environment without if (is_prod) checks scattered through your code.
HOW IT WORKS: While Product Flavors are a native Android feature configured in Gradle, React Native apps often manage this through libraries. A common pattern is using a library like react-native-config which reads variables from a .env file (e.g., .env.staging). The library then uses the native build system's capabilities, like Product Flavors, to inject these variables so they are accessible in your JavaScript code via an imported config object.
WHEN TO USE IT: Use this approach when you need to manage environment-specific settings. This includes API endpoints, feature flags, analytics keys, or different app names and icons for white-labeling. It's a core part of the "12-factor app" methodology, applied to mobile development.
WHEN NOT TO USE IT: Do not use this mechanism for storing highly sensitive secrets like production API secret keys or private certificates. Since these configuration values are bundled into the app package, they can be extracted by a determined user through reverse engineering. Design your app and APIs assuming these client-side values are not truly secret.
ONE CANONICAL EXAMPLE: A common setup involves creating multiple .env files in your project root: .env.development with API_URL=https://dev-api.myapp.com and .env.production with API_URL=https://api.myapp.com. When you build the "development" flavor of your Android app, a library hooks into the build process to make Config.API_URL resolve to the development URL in your React Native code. The production build automatically gets the production URL.
Read the original → github.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.