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:
gradleCopyEditandroid {
...
buildTypes {
debug {
applicationIdSuffix ".dev"
resValue "string", "app_name", "MyApp Dev"
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
flavorDimensions "default"
productFlavors {
dev {
dimension "default"
applicationIdSuffix ".dev"
versionNameSuffix "-dev"
buildConfigField "String", "BASE_URL", "\"https://dev.api.example.com/\""
}
live {
dimension "default"
buildConfigField "String", "BASE_URL", "\"https://api.example.com/\""
}
}
}
Step 2: Use Flavors in Code
Access the BuildConfig fields in your code:
javaCopyEditString apiUrl = BuildConfig.BASE_URL;
Log.d("API URL", apiUrl);
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
):
xmlCopyEdit<resources>
<string name="api_url">https://dev.api.example.com/</string>
</resources>
Example for live
environment (res/values/live/strings.xml
):
xmlCopyEdit<resources>
<string name="api_url">https://api.example.com/</string>
</resources>
Step 3: Access in Java/Kotlin
javaCopyEditString apiUrl = getString(R.string.api_url);
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:
xmlCopyEdit<application
android:label="@string/app_name">
</application>
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
:
gradleCopyEditandroid.applicationVariants.all { variant ->
def fileName = variant.name.contains("dev") ? "google-services-dev.json" : "google-services-live.json"
def filePath = "$projectDir/app/src/${variant.flavorName}/$fileName"
variant.mergeResources.doLast {
copy {
from filePath
into "$buildDir/intermediates/merged_res/${variant.dirName}/google-services.json"
}
}
}
6. CI/CD and Environment Automation
If you're using GitHub Actions, GitLab CI, or Jenkins, you can automate builds for different environments:
yamlCopyEdit- name: Build Dev APK
run: ./gradlew assembleDevDebug
- name: Build Live APK
run: ./gradlew assembleLiveRelease
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