12

I want to assign a value to a string in string.xml different values depending on the Build Variant/buildType. I imagine something like this:

res/values-debug/string.xml
    <string name="my_string">some debug value</string>

res/values-release/string.xml
    <string name="my_string">some release value</string>

but I don't see anything like this out there. Is this possible?

Nicola Gallazzi
  • 7,897
  • 6
  • 45
  • 64
Al Lelopath
  • 6,448
  • 13
  • 82
  • 139

4 Answers4

19

It possible via your build.gradle file

buildTypes {
    release {
        resValue "string", "my_string", "some release value"
    }
    debug {
        resValue "string", "my_string", "some debug value"
    }
}

Then you can just use this value like @string/my_string where you want

Volodymyr
  • 6,393
  • 4
  • 53
  • 84
8

Yes it's possible!

In your build.gradle you can add something like this:

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        signingConfig signingConfigs.release
    }
    debug {
        applicationIdSuffix ".debug"
        minifyEnabled false
        debuggable true
        signingConfig signingConfigs.release
    }
}

Then in your src folder in your project you should have a folder called main, add one next to it called debug. Den as long as you are building your debug flavour any resources in your debug folder will replace those in main which is the release folder.

Should look like this:

src/
    main/
        java/ -- all your java code
        res/
            ...
            values/
                strings.xml
    debug/
        res/
            ...
            values/
                strings.xml

EDIT: The approaches from the two other answers works fine as well. But if you have a lot of strings keeping them as xml might be easier to handle.

just_user
  • 11,769
  • 19
  • 90
  • 135
  • 1
    I would like to use this approach because then I could also add mipmap folders. I can't get it to work though. I've added to the question to show you what I am doing. Can you see what I'm doing wrong? – Al Lelopath Dec 16 '16 at 16:45
  • 1
    I got it now. I didn't have the directory structure right. – Al Lelopath Dec 16 '16 at 17:15
  • 1
    I'm glad you like this way! I find it very useful to be able to distinguish the two apps, debug and release with icons, names, urls and whatever and this approach does it the best way. :) – just_user Dec 19 '16 at 12:11
3
resValue 'string', '<string_name>', "some string"

define different ones in your build.gradle for different build variants/product flavors

John O'Reilly
  • 10,000
  • 4
  • 41
  • 63
3

You can do it directly from Android Studio. For example, if you need a different app name for your "staging" flavor, you can (Android Studio v3.5):

  • Right click on values folder -> New -> Values resource file
  • Select "staging" in the source set menu
  • specify strings.xml as filename

At this point Android Studio generates an additional strings.xml file for your particular build variant. Edit the created file with your "staging" app name (E.g. MyAppName - Staging)

strings.xml - staging strings.xml - production

build.gradle(app)

productFlavors {
        stage {
            applicationIdSuffix ".staging"
            buildConfigField 'String', 'SITE_URL', '"[staging_link_here]"'
        }
        prod {
            buildConfigField 'String', 'SITE_URL', '"[production_link_here]"'
        }
 }
Nicola Gallazzi
  • 7,897
  • 6
  • 45
  • 64