How to access a buildConfigField elsewhere inside a gradle build file
Asked Answered
C

2

16

I am trying to set a buildConfigField in my productFlavor block, and then reference that field elsewhere in my gradle build file so that I can use this value when constructing the name for my apk.

e.g

productFlavors{
    flavor1{            
        buildConfigField "String", "APP_FLAVOR_NAME", '"MyApp-Flavor1"'

    }
    flavor2{            
        buildConfigField "String", "APP_FLAVOR_NAME", '"MyApp-Flavor2"'

    }

How can I access APP_FLAVOR_NAME elsewhere in my build.gradle script?

I am also struggling to figure out how to just reference the flavor name itself in the buildscript as another option to constructing the apk name. How can I do that as well?

Cannae answered 12/4, 2014 at 1:29 Comment(0)
A
16

Adding a buildConfigField writes an entry into your BuildConfig.java file and is not meant to be exposed at the buildscript level. This is a way provide meta-information about a specific build to interested application code -- not other buildscript code. If you want to throw names around at the buildscript level, you'll need to use Gradle properties.

Explicitly, if you want to access the BuildConfigField collection of objects elsewhere in your script, you can with android.productFlavors.flavor1.buildConfigFields.

Edit

To get a specific flavor name when iterating over android.applicationVariants.all, you can iterate over the flavors collection like so (note you don't need to capture the reference to the item you are iterating over, but it is instructive to see what objects you are actually accessing):

task printFlavors << {
    println "BuildTypes ⨯ Flavors:"
    android.applicationVariants.all { variant ->
        println name
        variant.productFlavors.each { flavor ->
            println flavor.name
        }
    }
}

Or, as suggested by kcoppock, this can be simplified to:

task printFlavorName << {
    android.applicationVariants.all {
        println flavorName
    }
}
Alexaalexander answered 12/4, 2014 at 1:52 Comment(7)
Could you suggest what would be the preferred way to reference the current variant's product flavor name if you were iterating over android.applicationVariants.all ?Cannae
Um I think it's name.Alexaalexander
Oh, I see what you want now. name refers to the variant. To get the flavor name, you'd need something like the edit above.Alexaalexander
@Alexaalexander @Cannae variant also has a flavorName field that you can use. I use it personally for naming APK outputs and I'm pretty certain that's what you're looking for. :)Coad
@kcoppock that's really helpful, I entirely missed that last time I was sifting through the android gradle plugin..Alexaalexander
It'd be nice if there was better autocomplete or documentation for these things, but it'll come in time, I'm sure.Coad
Yeah - since the autocomplete didn't popup I didn't find it, but I got the right property using ${variant.flavorName}Cannae
A
1

Will like to add on to dcow answer.

After getting the flavor, to get the BuildConfig field :

variant.productFlavors.each { flavor ->
    flavor.buildConfigFields.each { key, value ->
        if(key == "APP_FLAVOR_NAME") {
            println value.type
            println value.name
            println value.value
        }    
    }
}

BuildConfigs are defined as a final class. Fields are private. So only read-only.

Aplanatic answered 2/9, 2017 at 10:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.