Define buildconfigfield for an specific flavor AND

2019-03-10 15:50发布

问题:

I have 2 flavors, lets say Vanilla and Chocolate. I also have Debug and Release build types, and I need Vanilla Release to have a field true, while the other 3 combinations should be false.

def BOOLEAN = "boolean"
def VARIABLE = "VARIABLE"
def TRUE = "true"
def FALSE = "false"

    VANILLA {

        debug {

            buildConfigField BOOLEAN, VARIABLE, FALSE

        }

        release {

            buildConfigField BOOLEAN, VARIABLE, TRUE

        }


    }

    CHOCOLATE {
        buildConfigField BOOLEAN, VARIABLE, FALSE
    }

I'm having an error, so I guess the debug and release trick doesnt work. It is possible to do this?

回答1:

Loop the variants and check their names:

productFlavors {
    vanilla {}
    chocolate {}
}

applicationVariants.all { variant ->
    println("Iterating variant: " + variant.getName())
    if (variant.getName() == "chocolateDebug") {
        variant.buildConfigField "boolean", "VARIABLE", "true"
    } else {
        variant.buildConfigField "boolean", "VARIABLE", "false"
    }
}


回答2:

Here is a solution without lacks I've described under Simas answer

buildTypes {
    debug {}
    release {}
}

productFlavors {
    vanilla {
        ext {
            variable = [debug: "vanilla-debug value", release: "vanilla-release value"]
        }
    }
    chocolate {
        ext {
            variable = [debug: "chocolate-debug value", release: "chocolate-release value"]
        }
    }
}

applicationVariants.all { variant ->
    def flavor = variant.productFlavors[0]
    variant.buildConfigField "boolean", "VARIABLE", "\"${flavor.variable[variant.buildType.name]}\""
}


回答3:

Within the Gradle build system, buildTypes and productFlavors are unfortunately two separate entities.

As far as I am aware, to complete what you want to achieve, you would need to create another build flavour as such:

buildTypes {
        debug{}
        release {}
    }

    productFlavors {
        vanillaDebug {
             buildConfigField BOOLEAN, VARIABLE, FALSE
        }
        vanillaRelease {
             buildConfigField BOOLEAN, VARIABLE, TRUE
        }
        chocolate {
             buildConfigField BOOLEAN, VARIABLE, FALSE
        }
    }


回答4:

Here's how I solved this:

def GAME_DIMENSION = "game"
def BUILD_DIMENSION = "building"

flavorDimensions GAME_DIMENSION, BUILD_DIMENSION

productFlavors {
    lollipop {
        dimension BUILD_DIMENSION
        minSdkVersion 21
    }

    normal {
        dimension BUILD_DIMENSION
    }

    game_1 {
        dimension GAME_DIMENSION
        ext {
            fields = [
                    [type: 'String', name: 'TESTSTRING', values: [debug: 'debugstring', release: 'releasestring']],
                    [type: 'int', name: 'TESTINT', values: [debug: '1234', release: '31337']]
            ]
        }
    }

    game_2 {
        dimension GAME_DIMENSION
        ext {
            fields = []  // none for game_2
        }
    }
}

applicationVariants.all { variant ->

    // get the GAME dimension flavor
    def game = variant.getProductFlavors()
            .findAll({ flavor -> flavor.dimension == GAME_DIMENSION})
            .get(0)

    println "Adding " + game.ext.fields.size() + " custom buildConfigFields for flavor " + variant.name

    // loop over the fields and make appropriate buildConfigField
    game.ext.fields.each { field ->
        def fldType = field['type']
        def fldName = field['name']
        def fldValues = field['values']

        // get debug/release specific value from values array
        def fldSpecificValue = fldValues[variant.getBuildType().name]

        // add quotes for strings
        if (fldType == 'String') {
            fldSpecificValue = '\"' + fldSpecificValue + '\"'
        }

        println "    => " + fldType + " " + fldName + " = " + fldSpecificValue
        variant.buildConfigField fldType, fldName, fldSpecificValue
    }
}

(I have not yet been able to determine whether or not ext.fields exists on a flavor)



回答5:

For your specific case, you can also just play with defaultConfig:

defaultConfig {
    buildConfigField BOOLEAN, VARIABLE, TRUE
}

buildTypes {
    debug {
        buildConfigField BOOLEAN, VARIABLE, FALSE
    }
    release {
    }
}

productFlavors {
    VANILLA {
    }
    CHOCOLATE {
        buildConfigField BOOLEAN, VARIABLE, FALSE
    }
}

The Default value is TRUE, but then you put FALSE to all Debug builds and all Chocolate builds. So the only remaining TRUE is VANILLA-release.



回答6:

You can try this for multiple products flavors:

productFlavors {
        demo {
            applicationId "com.demo"
            versionCode 1
            versionName '1.0'
            ext {
                APP_BASE_URL = [debug: "${BASE_URL_DEV}", release: "${BASE_URL_PRODUCTION}"]
            }
        }
        demo1 {
            applicationId "com.demo1"
            versionCode 1
            versionName '1.2'
            ext {
                APP_BASE_URL = [debug: "${BASE_URL_DEV}", release: "${BASE_URL_PRODUCTION}"]
            }
        }


    applicationVariants.all { variant ->
            def flavor = variant.productFlavors[0]
            variant.buildConfigField "String", "BASE_URL", "${flavor.ext.APP_BASE_URL[variant.buildType.name]}"
        }


回答7:

I know it's an old thread but it's the first that popped up when I searched for this question. Actually, this is working with targetSdkVersion 28

flavorDimensions "type"
productFlavors {
    vanilla {
        dimension "type"
        applicationIdSuffix ".vanilla"
        buildConfigField "boolean", "IS_VANILLA", true
        resValue "string", "app_name", "Vanilla"
    }
    chocolate {
        dimension "type"
        applicationIdSuffix ".chocolate"
        buildConfigField "boolean", "IS_VANILLA", false
        resValue "string", "app_name", "Chocolate"
    }
}


回答8:

productFlavors {
    vanilla {}
    chocolate {}
}

buildTypes {
        release {
            productFlavors.vanilla {
                //your configuration for vanilla flavor with release buildType
            }
        }
        debug {
            productFlavors.chocolate{
                //your configuration for chocolate flavor with debug buildType
            }
        }
    }