5

这是一个具有三种口味的简单 gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.apipas.app.listallflavor"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    productFlavors {

        au {
            applicationId 'com.apipas.app.listallflavor.au'
            resConfigs 'en-rAU'
        }
        es {
            applicationId 'com.apipas.app.listallflavor.es'
            resConfigs 'es'
        }

        de {
            applicationId 'com.apipas.app.listallflavor.de'
            resConfigs 'de'
        }

    }
    productFlavors.all {
        flavor ->
            println( flavor.name)
            buildConfigField 'String', 'var_'+flavor.name, '\"'+flavor.name+'\"'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
}

BuildConfig.java 是:

/**
 * Automatically generated file. DO NOT MODIFY
 */
package com.apipas.app.listallflavor;

public final class BuildConfig {
  public static final boolean DEBUG = Boolean.parseBoolean("true");
  public static final String APPLICATION_ID = "com.apipas.app.listallflavor.au";
  public static final String BUILD_TYPE = "debug";
  public static final String FLAVOR = "au";
  public static final int VERSION_CODE = 1;
  public static final String VERSION_NAME = "1.0";
  // Fields from product flavor: au
  public static final String var_au = "au";
}

我的期望:

/**
 * Automatically generated file. DO NOT MODIFY
 */
package com.apipas.app.listallflavor;

public final class BuildConfig {
  public static final boolean DEBUG = Boolean.parseBoolean("true");
  public static final String APPLICATION_ID = "com.apipas.app.listallflavor.au";
  public static final String BUILD_TYPE = "debug";
  public static final String FLAVOR = "au";
  public static final int VERSION_CODE = 1;
  public static final String VERSION_NAME = "1.0";
  // Fields from product flavor: au
  public static final String var_au = "au";
  public static final String var_de = "de";
  public static final String var_es = "es";
}

奇怪的事情,我可以在日志中看到所有口味:au de es

但我没有像我预期的那样在 buildConfig 中为 'de' 和 'es' 提供字段.. 有什么提示吗,伙计们?

4

4 回答 4

11
  productFlavors.all {
        flavor ->
            defaultConfig.buildConfigField 'String', 'var_'+flavor.name, '\"'+flavor.name+'\"'
    }
于 2016-02-18T11:50:44.537 回答
2

您看到这个文件是因为在您的 IDE(我猜是 android studio)中,您选择构建变体是AuDebug. 如果您想查看其他变体。您可以选择Build -> Select Build Variant...然后选择要构建的变体。然后重新构建,可以看到 BuildConfig 文件的变化。

每个 BuildConfig 都是根据每个变体(风格和构建类型)生成的。您可以在 [your application directory]/[your app]/build/generated/source/[your flavor]/[your build type]/[your package]/BuildConfig.java 上看到您的 BuildConfig

于 2016-02-18T10:12:02.443 回答
0

我想出了如何解决这个问题..感谢 Pi Vincii

为每个变体循环所有风味并使用变体引用设置 buildConfig 的解决方案

gradle中android里面的代码:

applicationVariants.all { variant ->
    android.productFlavors.all {
        flavor ->
            println(flavor.name)
            variant.buildConfigField 'String', 'var_' + flavor.name, '\"' + flavor.name + '\"'
    }
}
于 2016-02-18T10:45:21.157 回答
0

基于接受的解决方案,我有一个稍微修改过的版本,它创建了大写的 contant 名称,例如

public static final String FLAVOR_BETA = "beta"; public static final String FLAVOR_DEV = "dev"; public static final String FLAVOR_PRODUCTION = "production";

 applicationVariants.all { variant ->
    android.productFlavors.all {
        flavor ->
            println(flavor.name)
            variant.buildConfigField 'String', 'FLAVOR_' + flavor.name.toUpperCase(), '\"' + flavor.name + '\"'
    }
}
于 2020-02-13T13:35:14.940 回答