live and dev environment on android studio
In Android Studio, managing Live (Production) and Dev (Development) Environments is crucial for application development. The best practices to achieve this include:
1. Using Build Variants (Flavors)
Android Studio allows you to define different build variants using product flavors in build.gradle
.
Step 1: Define Product Flavors in build.gradle (Module: app)
Modify your android
block to include different flavors:
Step 2: Use Flavors in Code
Access the BuildConfig fields in your code:
2. Creating Separate Configuration Files
Instead of hardcoding API URLs, you can use different res/values
XML files.
Step 1: Create Folders
Inside res/values
, create:
res/values/live/strings.xml
res/values/dev/strings.xml
Step 2: Define API URLs in XML
Example for dev
environment (res/values/dev/strings.xml
):
Example for live
environment (res/values/live/strings.xml
):
Step 3: Access in Java/Kotlin
3. Setting Up Environment-based App Icons & Names
Customize the app icon and name based on the environment.
Step 1: Create Different AndroidManifest.xml
Entries
In AndroidManifest.xml
, modify android:label
dynamically:
Then define different app_name
values in strings.xml
:
Dev (
res/values/dev/strings.xml
):"MyApp Dev"
Live (
res/values/live/strings.xml
):"MyApp"
4. Switching Between Environments in Android Studio
You can switch environments using Build Variants:
Go to:
View
→Tool Windows
→Build Variants
Select the variant (
devDebug
,liveRelease
)
5. Using Environment-based Firebase Configurations
If you're using Firebase, you can have separate google-services.json
files:
app/src/dev/google-services.json
app/src/live/google-services.json
Then configure build.gradle
:
6. CI/CD and Environment Automation
If you're using GitHub Actions, GitLab CI, or Jenkins, you can automate builds for different environments:
Final Thoughts
Using product flavors, environment-based resource files, and build variants, you can easily manage different environments (Dev & Live) in Android Studio. Let me know if you need further clarifications! 🚀
Last updated