Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Use different strings for different application flavors

I have an android application that has 2 different flavors/variants. How can I have different strings for each flavor (e.g app name, Button labels etc)? What I have done until now, that is actually working but not exactly as I would like is:
I have created two flavors using gradle. Specifically I have created a flavor1_res and a flavor2_res folder and put inside them flavor specific resources.
Specifically I have put under each folder a strings.xml file. Each file contains the same string labels but the values of the strings are different. The application is build ok from the command line (using gradlew assembleFlavor1 for example), but in the Android Studio my strings are not recognized and I get all these compilation errors.
Is there another way to do that? How can I make Android Studio recognise the two strings.xml files? (I have already added the corresponding folders as source folders).

Thanks, Thomas

like image 492
Thomas Kaliakos Avatar asked Jul 02 '13 12:07

Thomas Kaliakos


2 Answers

You can declare resource strings in the build.gradle file like this:

productFlavors {
    devel {
        resValue 'string', 'fancy_name', 'Development App'
    }
    prod {
        resValue 'string', 'fancy_name', 'Production App'
    }
}

So now depending on which flavour you are building, the 'fancy_name' string will be either 'Development App' or 'Production App'. I use this all the time.

like image 104
Alexis Farmer Avatar answered Sep 21 '22 00:09

Alexis Farmer


If I understand the question correctly, Android Studio (AS) is not recognising the resources folder for what it is. If in build.gradle (app) you have the following:

productFlavors {
    free {
        applicationIdSuffix ".free"
        versionNameSuffix "-free"
    }
    paid {
        applicationIdSuffix ".paid"
        versionNameSuffix "-paid"
    }
}

There is a pane in AS called "Build Variants" where free or paid can be selected. So if you click free, AS will recognise the res folder under free for what it is.

like image 35
David Avatar answered Sep 18 '22 00:09

David