How to manage configuration based on build type and build flavor

381 Views Asked by At

In my app I have 2 build types release and debug, below is the code snippet.

buildTypes {
    getByName("debug") {
        applicationIdSuffix = ".debug"
        versionNameSuffix = ".debug"
        buildConfigField("boolean", "DEBUG_MODE", "true")
    }

    getByName("release") {
        proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
        buildConfigField("boolean", "DEBUG_MODE", "false")
    }
}

and I also have 2 product flavors below is the code snippet.

flavorDimensions("version")
productFlavors {
    create("flavor1") {
        setDimension("version")
    }
    create("flavor2") {
        setDimension("version")
        versionCode = 1
        versionName = "1.0"
    }
}

Now in total I have 4 product types

  • flavor1debug
  • flavor1release
  • flavor2debug
  • flavor2release

Now I need to create a variable app_name so that the app name can be different for different product types.

  • flavor1debug ---------> app_name : flavor1 (debug)
  • flavor1release ---------> app_name : flavor1
  • flavor2debug ---------> app_name : flavor2 (debug)
  • flavor2release ---------> app_name : flavor2

How do I achieve the same using configuration or strings.xml

Edit: Let's assume, I am using a third party library and I want to use different authentication key, how can I achieve this using Flavor & buildType combination?

i.e I need 4 different keys based on following configuration

  1. Flavor1 + debug ========= Key1
  2. Flavor1 + release ========= Key2
  3. Flavor2 + debug =========== Key3
  4. Flavor2 + release ========== Key4

any help would be appreciated.

2

There are 2 best solutions below

4
On

Declare strings.xml in every source set and then you can put different values for different build type and flavor check this

1
On
Try this,


productFlavors {
   flavorA {
        setDimension("version")
        manifestPlaceholders = [appLabel : "FlavourA"]

    }
   flavorB {
        setDimension("version")
        versionCode = 1
        versionName = "1.0"
        manifestPlaceholders = [appLabel : "FlavourB"]

    }
}


In android manifest file set this appLabel.

 <application
        android:name=".ApplicationClass"
        android:icon="@mipmap/logo_launcher"
        android:label="${appLabel}"    // Add this line.
        android:largeHeap="true"
        android:theme="@style/AppTheme"
/>