buildConfigField 取决于风味 + buildType

buildConfigField depending on flavor + buildType(buildConfigField 取决于风味 + buildType)
本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Android release APK crash with java.lang.AssertionError: impossible in java.lang.Enum(Android 发布 APK 因 java.lang.AssertionError 崩溃:在 java.lang.Enum 中不可能)
Finished with Non Zero Exit Value 3(以非零退出值 3 结束)
On gradle:3.0.0 More than one file was found with OS independent path #39;META-INF/ASL2.0#39;(在 gradle:3.0.0 上找到多个文件,其独立于操作系统的路径为“META-INF/ASL2.0)
Android : app loading library at runtime on Lollipop but not IceCreamSandwich(Android:运行时在 Lollipop 上而不是 IceCreamSandwich 上的应用程序加载库)
How do I suppress warnings when compiling an android library with gradle?(使用 gradle 编译 android 库时如何抑制警告?)
Error:Could not run build action using Gradle installation #39;C:Program FilesAndroidAndroid Studio1gradlegradle-2.2.1#39;(错误:无法使用 Gradle 安装 C:Program FilesAndroidAndroid Studio1gradlegradle-2.2.1 运行构建操作) - IT屋-程序员软件开发