Android Gradle. How to combine Flavors with buildTypes

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;