Gradle DSL - resValue not writing in correct flavor path

Gradle Version: 2.1.2
Operating System: Mac os
IDE: Android Studio 2.1.3 with com.android.tools.build:gradle:2.1.2

The problem is the resValue it’s not being written in the correct path. It’s always writing the last/same values (generated.xml content) in all flavors.

MY CURRENT BUILD VARIANT IS “flavor_foo”

generated.xml in “android/app/build/generated/res/resValues/flavor_foo/debug/values/”
<?xml version="1.0" encoding="utf-8"?>

    <!-- Automatically generated file. DO NOT MODIFY -->

    <!-- Values from build type: debug -->
    <string name="app_name" translatable="false">DEV - BAR</string>

</resources>

generated.xml in “android/app/build/generated/res/resValues/flavor_bar/debug/values/”
<?xml version="1.0" encoding="utf-8"?>

    <!-- Automatically generated file. DO NOT MODIFY -->

    <!-- Values from build type: debug -->
    <string name="app_name" translatable="false">DEV - BAR</string>

</resources>

My build.gradle file:
apply plugin: ‘com.android.application’

android {
    compileSdkVersion 24
    buildToolsVersion "23.0.3"

    def FLAVOR_FOO_VERSION_CODE = 2
    def FLAVOR_BAR_VERSION_CODE = 10

    def FLAVOR_FOO_VERSION_NAME = "1.0"
    def FLAVOR_BAR_VERSION_NAME = "1.1.3"

    def DEV_PREFIX = "DEV - "

    def FLAVOR_FOO_APP_NAME = "FOO"
    def FLAVOR_BAR_APP_NAME = "BAR"


    defaultConfig {
        minSdkVersion 17
        targetSdkVersion 24

    }

    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
    }

    productFlavors {

        flavor_foo {
            applicationId "com.my.foo"
            versionCode FLAVOR_FOO_VERSION_CODE
            versionName FLAVOR_FOO_VERSION_NAME
            buildConfigField "String", "FLAVOR_NAME", '"boo"'

            buildTypes {
                release {
                    minifyEnabled false
                    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                    resValue "string", "app_name", FLAVOR_FOO_APP_NAME
                }
                debug {
                    resValue "string", "app_name", DEV_PREFIX + FLAVOR_FOO_APP_NAME
                }
            }

        }

        flavor_bar {
            applicationId "com.my.bar"
            versionCode FLAVOR_BAR_VERSION_CODE
            versionName FLAVOR_BAR_VERSION_NAME
            buildConfigField "String", "FLAVOR_NAME", '"bar"'

            buildTypes {
                release {
                    minifyEnabled false
                    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                    resValue "string", "app_name", FLAVOR_BAR_APP_NAME
                }
                debug {
                    resValue "string", "app_name", DEV_PREFIX + FLAVOR_BAR_APP_NAME
                }
            }
        }
    }
}

dependencies {
    ...
}

Flavors and BuildTypes are defined next to each other, not nested. You are overwriting your build types in the second call.

Ok it makes sense, can you give an example how can i have buildTypes per flavor? Thanks in advance

You can’t.

I think you should explain your use case in the Google Android forum, I’m sure they can help you find a solution.