Android 在代码中使用 Gradle Build 风格,就像 if case

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24119557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:48:48  来源:igfitidea点击:

Android using Gradle Build flavors in the code like an if case

androidbuildandroid-gradle-plugin

提问by Stephan Celis

I'm trying to work with build flavors. In my build.gradle I've defined 2 flavors, a normal flavor and an admin flavor.

我正在尝试使用构建风格。在我的 build.gradle 中,我定义了 2 种风格,一种普通风格和一种管理风格。

Basicly the admin flavor has an extra button on the main activity.

基本上,管理员风格在主要活动上有一个额外的按钮。

I understand that I can define different packages/classes for different flavours. But is there a way to make a sort of if case to add/remove a piece of code depending on the flavor?

我知道我可以为不同的口味定义不同的包/类。但是有没有办法制作一种 if case 来根据风格添加/删除一段代码?

Basicly I would need two versions of an Activity. But I don't want two entire different versions of the activity and maintain them.

基本上我需要一个活动的两个版本。但我不想要活动的两个完全不同的版本并维护它们。

So in my activity I would like to do

所以在我的活动中我想做

=> gradle check if flavour is 'admin' => if yes add this code of the button

=> gradle 检查风味是否为 'admin' => 如果是,请添加此按钮代码

is this possible? Or would you need two different physical activities and thus maintain both of them when you add functionality afterwards.

这可能吗?或者您是否需要两种不同的体育活动,从而在之后添加功能时同时保持这两种活动。

回答by mixel

BuildConfig.FLAVORgives you combined product flavor. So if you have only one flavor dimension:

BuildConfig.FLAVOR为您提供组合产品风味。因此,如果您只有一个风味维度:

productFlavors {
    normal {
    }

    admin {
    }
}

Then you can just check it:

然后你可以检查它:

if (BuildConfig.FLAVOR.equals("admin")) {
    ...
}

But if you have multiple flavor dimensions:

但是,如果您有多个风味维度:

flavorDimensions "access", "color"

productFlavors {
    normal {
        dimension "access"
    }

    admin {
        dimension "access"
    }

    red {
        dimension "color"
    }

    blue {
        dimension "color"
    }
}

there are also BuildConfig.FLAVOR_accessand BuildConfig.FLAVOR_colorfields so you should check it like this:

还有BuildConfig.FLAVOR_accessBuildConfig.FLAVOR_color字段,所以你应该像这样检查它:

if (BuildConfig.FLAVOR_access.equals("admin")) {
    ...
}

And BuildConfig.FLAVORcontains full flavor name. For example, adminBlue.

BuildConfig.FLAVOR包含完整的风味名称。例如,adminBlue

回答by user2878850

To avoid plain string in the condition, you can define a boolean property:

为了避免条件中的纯字符串,您可以定义一个布尔属性:

productFlavors {
    normal {
        flavorDimension "access"
        buildConfigField 'boolean', 'IS_ADMIN', 'false'
    }

    admin {
        flavorDimension "access"
        buildConfigField 'boolean', 'IS_ADMIN', 'true'
    }
}

Then you can use it like this:

然后你可以像这样使用它:

if (BuildConfig.IS_ADMIN) {
    ...
}

回答by auspicious99

You can define either different build configuration fields or different resource values (like string values) per flavor, e.g. (as per Google's gradle tips and recipes), e.g.,

您可以为每种口味定义不同的构建配置字段或不同的资源值(如字符串值),例如(根据Google 的 gradle 提示和配方),例如,

android {
  ...
  buildTypes {
    release {
      // These values are defined only for the release build, which
      // is typically used for full builds and continuous builds.
      buildConfigField("String", "BUILD_TIME", "\"${minutesSinceEpoch}\"")
      resValue("string", "build_time", "${minutesSinceEpoch}")
      ...
    }
    debug {
      // Use static values for incremental builds to ensure that
      // resource files and BuildConfig aren't rebuilt with each run.
      // If they were dynamic, they would prevent certain benefits of
      // Instant Run as well as Gradle UP-TO-DATE checks.
      buildConfigField("String", "BUILD_TIME", "\"0\"")
      resValue("string", "build_time", "0")
    }
  }
}

So in this case, something like

所以在这种情况下,就像

productFlavors {
    normal {
        dimension "access"
        buildConfigField("boolean", "IS_ADMIN", "false")
    }

    admin {
        dimension "access"
        buildConfigField("boolean", "IS_ADMIN", "true")
    }
}

and then use it like

然后像这样使用它

if (BuildConfig.IS_ADMIN) {
    ...
} else {
    ...
}

or if it is just to have different string values for different flavors, it can be done with different resValuesand then you don't even need the if/then

或者如果只是为了不同的口味有不同的字符串值,它可以用不同的来完成,resValues然后你甚至不需要if/then