Android using Gradle Build flavors in the code like an if case

AndroidBuildAndroid Gradle-Plugin

Android Problem Overview


I'm trying to work with build flavors. In my build.gradle I've defined 2 flavors, a normal flavor and an admin flavor.

Basicly, the admin flavor has an extra button on the main activity.

I understand that I can define different packages/classes for different flavors. But is there a way to make a sort of if case to add/remove a piece of code depending on the flavor?

Basicly, I would need two versions of an Activity. But I don't want two entire different versions of the activity and maintain them.

So in my activity I would like to do

=> gradle check if flavor is 'admin'

=> if yes, add this code of the button

Is this possible? Or would you need two different physical activities and thus maintain both of them when you add functionality afterwards.

Android Solutions


Solution 1 - Android

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.equals("admin")) {
    ...
}

But if you have multiple flavor dimensions:

flavorDimensions "access", "color"

productFlavors {
    normal {
        dimension "access"
    }

    admin {
        dimension "access"
    }

    red {
        dimension "color"
    }

    blue {
        dimension "color"
    }
}

there are also BuildConfig.FLAVOR_access and BuildConfig.FLAVOR_color fields so you should check it like this:

if (BuildConfig.FLAVOR_access.equals("admin")) {
    ...
}

And BuildConfig.FLAVOR contains full flavor name. For example, adminBlue.

Solution 2 - Android

To avoid plain string in the condition, you can define a boolean property:

productFlavors {
    normal {
        flavorDimension "access"
        buildConfigField 'boolean', 'IS_ADMIN', 'false'
    }

    admin {
        flavorDimension "access"
        buildConfigField 'boolean', 'IS_ADMIN', 'true'
    }
}

Then you can use it like this:

if (BuildConfig.IS_ADMIN) {
    ...
}

Solution 3 - Android

You can define either different build configuration fields or different resource values (like string values) per flavor, e.g. (as per Google's gradle tips and recipes), e.g.,

android {
  ...
  buildTypes {
    release {
      // These values are defined only for the release build, which
      // is typically used for full builds and continuous builds.
      buildConfigField("String", "BUILD_TIME", "\"${minutesSinceEpoch}\"")
      resValue("string", "build_time", "${minutesSinceEpoch}")
      ...
    }
    debug {
      // Use static values for incremental builds to ensure that
      // resource files and BuildConfig aren't rebuilt with each run.
      // If they were dynamic, they would prevent certain benefits of
      // Instant Run as well as Gradle UP-TO-DATE checks.
      buildConfigField("String", "BUILD_TIME", "\"0\"")
      resValue("string", "build_time", "0")
    }
  }
}

So in this case, something like

productFlavors {
    normal {
        dimension "access"
        buildConfigField("boolean", "IS_ADMIN", "false")
    }

    admin {
        dimension "access"
        buildConfigField("boolean", "IS_ADMIN", "true")
    }
}

and then use it like

if (BuildConfig.IS_ADMIN) {
    ...
} else {
    ...
}

or if it is just to have different string values for different flavors, it can be done with different resValues and then you don't even need the if/then

Solution 4 - Android

You can try this way

   productFlavors {
    def app_name = "you app name"
    development {
        versionCode 1
        versionName "1.0.1"          
        buildConfigField 'String', 'varibalename', ""           
    }

    release {
        versionCode 1
        versionName "1.0.1"               
        buildConfigField 'String', 'varibalename', ""           
    }     
}

if(BuildConfig.varibalename){}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStephan CelisView Question on Stackoverflow
Solution 1 - AndroidmixelView Answer on Stackoverflow
Solution 2 - Androiduser2878850View Answer on Stackoverflow
Solution 3 - Androidauspicious99View Answer on Stackoverflow
Solution 4 - AndroidgpuserView Answer on Stackoverflow