buildConfigField depending on flavor + buildType

11,469

Edit2: The version after 0.14.2 will allow doing this:

applicationVariants.all { variant ->
    variant.buildConfigField "int", "VALUE", "1"
}

So you'd be able to do something like this (to match the original question):

applicationVariants.all { variant ->
    variant.buildConfigField "String", "WS_API_KEY", variant.productFlavors.get(0).name + '_' + variant.buildType.name
}

Edit: it's not currently possible. The API is missing for this. Bug: https://code.google.com/p/android/issues/detail?id=67416

Try this:

applicationVariants.all { variant ->
    variant.mergedFlavor.buildConfigField "String", "NAME", '"VALUE"'
}

Share:
11,469

Related videos on Youtube

Maragues
Author by

Maragues

Android & Web Developer SOreadytohelp

Updated on November 16, 2020

Comments

  • Maragues
    Maragues over 3 years

    I'm trying to define a buildConfigVariable depending on the flavor + buildType. Ideally, this is what I want

    productFlavors {
        strawberry {
            buildConfigField "String", "WS_API_KEY", name + variant.buildType.name
        }
        ... more flavors ..
    }
    

    name does contain "strawberry", but I don't know if it's possible to access the variant's buildType.

    Placed outside the Android closure I do have access to the BuildType and variant, but then I can't invoke buildConfigField

    android.applicationVariants.all { variant ->
        println "****************************"
        println "variant: ${variant.name}"
        println "flavor: ${variant.flavorName}"
        println "****************************"
    
        if (variant.buildType.name == 'release') {
            if (variant.flavorName == 'strawberry') {
                buildConfigField "String", "WS_API_KEY", '"strawberry_release"'
            } else {
                buildConfigField "String", "WS_API_KEY", '"chocolate_release"'
            }
        } else if(variant.buildType.name == 'debug') {
            if (variant.flavorName == 'strawberry') {
                buildConfigField "String", "WS_API_KEY", '"strawberry_debug"'
            } else {
                buildConfigField "String", "WS_API_KEY", '"chocolate_debug"'
            }
        }
    
    
    ****************************
    variant: strawberryRelease
    flavor: strawberry
    ****************************
    org.gradle.api.internal.MissingMethodException: 
        Could not find method buildConfigField() 
        for arguments [String, WS_API_KEY, "strawberry_release"]
    

    I can easily create a Java factory and return the appropriate API_KEY depending on some BuildConfig constants, but I'd rather keep the code configuration agnostic.

  • Maragues
    Maragues over 10 years
    That throws an error MissingMethodException: No signature of method: com.android.builder.DefaultProductFlavor.buildConfigField() is applicable for argument types: (java.lang.String, java.lang.String, java.lang.String) values: [String, NAME, "VALUE"] Possible solutions: getBuildConfigFields()
  • Maragues
    Maragues over 10 years
    I have a solution of my own tho it's probably fragile, so I'd be happy to see a better one. I'll post it in 5 minutes.
  • Xavier Ducrohet
    Xavier Ducrohet over 10 years
    oh yeah, mergedFlavor is not of the proper type for this. let me look.
  • Maragues
    Maragues over 10 years
    Hi Xavier, I've updated my answer with a working version, but I'm curious if there's a better way (there must be, I guess). Thanks for your work!
  • Xavier Ducrohet
    Xavier Ducrohet over 10 years
    well this looks quite ugly, but I guess there's no better way right now. We'll have to improve this. code.google.com/p/android/issues/detail?id=67416
  • Maragues
    Maragues over 10 years
    Thanks, I'll keep an eye on that bug.
  • JJD
    JJD over 9 years
    @XavierDucrohet Any news here?
  • TWiStErRob
    TWiStErRob over 9 years
    You can use the public API, no need for ClassFieldImpl: com.android.builder.model.ClassField apiKeyField and then declare def createClassField = com.android.builder.core.AndroidBuilder.&createClassField and then instead of calling new ClassFieldImpl(...) you can just call createClassField(...).