buildConfigField depending on flavor + buildType

陌路散爱 提交于 2019-11-28 06:30:56

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"'
}

Warning: this might be a fragile solution, use at your own risk. See https://code.google.com/p/android/issues/detail?id=67416

This is how I accomplished what I wanted. You need to change the values just before the task is executed, so I needed a way to hook my code in there.

final projectName = project.name
gradle.taskGraph.beforeTask { Task task ->
    if (task.path ==~ /:$projectName:generate.*BuildConfig/) {
        //extracts flavor and buildType name. See http://stackoverflow.com/a/7594052/218473 for regex
        final values = task.name.replace("generate","").replace("BuildConfig","").split("(?<!(^|[A-Z]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])")

        final flavorName = values[0].toLowerCase()
        final buildTypeName = values[1].toLowerCase()

        com.android.build.gradle.api.ApplicationVariant variant;
        android.applicationVariants.all { currentVariant ->
            if (currentVariant.getVariantData().getVariantConfiguration().getBuildType().getName() == buildTypeName) {
                if (currentVariant.getVariantData().getVariantConfiguration().getFlavorName() == flavorName) {
                    variant = currentVariant;
                }
            }
        }

        if(variant != null) {
            com.android.builder.internal.ClassFieldImpl apiKeyField
            if (flavorName == 'strawberry') {    
                if (buildTypeName == 'release') {
                    apiKeyField = new com.android.builder.internal.ClassFieldImpl("String", "WS_API_KEY", '"strawberry_release"')
                } else {
                    apiKeyField = new com.android.builder.internal.ClassFieldImpl("String", "WS_API_KEY", '"strawberry_debug"')
                }
            } else if (flavorName == 'chocolate') {                        
                if (buildTypeName == 'release') {
                    apiKeyField = new com.android.builder.internal.ClassFieldImpl("String", "WS_API_KEY", '"chocolate_release"')
                } else {
                    apiKeyField = new com.android.builder.internal.ClassFieldImpl("String", "WS_API_KEY", '"chocolate_debug"')
                }
            }

            variant.getVariantData().getVariantConfiguration().getFlavorConfigs().get(0).addBuildConfigField(apiKeyField)
        }
    }
}

To understand why this works, download the AOSP source code and check VariantConfiguration.getBuildConfigItems()

Using Xavier's answer throws a MethodMissingError. If I use variant.mergedFlavor.addBuildConfigField() there's no error, but the variable isn't added.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!