Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 301
Maher Abuthraa Avatar asked Feb 18 '16 09:02

Maher Abuthraa


2 Answers

  productFlavors.all {
        flavor ->
            defaultConfig.buildConfigField 'String', 'var_'+flavor.name, '\"'+flavor.name+'\"'
    }
like image 82
Lubos Horacek Avatar answered Nov 19 '22 05:11

Lubos Horacek


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

like image 28
Pi Vincii Avatar answered Nov 19 '22 05:11

Pi Vincii