Define buildconfigfield for an specific flavor AND

2019-03-10 15:25发布

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?

8条回答
小情绪 Triste *
2楼-- · 2019-03-10 16:10

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"
    }
}
查看更多
相关推荐>>
3楼-- · 2019-03-10 16:13

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
        }
    }
查看更多
Evening l夕情丶
4楼-- · 2019-03-10 16:13

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"
    }
}
查看更多
放我归山
5楼-- · 2019-03-10 16:15
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
            }
        }
    }
查看更多
爷、活的狠高调
6楼-- · 2019-03-10 16:20

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楼-- · 2019-03-10 16:22

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]}\""
}
查看更多
登录 后发表回答