Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different string.xml files according to app Build Variants

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?

like image 347
Al Lelopath Avatar asked Dec 16 '16 15:12

Al Lelopath


People also ask

What are strings XML?

A single string that can be referenced from the application or from other resource files (such as an XML layout). Note: A string is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file).

What are build variants?

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. Although you do not configure build variants directly, you do configure the build types and product flavors that form them.

What is string XML file in android?

String Array. XML resource that provides an array of strings. Quantity Strings (Plurals) XML resource that carries different strings for pluralization. All strings are capable of applying some styling markup and formatting arguments.

In which folder can you find the string resource file strings XML?

The XML resource files containing localized strings are placed in subfolders of the project's res folder. Android uses a special folder-naming scheme to automatically choose the correct localized resources—for example, the folder values-fr would contain a strings.


2 Answers

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

like image 183
Volodymyr Avatar answered Oct 06 '22 17:10

Volodymyr


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]"'
        }
 }
like image 28
Nicola Gallazzi Avatar answered Oct 06 '22 19:10

Nicola Gallazzi