buildConfigField取决于flavor + buildType

时间:2014-03-19 12:45:59

标签: android android-gradle android-productflavors

我尝试根据 flavor + buildType 定义 buildConfigVariable 。理想情况下,这就是我想要的

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

名称确实包含" strawberry",但我不知道是否可以访问变种&# 39; s buildType

放置在 Android 闭包之外我可以访问 BuildType 变体,但是我无法调用{{ 1}}

buildConfigField

我可以轻松地创建一个Java工厂并根据一些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"] 常量返回相应的API_KEY,但我宁愿保持代码配置不可知。

2 个答案:

答案 0 :(得分:15)

Edit2:0.14.2之后的版本允许这样做:

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

所以你可以做这样的事情(以匹配原始问题):

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

编辑:目前无法实现。缺少API。 错误:https://code.google.com/p/android/issues/detail?id=67416

试试这个:

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

<击>

答案 1 :(得分:0)

警告:这可能是一个脆弱的解决方案,使用风险自负。请参阅https://code.google.com/p/android/issues/detail?id=67416

这就是我完成我想要的方式。您需要在执行任务之前更改值,因此我需要一种方法将代码挂钩到那里。

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)
        }
    }
}

要了解其工作原理,请下载AOSP源代码并检查 VariantConfiguration.getBuildConfigItems()

使用Xavier的答案会引发MethodMissingError。如果我使用 variant.mergedFlavor.addBuildConfigField(),则没有错误,但是没有添加变量。