Maher Abuthraa Maher Abuthraa - 1 month ago 19
Android Question

How to list all flavors in buildConfig via gradle in Android

This is a simple gradle with three flavors :

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 is:

/**
* 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";
}


What I expected :

/**
* 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";
}


Weird thing that I can see all flavors in logs:
au
de
es

but I don't have fields in buildConfig for 'de' and 'es' as I expected .. any hint, guys ?

Answer
  productFlavors.all {
        flavor ->
            defaultConfig.buildConfigField 'String', 'var_'+flavor.name, '\"'+flavor.name+'\"'
    }