Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change string resource by flavor / debug-build

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.

like image 793
Martin Pfeffer Avatar asked Dec 30 '15 04:12

Martin Pfeffer


People also ask

How do I change a variant in build?

NOTE: By default, the Android Studio will generate "debug" and "release" Build Types for your project. So, to change a Build Type, all you need to do is just select your Build Type from the Build Variant and after the project sync, you are good to go.

When would you use product flavor in your build setup?

You use same core ingredients to make the base but will use different toppings for each one to have a different taste. Similarly, android apps can have the same base functionalities with changes to some of the features like styles, logo etc. This can be achieved using product flavours.

How do I get strings from resources?

String string = getString (R. string. hello); You can use either getString(int) or getText(int) to retrieve a string.

What is buildType in Gradle android?

Build types define certain properties that Gradle uses when building and packaging your app, and are typically configured for different stages of your development lifecycle.


2 Answers

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

like image 31
Bharatesh Avatar answered Oct 26 '22 22:10

Bharatesh


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.

like image 95
Madhukar Hebbar Avatar answered Oct 26 '22 22:10

Madhukar Hebbar