androidgradlebuild-variant

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?


Solution

  • 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.