androidgradleandroid-gradle-pluginbuild.gradleandroid-resources

Defining 'resValue' using an existing string definition


I'm interested in defining my many flavors of my apps more so in the strings.xml files rather than the build.gradle. Given multiple flavors, I'd like a simplified release/debug variant:

  buildTypes {
    release {
        signingConfig signingConfigs.release
        resValue "string", "app_name", "@string/prod_name"
    }
    debug {
        applicationIdSuffix ".beta"
        resValue "string", "app_name", "@string/beta_name"
    }

Then in each of my build flavors' custom res/values/strings.xml files, I would define each their own "prod_name" and "beta_name". I also want to use this similar framework for defining providers' authorities etc...

This currently will build fine via gradle command-line, but fails to be recognized by Android Studio.

Android Studio Error:

I find this within 'generated.xml'

<!-- Values from build type: debug -->
<string name="app_name">@string/beta_name</string>

Which is typical of how one string references another. But this time Android Studio gives me this error:

Error:(7, 29) No resource found that matches the given name 
(at 'app_name' with value '@string/beta_name').

I'm using Android Studio 2.1 Preview 5


Solution

  • In my experience you can't resolve a @string/my_string in the resValue DSL. Gradle put the value as a simple string inside the resource file.

    In this case you can use different folder to achieve it:

    Just use:

    src/release/res/values/strings.xml
    src/debug/res/values/strings.xml
    

    If you would like to use different resource for each build variant (build type+flavor) you can use:

    src/flavor1Release/res/values/strings.xml
    src/flavor1Debug/res/values/strings.xml
    src/flavor2Release/res/values/strings.xml
    src/flavor2Debug/res/values/strings.xml