How to list all flavors in buildConfig via gradle in Android
Asked Answered
W

4

7

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 ?

Weixel answered 18/2, 2016 at 9:57 Comment(0)
S
11
  productFlavors.all {
        flavor ->
            defaultConfig.buildConfigField 'String', 'var_'+flavor.name, '\"'+flavor.name+'\"'
    }
Sextain answered 18/2, 2016 at 11:50 Comment(0)
S
2

You see this file because in your IDE (I guess android studio) you choose build variant is AuDebug. If you want to see another variant. You can select Build -> Select Build Variant... then choose what variant you want to build. Then rebuild and you can see the change of BuildConfig file.

Each BuildConfig is generate based on each variant(flavor and build type). You can see your BuildConfig on [your application directory]/[your app]/build/generated/source/[your flavor]/[your build type]/[your package]/BuildConfig.java

Spinoff answered 18/2, 2016 at 10:12 Comment(1)
Does that mean I need to loop all flavour for all variant to get all flavors in each variant ?Weixel
W
0

I figured out how to solve that .. thanks Pi Vincii

solution to loop all flavors for each variant and use variant references to set buildConfig

code inside android in gradle:

applicationVariants.all { variant ->
    android.productFlavors.all {
        flavor ->
            println(flavor.name)
            variant.buildConfigField 'String', 'var_' + flavor.name, '\"' + flavor.name + '\"'
    }
}
Weixel answered 18/2, 2016 at 10:45 Comment(0)
U
0

Based on the accepted solution I have a slightly modified version which creates uppercased contant names like

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 + '\"'
    }
}
Udell answered 13/2, 2020 at 13:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.