I have 2 buildTypes (debug, release) and 2 productFlavors (product1, product2). I want to define a buildConfigField for each buildType and productFlavors. The buildConfigField is the url the app to download data from the server, and it changes for each productFlavor and buildTypes.
Now, I have:
buildTypes {
debug {
debuggable true
}
release {
debuggable false
}
}
productFlavors {
product1 {
buildConfigField STRING, "URL_BASE", '"https://api1.release.com"'
}
product2 {
buildConfigField STRING, "URL_BASE", '"https://api2.release.com"'
}
}
But I want something like this:
buildTypes {
debug {
debuggable true
}
release {
debuggable false
}
}
productFlavors {
product1 {
debug {
buildConfigField STRING, "URL_BASE", '"https://api1.debug.com"'
}
release {
buildConfigField STRING, "URL_BASE", '"https://api1.release.com"'
}
product2 {
debug {
buildConfigField STRING, "URL_BASE", '"https://api2.debug.com"'
}
release {
buildConfigField STRING, "URL_BASE", '"https://api2.release.com"'
}
}
}
How I can achieve this?
Update:
Each URL_BASE has a different pattern so I can't group the URLs. A posible solution is to add the url base of the 2 flavor in the different build types and select the right one in the flavor.
buildTypes {
debug {
debuggable true
buildConfigField STRING, API_VARIANT_PRODUCT1, '"api1.deb.com"'
buildConfigField STRING, API_VARIANT_PRODUCT2, '"api2.debug.com"'
}
release {
debuggable false
buildConfigField STRING, API_VARIANT_PRODUCT1, '"api1.release.com"'
buildConfigField STRING, API_VARIANT_PRODUCT2, '"api2.release.com"'
}
}
productFlavors {
product1 {
buildConfigField STRING, URL_BASE, '"https://" + API_VARIANT_PRODUCT1 + "/v1"'
}
product2 {
buildConfigField STRING, URL_BASE, '"https://" + API_VARIANT_PRODUCT2 + "/v1"'
}
}
}
UPDATE 2
If you need to add resources in gradle, like a 'KEY_MAP' the solution is in this page.
Build Type is not part of Product Flavor and vice versa. Variant is calculated based on both build type and product flavor. Using this you can just create either an extension (option 1) or a property (option 2) with a consistent format using both product flavor and build type.
Option 1
ext.product1_release_base_url = 'http://baseurl.myproduct/public'
ext.product2_release_base_url = 'http://baseurl.yourproduct/secure'
ext.product1_debug_base_url = 'http://debugurl.myproduct/test'
ext.product2_debug_base_url = 'http://yourproduct/debug'
android {
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled false
}
}
productFlavors {
product1 {}
product2 {}
}
}
project.android.applicationVariants.all {variant ->
def url = project.ext."${variant.flavorName}_${variant.buildType.name}_base_url"
variant.buildConfigField('String', 'URL_BASE', "\"${url}\"")
}
Option 2
in gradle.properties
product1_release_base_url = 'http://baseurl.myproduct/public'
product2_release_base_url = 'http://baseurl.yourproduct/secure'
product1_debug_base_url = 'http://debugurl.myproduct/test'
product2_debug_base_url = 'http://yourproduct/debug'
in build.gradle
android {
buildTypes {
release {}
debug {}
}
productFlavors {
product1 {}
product2 {}
}
}
project.android.applicationVariants.all {variant ->
def url = project."${variant.flavorName}_${variant.buildType.name}_base_url"
variant.buildConfigField('String', 'URL_BASE', "\"${url}\"")
}
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