Change string resource with flavor / debug-build

Say we have strings_test.xml that stores string values ​​for testing and should be displayed in debug-release. When apk receives the assembly as the release version, all values ​​should be changed to an empty string, for example. <string name="test_some_card_text">@string/empty</string>.

Is there any way to achieve this?

As always, thanks in advance.

+4
source share
4 answers

Yes, you can do this in your gradle application under buildTypes ..

 buildTypes {
        mybuild {
                 resValue "string", "test_some_card_text", '"test"'
                 resValue "string", "other_text", '"other"'
                }
         debug {
                 resValue "string", "test_some_card_text", '"test"'
                 resValue "string", "other_text", '"other"'
              }
          }

Then access this.

getApplicationContext().getResources().getString(R.string.test_some_card_text);
getApplicationContext().getResources().getString(R.string.other_text);

To build, you need to select this one build variantsand create it.

+7
source

Yes, Gradle allows you to redefine strings.

  • buildTypes {} /build.gradle

    debug {               applicationIdSuffix "debug"           }

  • debug main. , . (, , , .)

  • , strings_test.xml res/values, debug/ strings_text.xml . . , release/main/res/values, .

PS: res , , buildTypes . Java, .

+3

apk. edittext... etx apk.

+1

@Aditya Naik , Flavors.

BuildType β†’ Flavor β†’ main β†’ Dependencies.

, , , .

Note that for the merge area, resources of the same type (type, name), but different qualifiers are processed separately.

This means that if src / main / res has

  • Res / layout / foo.xml
  • res / layout-land / foo.xml

    and src / debug / res has

  • res / layout / foo.xml

    Then the combined resource folder will contain foo.xml by default from src / debug / res, but the landscape version from src / main / res

for more information visit White Paper - Merging Resources

+1
source

Source: https://habr.com/ru/post/1622162/


All Articles