Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use different resources for different application flavors using gradle


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

like image 923
Thomas Kaliakos Avatar asked Jun 14 '13 07:06

Thomas Kaliakos


People also ask

What are buildTypes and product Flavours in Gradle and what can you use them for?

Build Variants are the result of Gradle using a specific set of rules to combine settings, code, and resources configured in your build types and product flavors. Build Type applies different build and packaging settings. An example of build types are “Debug” and “Release”.

What is Gradle product flavors?

Product flavours lets you create multiple variants of an android app while using a single codebase. To create product flavours you need to define rules in the build. gradle which you'll learn shortly.

When would you use a product flavors in your build setup?

When to use Product Flavors. When we want to address the issue of having separate project code for each version of the app while still having one project code. Given a scenario where you have a free and a paid app you can limit features in the free and expose all the other features in the paid version of the app.

How do I get the current flavor in Gradle?

There is no direct way to get the current flavor; the call getGradle().


1 Answers

The solution was to add directory

values

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

like image 87
Thomas Kaliakos Avatar answered Oct 27 '22 13:10

Thomas Kaliakos