18


I have an android application and I would like to have different flavors. Specifically I would like to have 2 flavors and for each flavor use different strings (different strings.xml file) and maybe have some icons different.

I have tried creating two folders in the project's root folder: flav1 and flav2 and used the following build.gradle

android {
    compileSdkVersion "Google Inc.:Google APIs:15"
    buildToolsVersion "17.0.0"

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        instrumentTest.setRoot('tests')
    }

    productFlavors {
        flav1 {
            packageName "com.ic.flav1"
        }

        flav2 {
            packageName "com.ic.flav2"
        }
    }

    android.sourceSets.flav2 {
        res {
            srcDir 'flav2'
        }
        resources {
            srcDir 'flav2'
        }
    }
    android.sourceSets.flav1 {
        res {
            srcDir 'flav1'
        }
        resources {
            srcDir 'flav1'
        }
    }
}

The result of this is that none of the strings is recognized, getting multiple errors of the following type:

build FAILED :

error: Error: No resource found that matches the given name (at 'contentDescription' with value '@string/txt_addr').

Am I missing something? How should the build.gradle be?

Thanks

Mark Parnell
  • 9,175
  • 9
  • 31
  • 36
Thomas Kaliakos
  • 3,274
  • 4
  • 25
  • 39

1 Answers1

9

The solution was to add directory

values

under each corresponding res folder, and all the strings were recognized.

Thomas Kaliakos
  • 3,274
  • 4
  • 25
  • 39
  • what is the difference between "res" and "resources" ? i'm an AS newbie coming from eclipse... i see the folder in my project but i'm not sure what it does and how you would benefit from having a separate "resources" for each flavor? – Lou Morda May 07 '15 at 14:38
  • Res and resources is the same thing. It's just the naming convention that AS uses ("res"). Having different resources for each flavor would give you the opportunity for example to have different images for the same resource id (e.g. R.id.logo can be a different image in different flavors) – Thomas Kaliakos May 07 '15 at 20:47