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?
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).
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.
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.
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.
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
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):
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)
build.gradle(app)
productFlavors {
stage {
applicationIdSuffix ".staging"
buildConfigField 'String', 'SITE_URL', '"[staging_link_here]"'
}
prod {
buildConfigField 'String', 'SITE_URL', '"[production_link_here]"'
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With