buildConfigField 取决于风味 + buildType

时间:2023-02-13
本文介绍了buildConfigField 取决于风味 + buildType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据 flavor + buildType 定义一个 buildConfigVariable.理想情况下,这就是我想要的

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 确实包含草莓",但我不知道是否可以访问 variantbuildType.

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

放置在 Android 闭包之外我确实可以访问 BuildTypevariant,但是我无法调用 buildConfigField

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"]

我可以轻松创建 Java 工厂并根据一些 BuildConfig 常量返回适当的 API_KEY,但我宁愿保持代码配置不可知.

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.

推荐答案

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

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
}

目前不可能.缺少此 API.错误:https://code.google.com/p/android/issues/detail?id=67416

试试这个:

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

这篇关于buildConfigField 取决于风味 + buildType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:使用 gradle 编译 android 库时如何抑制警告? 下一篇:Android:运行时在 Lollipop 上而不是 IceCreamSandwich 上

相关文章

最新文章