When we create apk file or run app, we have to check if the app url is set to production or development server.
That’s why sometimes we forget to update it and provide wrong app to user.
I want a way to set such constants as per the debug / release type of gradle.
Any information is welcomed
Thanks!!
The following code sample creates a flavor dimension named. Please check this document
flavorDimensions += "AppName"
productFlavors {
create("development") {
dimension = "AppName"
buildConfigField("String", "BASE_URL", "Your url")
buildConfigField("String", "BASE_API_URL", "Your url")
buildConfigField("String", "BASE_SOCKET_URL", "Your url")
}
create("production") {
dimension = "AppName"
buildConfigField("String", "BASE_URL", "Your url")
buildConfigField("String", "BASE_API_URL", "Your url")
buildConfigField("String", "BASE_SOCKET_URL", "Your url")
}
}
1
To create separate app variants for development and production in an Android project, you can leverage the “Build Variants” feature in Gradle. tried out the below code to archive different URLs based on your selected variants.
android {
// Define product flavors
flavorDimensions "default"
productFlavors {
development {
dimension "default"
buildConfigField "String", "BASE_URL", ""https://dev.api.example.com/""
}
production {
dimension "default"
buildConfigField "String", "BASE_URL", ""https://api.example.com/""
}
}
// Optionally specify build types if different configurations are needed
buildTypes {
debug {
...
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
For more details check the the link.
https://developer.android.com/build/build-variants