Maher Abuthraa
Maher Abuthraa

Reputation: 17813

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 ?

Upvotes: 7

Views: 14395

Answers (4)

Szabolcs Becze
Szabolcs Becze

Reputation: 537

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 + '\"'
    }
}

Upvotes: 0

Lubos Horacek
Lubos Horacek

Reputation: 1582

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

Upvotes: 11

Maher Abuthraa
Maher Abuthraa

Reputation: 17813

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 + '\"'
    }
}

Upvotes: 0

Pi Vincii
Pi Vincii

Reputation: 609

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

Upvotes: 2

Related Questions