Frequently a developer will want to capture the build number and feature branch being built in TFS from within their Gradle build file.  The best way to capture these environment variables inside of Gradle for use internally in the project for displaying on a layout inside of your app or as a reference point inside of your BuildConfig Java class, simply use the System environment variables reference as indicated below:

buildConfigField("int", "BUILDNUMBER", "1")
if (System.getenv("BUILD_BUILDNUMBER") != null) {
buildConfigField("int", "BUILDNUMBER", Integer.parseInt("${System.getenv('BUILD_BUILDNUMBER')}"))
buildConfigField("String", "BRANCH", "\"local\"")
if (System.getenv('BUILDBRANCHNAME') != null) { //BUILD_SOURCEBRANCHNAME is only the name of the branch
buildConfigField("String", "BRANCH", "\"${System.getenv('BUILDBRANCHNAME')}\"")

What this does is to create constants inside the project’s generated BuildConfig class which can then be used as a reference in your app (ex: BuildConfig.BRANCH):

public final class BuildConfig {
public static final String BRANCH = "local";
public static final int BUILDNUMBER = 1;

So, after spending quite a while navigating through the predefined TFS variables, I was finally able to discover the correct reference for converting environment variables (specifically ‘build’ environment variables) for use in Gradle. I thought I would add a blog post to help someone else out that may be having this issue.