Different string.xml files according to app Build Variants

android build variants vs flavors
please select the build variants to build and sign
android flavor dependencies
flavordimensions default
gradle variant
flavor in build gradle
android get build flavor programmatically
android applicationidsuffix

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?

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.

Configure build variants, Each build variant represents a different version of your app that you can build. You can create and configure build types in the module-level build.gradle file and product flavors, and names them according to <product-flavor><Build-Type> . If two files share the same name, such as two strings.xml files, priority is given​  Although you do not configure build variants directly, you do configure the build types and product flavors that form them. For example, a "demo" product flavor can specify different features and device requirements, such as custom source code, resources, and minimum API levels, while the "debug" build type applies different build and packaging

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

An Android Studio Gradle Build Variants Example, The goal of this chapter is to use the build variants feature of Android Studio to different source code bases in order to provide differing application behavior. a copy of the activity's activity_build_example.xml and strings.xml resource files. Android build types and build variants During development, we might need to configure the code to use different api endpoint, or other environment variables for dev, staging and prod builds . This can be done by defining different build types in the Android app build gradle file.

resValue 'string', '<string_name>', "some string"

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

Android Studio 2.2 Development Essentials, Edit the tablet flavor strings.xml file so that the variant_text string resource reads two flavors have been configured, each with different string and layout resources. change the Build Variant setting for the app module to phoneDebug before  With the initial project created, the next step is to configure the module level build.gradle file to add the two build flavor configurations. Within the Android Studio Project tool window, navigate to the build.gradle file listed as app -> Gradle Scripts -> build.gradle (Module: app) (Figure 58-1) and double click on it to load it into the editor:

Handling multiple Java source and resources using flavors on Gradle, Open the build.gradle (Module:app) and insert the code below on tag android {…​.} You can choose the build you want on “build variants” as showed below. This is These folder will be used by gradle to find the resources stored in the strings.​xml. Configuring multiple build using java class file strategy. Open the build.gradle (Module:app) and insert the code below on tag android {….} This will actived the multiple build on Android Studio. You can choose the build you want on “build variants

Android Studio Cookbook, Now, within the same folder, open the icon.xml file and make the drawable folder use different values (or classes or layouts) for the debug and release build types. Copy the strings.xml file from the main/res/values folder and paste it into the Build variants Select the app folder and choose Select Build Variant from the  The build system enables you to specify a different ProGuard rules file for each build variant. When building your app, the build system applies the appropriate set of rules to shrink your code and resources using its built-in shrinking tools, such as R8.

Android For Beginners. Developing Apps Using Android Studio, the same file name is possible in Flavors and in themain/ folder,e.g. strings.xml. You canselect individual build variants from the DropDown menu underthe Example: android { signingConfigs { production { storeFile file("other.keystore  That’s where your build tool can help. Let’s see how we can automate the process of generating different APKs for different environment with Gradle. Build variants. Gradle has the concepts of Build Types and Build Flavors. When combining the two, you get a Build Variant. There two default build types: release and debug.

Building Android Apps in Python Using Kivy with Android Studio: , The first string is named app_name and it defines the name of the application. Specifying the String Resources Inside the strings.xml File <?xml version="1.0" the required files based on such templates, Buildozer builds the Android project. It downloads the SDK and NDK according to the versions defined in the  Build type is the basic building block for having different configurations when building an Android app. The expectation is to build types to refer to the same app (e.g. debug and release build types: it's the same app but with some debug options enabled, different signing keys, etc).

Comments
  • This is much easier when you use buildVariant not buildType. In your example buildType is used
  • interesting, the answers on this page are for build types, not build variants...
  • So what would using buildVariant look like?
  • 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?
  • I got it now. I didn't have the directory structure right.
  • 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. :)
  • And I remove the entry in strings.xml?
  • @AlLelopath yes, now you can remove it form xml