0

I'm wondering if there is a way to force getting strings from the custom file? Let's say I would like to have testing strings set and use them in the debug mode. As far as I know, there is no possibility to create a custom Locale and use for example values-test folder. But maybe there is the possibility to override something and force getting strings from the assets folder file?

falsetto
  • 789
  • 2
  • 11
  • 35

2 Answers2

0

If you want to separate string resources between release and debug - just separate them in folders (check this answer):

project
  -app
    -src
      -debug
        -java
          ...
        -res
          -values
            -strings.xml
      -release
        -java
          ...
        -res
          -values
            -strings.xml
      -main
        -java
          ...
        -res
          -values
            -strings.xml

But if you want to override string resources there is great and useful library Restring.

You can put your own string resources to it and it will replace your default ones. Successfully used in project with 10M+ installs.

Marat Zangiev
  • 1,172
  • 7
  • 12
  • Well, I want to have the possibility to jump between languages while using the app so building debug version with the specific strings is not a good idea. I will look into this library, thanks a lot. – falsetto Dec 01 '22 at 11:44
  • why not?) create strings-en.xml etc. in both release and debug folder> And it will work fine :) – Marat Zangiev Dec 01 '22 at 11:47
  • create strings for all languages you want and put them in both release and debug – Marat Zangiev Dec 01 '22 at 11:49
0

You can do that in build.gradle(app) by using following snipped of code.

android {
    buildTypes {
        debug {
            resValue 'string', 'api_key', '"000000000"'
        }
        release {
            resValue 'string', 'api_key', '"123456789"'
        }
    }
}

Usage (Java)

val text = resources.getString(R.string.api_key)
Toast.makeText(this, text, Toast.LENGTH_SHORT).show()

Usage (Kotlin)

String text = resources.getString(R.string.api_key)
Toast.makeText(this, text, Toast.LENGTH_SHORT).show()

Add this, sync your project & build your project. You may seem red warning, ignore that and use it.

No need to manually check for BuildConfigs.

Sohaib Ahmed
  • 1,990
  • 1
  • 5
  • 23