I'm working on a white brand app.
We create a different flavor per client and each client has Debug
and Production
APIs, so I'm trying to set them up in the Gradle.
How should I do that?
Here is what I've tried:
buildTypes {
debug {
// some configurations
}
release {
// some configurations
}
}
flavorDimensions "client"
productFlavors {
company1{
dimension "client"
buildConfigField("String", "BASE_URL", "\"https://app.company1/devApi/\"")
}
company2 {
dimension "client"
buildConfigField("String", "BASE_URL", "\"https://app.company2/devApi/\"")
}
}
EDIT:
I would like to be able to define a different BASE_URL
per each Flavor and Buildtype.
Flavor company1, BuildType debug
https://app.company1.com/devApi/
Flavor company1, BuildType release
https://app.company1.com/prodApi/
Flavor company2, BuildType debug
https://dev.company2.com/api/
Flavor company2, BuildType release
https://prod.company2.com/api/
Once the new project is created, by default it consists of two build types/variants - debug, release. Debug is the build type that is used when we run the application from the IDE directly onto a device. A release is the build type that requires you to sign the APK.
BuildConfig.FLAVOR gives you combined product flavor. So if you have only one flavor dimension: productFlavors { normal { } admin { } } Then you can just check it: if (BuildConfig. FLAVOR.
A build type determines how an app is packaged. By default, the Android plug-in for Gradle supports two different types of builds: debug and release . Both can be configured inside the buildTypes block inside of the module build file.
For my specific problem, where the URLs differ a lot one to another, I couldn't make it work with Flavours and BuildTypes.
I was able to define the debug/production URLs by using specific strings.xml
for each build variant (which is each combination of flavour and build type):
These are the folder structures to do so:
src/flavour1Debug/res/values/strings.xml
src/flavour1Release/res/values/strings.xml
and
src/flavour2Debug/res/values/strings.xml
src/flavour2Release/res/values/strings.xml
EXTRA:
This can also be used to host different google-services.json
files
Try something like this:
buildTypes {
debug {
buildConfigField("String", "BASE_URL_PATH", "\"devApi/\"")
}
release {
buildConfigField("String", "BASE_URL_PATH", "\"prodApi/\"")
}
}
flavorDimensions "client"
productFlavors {
company1{
dimension "client"
buildConfigField("String", "BASE_URL_DOMAIN", "\"https://app.company1/\"")
}
company2 {
dimension "client"
buildConfigField("String", "BASE_URL_DOMAIN", "\"https://app.company2/\"")
}
}
And use it like:
String BASE_URL = BuildConfig.BASE_URL_DOMAIN + BuildConfig.BASE_URL_PATH
Here is the working example
// Specifies one flavor dimension.
flavorDimensions "version"
productFlavors {
dev {
dimension "version"
applicationId "com.example.lk"
versionCode 1
versionName "1.0.1"
buildConfigField("String", "BASE_URL_PATH", "\"https://app.dev.com/api/v1/\"")
}
prod {
dimension "version"
applicationId "com.example.in"
versionCode 1
versionName "1.0.1"
buildConfigField("String", "BASE_URL_PATH", "\"https://app.prod.com/api/v1/\"")
}
staging {
dimension "version"
applicationId "com.example.other"
versionCode 1
versionName "1.0.1"
buildConfigField("String", "BASE_URL_PATH", "\"https://app.staging.com/api/v1/\"")
}}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
you can access it like this
String baseURL =BuildConfig.BASE_URL_PATH;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With