13

Let's say we have strings_test.xml, which stores string values for testing and should be shown in a debug-release. When the apk gets build as a release version all values should be change to an empty string e.g. <string name="test_some_card_text">@string/empty</string>.

Is there a possibility to achieve this?

As always, thanks in advance.

Martin Pfeffer
  • 12,471
  • 9
  • 59
  • 68

5 Answers5

24

Yes you can do that inside your app gradle 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 it like this.

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

For build you need to select that build variants and have to build it.

Madhukar Hebbar
  • 3,113
  • 5
  • 41
  • 69
4

Yes, Gradle lets you override strings.

  • Add this inside buildTypes{} in your app/build.gradle

    debug { applicationIdSuffix "debug" }

  • That should create a directory titled debug next to main. If not then manually create one. (Seriously, I haven't tried this, but I know this is possible.)

  • Then if your strings_test.xml is under res/values, create similar directory structure under debug/ and put your strings_text.xml with debug specific strings there. This will show up in your debug build. The ones under release/main/res/values will show up in your release build.

PS: You can override all res and asset data like this according to buildTypes and flavor. You can't override Java files though, you could however add them.

Aditya Naique
  • 1,120
  • 13
  • 25
4

As @Aditya Naik said it is possible using Flavors. Official doc says

BuildType -> Flavor -> main -> Dependencies.

This means that if a resource is declared in both the Build Type and in main, the one from Build Type will be selected.

Note that for the scope of the merging, resources of the same (type, name) but different qualifiers are handled 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 merged resource folder will contain the default foo.xml from src/debug/res but the landscape version from src/main/res

for more info visit Official doc - Resource Merging

Bharatesh
  • 8,943
  • 3
  • 38
  • 67
1

It is not possible to change the string value after creation of the apk. But you can assing the value to text or edittext ... etx dynamically after creation of the apk.

ManiTeja
  • 849
  • 1
  • 9
  • 13
  • Yes, thats true.. But it seems to be a bit unhandy to override each TextView/EditText itself. There should be a possibility by providing different build flavors, I guess, but I didn't find a solution yet. – Martin Pfeffer Dec 30 '15 at 04:53
  • can you let me know why you want to change the string value so that i can give you some idea – ManiTeja Dec 30 '15 at 04:56
  • When I test my apps I want to be able to insert some dummy data in the GUI (including multiple lines in various combinations, etc) -> let's call it stress-testing the layouts. :) But it would be nice if these test-resources are overridden in a release build. – Martin Pfeffer Dec 30 '15 at 05:02
  • yes but we cannot do it .create a table in the database with name and id and pass all the values to the db and from to gui .It is bit lengthy but only one time process.for one of my gui i did the same thing. – ManiTeja Dec 30 '15 at 05:08
0

For those who come here looking for some way to apply a similar method to raw resources, I dealt with it using buildConfigField.

gradle
...
buildTypes {
    debug {
        ...

        buildConfigField "int", "shared_resource_name", 'R.raw.debug_resource_name'

        ...
    }
    prod {
        ...

        buildConfigField "int", "shared_resource_name", 'R.raw.prod_resource_name'

        ...
    }
}

Pay attention to the quotes. After that, place BuildConfig.shared_resource_name in the files wherever R.raw.resource_value used to be accessed directly.

This can be used to other resources I think.

brooksrelyt
  • 3,925
  • 5
  • 31
  • 54
phsa
  • 61
  • 7