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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

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

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)